我正在尝试使用PHPunit来测试输出一些自定义标头的类.
问题是在我的机器上这个:
<?php
class HeadersTest extends PHPUnit_Framework_TestCase {
public function testHeaders()
{
ob_start();
header('Location: foo');
$headers_list = headers_list();
header_remove();
ob_clean();
$this->assertContains('Location: foo', $headers_list);
}
}
Run Code Online (Sandbox Code Playgroud)
甚至这个:
<?php
class HeadersTest extends PHPUnit_Framework_TestCase {
public function testHeaders()
{
ob_start();
header('Location: foo');
header_remove();
ob_clean();
}
}
Run Code Online (Sandbox Code Playgroud)
返回此错误:
name@host [~/test]# phpunit --verbose HeadersTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
E
Time: 0 seconds, Memory: 2.25Mb
There was 1 error:
1) HeadersTest::testHeaders
Cannot modify header information - headers already sent by (output started at /usr/local/lib/php/PHPUnit/Util/Printer.php:173)
/test/HeadersTest.php:9 …Run Code Online (Sandbox Code Playgroud) 什么是避免phpunit必须为模拟对象调用构造函数的方法?否则我需要一个模拟对象作为构造函数参数,另一个需要等等.api似乎是这样的:
getMock($className, $methods = array(), array $arguments = array(),
$mockClassName = '', $callOriginalConstructor = TRUE,
$callOriginalClone = TRUE, $callAutoload = TRUE)
Run Code Online (Sandbox Code Playgroud)
我不懂它.它仍然抱怨构造函数参数,即使$callOriginalConstructor设置为false.
我只在构造函数中有一个对象,它是一个依赖注入.所以我认为那里没有设计问题.
我最近通过pear安装程序在我的服务器上安装了phpunit.
当我去运行测试时,我收到以下错误:
PHP警告:require_once(PHPUnit/Util/Filter.php):无法打开流:第44行/ usr/bin/phpunit中没有此类文件或目录
PHP致命错误:require_once():在44行/ usr/bin/phpunit中打开所需的'PHPUnit/Util/Filter.php'(include_path ='.:/ usr/bin/php')失败
在做了一些搜索之后,我尝试对服务器上的php.ini文件中的include_path进行一些修改.但这没有做到.
知道可能导致这种情况的原因吗?
我通过PEAR 1.9.0安装PHPUnit 3.4.6时遇到问题.在我发现channel pear.phpunit.de并尝试使用以下命令之一后:
pear安装phpunit/PHPUnit
pear install --alldeps phpunit/PHPUnit
pear install --onlyreqdeps phpunit/PHPUnit
它失败了,给我以下错误:
没有可用于"pear.phpunit.de/PHPUnit"包的版本
安装失败
我发现有几个线程来自同样问题的人,但将PEAR升级到最新版本通常适用于他们.似乎还有几乎没有安装PHPUnit for Windows的教程.
编辑:
我也尝试将prefered_state更改为beta; 没有帮助.
我还尝试获取所有可用包的列表:
pear remote-list -c phpunit
它给了我另一个错误:
无法下载非http网址"/c/categories.xml"
我正在运行一大堆phpunit测试,我希望看到哪个测试失败后立即失败,而不是等待所有测试完成然后让它列出所有失败.
我怎么能告诉phpunit这样做?
如何选择要执行的特定测试套件?
$ phpunit --configuration config.xml
config.xml文件:
<testsuites>
<testsuite name="Library">
<directory>library</directory>
</testsuite>
<testsuite name="XXX_Form">
<file>library/XXX/FormTest.php</file>
<directory>library/XXX/Form</directory>
</testsuite>
</testsuites>
Run Code Online (Sandbox Code Playgroud) 为了提高我的代码质量,我决定尝试学习如何使用单元测试而不是我最平庸的测试解决方案来测试我的代码.
我决定使用composer安装PHPUnit,用于个人库,允许我实现常见的数据库功能.起初我没有PHPUnit的配置文件,当我运行如下命令:
$ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个终端命令,所以我没有包含.php扩展名.上面提到的GeneralStringFunctionsTest实际上是一个GeneralStringFunctionsTest.php文件.
输出是我的预期:
时间:31毫秒,内存:2.75Mb
好的(1个测试,1个断言)
然后,我尝试使用配置文件自动加载测试套件,而不必每次都手动输入文件.我创建了一个phpunit.xml在我的根目录中调用的文件,并在文件中输入以下内容:http://pastebin.com/0j0L4WBD:
<?xml version = "1.0" encoding="UTF-8" ?>
<phpunit>
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
现在,当我运行命令时:
phpunit
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Sebastian Bergmann和贡献者的PHPUnit 4.5.0.
配置从/Users/muyiwa/Projects/DatabaseHelper/phpunit.xml中读取
时间:16毫秒,内存:1.50Mb
没有执行测试!
如果它有用,我的目录结构如下:
src - 顶级目录(包含我的所有源代码)
测试 - 顶级目录(包含我的所有测试,结构与我的src文件夹相同)
供应商 - Composer第三方文件
我还有作曲家json和锁定文件,以及顶层的phpunit xml文件作为文件.
phpunit.xml为tests/GeneralStringFunctionsphpunit.xml为./testsphpunit.xml文件移动到tests目录,然后将目录更改为./而不是tests.phpunit.xml以将"测试"指定为显式后缀.我正在尝试使用PHPUnit测试我的symfony2应用程序.我有一个项目,其中一切都按预期工作,但在我的另一个项目中,我有这种奇怪的行为,PHPUnit在所有测试结束后随机停止执行Test Suite,并在完成Test Suite并编写代码后重新启动或重新启动测试覆盖.其他时候它正常运行.
这里有一些输出可以看到正在发生的事情(测试重复一遍又一遍):
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from C:\workspace\cllctr\app\phpunit.xml
................................................................. 65 / 83 ( 78%)
...........PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from C:\workspace\cllctr\app\phpunit.xml
................................................................. 65 / 83 ( 78%)
...PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from C:\workspace\cllctr\app\phpunit.xml
................................................................. 65 / 83 ( 78%)
............PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from C:\workspace\cllctr\app\phpunit.xml
................................................................. 65 / 83 ( 78%)
............PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from C:\workspace\cllctr\app\phpunit.xml
................................................................. 65 / 83 ( 78%)
.................. …Run Code Online (Sandbox Code Playgroud) 我正在使用与jenkins相关的phpunit,我想通过在XML文件中设置配置来跳过某些测试 phpunit.xml
我知道我可以在命令行上使用:
phpunit --filter testStuffThatBrokeAndIOnlyWantToRunThatOneSingleTest
如何将其转换为XML文件,因为<filters>标记仅用于代码覆盖?
我想进行所有测试 testStuffThatAlwaysBreaks
单元测试是测试一小段代码的方法(主要是单一方法).
集成测试是测试多个代码区域之间交互的测试(希望它们已经有了自己的单元测试).有时,部分测试代码需要其他代码以特定方式执行操作.这就是Mocks&Stubs的用武之地.因此,我们非常具体地模拟/删除部分代码.这使我们的集成测试可以预测地运行而没有副作用.
所有测试都应该能够独立运行而无需数据共享.如果需要数据共享,这表明系统没有足够的分离.
当与外部API(特别是将使用POST请求修改实时数据的RESTful API)进行交互时,我理解我们可以(应该?)模拟与该API的交互(在此答案中更加雄辩地说明)进行集成测试.我也理解我们可以单元测试与该API交互的各个组件(构造请求,解析结果,抛出错误等).我没有得到的是如何实际解决这个问题.
如何测试与具有副作用的外部API的交互?
一个完美的例子是Google的Content API用于购物.为了能够执行手头的任务,它需要大量的准备工作,然后执行实际请求,然后分析返回值.其中一些没有任何"沙盒"环境.
执行此操作的代码通常具有相当多的抽象层,例如:
<?php
class Request
{
public function setUrl(..){ /* ... */ }
public function setData(..){ /* ... */ }
public function setHeaders(..){ /* ... */ }
public function execute(..){
// Do some CURL request or some-such
}
public function wasSuccessful(){
// some test to see if the CURL request was successful
}
}
class GoogleAPIRequest
{
private $request;
abstract protected function …Run Code Online (Sandbox Code Playgroud)