我想要通过测试得到以下内容:"此测试没有执行任何断言"
我知道我可以添加类似assertTrue(true)的内容但是是否可以在配置中添加一些东西以使这些测试通过?
我很确定这只发生在PHPUnit 3.5.0版本之后,引入了--strict
Ulr*_*rdt 15
使用 PHPUnit 7.2,您可以获得另一种选择:
public function testCodeWithoutUsingAssertions()
{
$this->expectNotToPerformAssertions();
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
另见https://github.com/sebastianbergmann/phpunit/pull/3042。
Dav*_*ess 13
编辑:根据您使用的版本,您有几个选择,是要忽略所有有风险的测试还是仅忽略一些,以及您希望它是永久的还是临时的。
在 5.6 之前,如果您不想在所有测试中添加虚假断言,则必须避免传递--strict给 PHPUnit 或添加strict="false"到您的phpunit.xml. 此选项的重点是“如果没有断言,则将测试标记为不完整”。
在某些时候,PHPUnit 添加了相关的--dont-report-useless-tests命令行开关和beStrictAboutTestsThatDoNotTestAnything="false"配置选项。我还没有检查它们是替代品还是额外的细粒度版本。
上述选项会影响所有风险测试。使用它们会让你在没有断言的情况下意外编写测试。以下新选项更安全,因为您必须有目的地标记您希望允许的每个风险测试。
PHPUnit 5.6添加了@doesNotPerformAssertions注释以将单个测试用例标记为“无风险”,即使它们不执行断言。
/**
* @doesNotPerformAssertions
*/
public function testWithoutAsserting() {
$x = 5;
}
Run Code Online (Sandbox Code Playgroud)
PHPUnit 7.2引入了TestCase::expectNotToPerformAssertions()它做同样的事情。
public function testWithoutAsserting() {
$this->expectNotToPerformAssertions();
$x = 5;
}
Run Code Online (Sandbox Code Playgroud)
Nat*_*hur 11
使用@doesNotPerformAssertions注释:
/**
* @doesNotPerformAssertions
*/
public function testCodeWithoutUsingAssertions()
{
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
利用$this->addToAssertionCount(1).见下文.
class NoAssertTest extends PHPUnit_Framework_TestCase
{
function testWithoutAssertions() {
$x = 5;
// Increment the assertion count to signal this test passed.
// This is important if you use a @depends on this test
$this->addToAssertionCount(1);
}
}
Run Code Online (Sandbox Code Playgroud)