有没有人知道是否有一个assert或类似的东西可以测试是否在被测试的代码中抛出异常?
我奋力奔跑名为单个测试方法testSaveAndDrop在文件中escalation/EscalationGroupTest.php使用phpunit.我尝试了以下组合:
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=escalation/EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=testSaveAndDrop
Run Code Online (Sandbox Code Playgroud)
在每种情况下,执行文件中的所有测试方法escalation/EscalationGroupTest.php.如何选择一种方法呢?
该类的名称是3.2.8 EscalationGroupTest的版本phpunit.
我发现讨论你是否测试私人方法的信息.
我已经决定,在某些类中,我想要保护方法,但测试它们.其中一些方法是静态的和简短的.由于大多数公共方法都使用它们,我可能会在以后安全地删除测试.但是对于从TDD方法开始并避免调试,我真的想测试它们.
我想到了以下几点:
哪个是最佳做法?还有别的事吗?
看来,JUnit会自动将受保护的方法更改为公开,但我没有深入了解它.PHP不允许通过反射.
如何使用PHPUnit测试抽象类的具体方法?
我希望我必须创建某种对象作为测试的一部分.虽然,我不知道最好的做法,或者如果PHPUnit允许这样做.
在运行PHPUnit测试时,我希望能够转储输出,这样我就可以调试一两件事.
我尝试了以下(类似于PHPUnit手册示例);
class theTest extends PHPUnit_Framework_TestCase
{
/**
* @outputBuffering disabled
*/
public function testOutput() {
print_r("Hello World");
print "Ping";
echo "Pong";
$out = "Foo";
var_dump($out);
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
PHPUnit @package_version@ by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.00Mb
OK (1 test, 0 assertions)
Run Code Online (Sandbox Code Playgroud)
请注意,没有预期的输出.
我在2011年9月19日使用了HEAD版本的git repos.
产量php -version:
$ php -version
PHP 5.2.9 (cli) (built: Dec 8 2010 11:36:37)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend …Run Code Online (Sandbox Code Playgroud) 我有一个PHPUnit模拟对象,'return value'无论它的参数是什么都返回:
// From inside a test...
$mock = $this->getMock('myObject', 'methodToMock');
$mock->expects($this->any))
->method('methodToMock')
->will($this->returnValue('return value'));
Run Code Online (Sandbox Code Playgroud)
我想要做的是根据传递给mock方法的参数返回一个不同的值.我尝试过类似的东西:
$mock = $this->getMock('myObject', 'methodToMock');
// methodToMock('one')
$mock->expects($this->any))
->method('methodToMock')
->with($this->equalTo('one'))
->will($this->returnValue('method called with argument "one"'));
// methodToMock('two')
$mock->expects($this->any))
->method('methodToMock')
->with($this->equalTo('two'))
->will($this->returnValue('method called with argument "two"'));
Run Code Online (Sandbox Code Playgroud)
但是如果没有使用参数调用mock,这会导致PHPUnit抱怨'two',所以我假设定义methodToMock('two')覆盖了第一个的定义.
所以我的问题是:有没有办法让PHPUnit模拟对象根据其参数返回不同的值?如果是这样,怎么样?
我想知道是否有任何有这方面经验的人可以对这两者之间的显着差异有所了解吗?
每种特定的强度使其适合任何特定情况?
当数组中元素的顺序不重要或甚至可能发生变化时,断言两个对象数组相等的好方法是什么?
PHPUnit包含一个assertEquals方法:https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertEquals
它还有一个assertSame方法:https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertSame
乍一看,看起来他们做同样的事情.两者有什么区别?他们为什么都指定了?
有没有办法为不同的输入参数定义不同的模拟期望?例如,我有一个名为DB的数据库层类.此类具有名为"Query(string $ query)"的方法,该方法在输入时采用SQL查询字符串.我可以为此类(DB)创建模拟,并为依赖于输入查询字符串的不同Query方法调用设置不同的返回值吗?