PHPUnit通过了一个在配置中没有断言的测试

PHPUnit通过了一个在配置中没有断言的测试,phpunit,Phpunit,我想通过以下测试:“此测试未执行任何断言” 我知道我可以添加类似于assertTrue(true)的内容,但是是否可以向配置中添加一些内容以使这些测试通过得更干净 我敢肯定,只有在phpunit3.5.0版本引入 --strict编辑:您有一些选择,这取决于您使用的版本,是要忽略所有风险测试还是仅忽略一些测试,以及您希望它是永久性的还是临时性的 在5.6之前,如果您不想在所有测试中添加虚假断言,那么必须避免将--strict传递到PHPUnit,或者将strict=“false”添加到PHPUn

我想通过以下测试:“此测试未执行任何断言”

我知道我可以添加类似于
assertTrue(true)
的内容,但是是否可以向配置中添加一些内容以使这些测试通过得更干净

我敢肯定,只有在phpunit3.5.0版本引入
--strict

编辑:您有一些选择,这取决于您使用的版本,是要忽略所有风险测试还是仅忽略一些测试,以及您希望它是永久性的还是临时性的

在5.6之前,如果您不想在所有测试中添加虚假断言,那么必须避免将
--strict
传递到PHPUnit,或者将
strict=“false”
添加到
PHPUnit.xml
。此选项的要点是“如果没有断言,则将测试标记为不完整”

在某些情况下,PHPUnit添加了相关的
--不要报告无用的测试
命令行开关和
bestrictaboutteststhattonottestanything=“false”
config选项。我还没有检查它们是替换版本还是其他细粒度版本

上述选项会影响所有风险测试。使用它们会让您在没有断言的情况下意外编写测试。以下新选项更安全,因为您必须有目的地标记希望允许的每个风险测试

PHPUnit添加了注释,以将单个测试用例标记为“无风险”,即使它们不执行断言

/**
 * @doesNotPerformAssertions
 */
public function testWithoutAsserting() {
    $x = 5;
}
PHPUnit介绍了做同样事情的方法

public function testWithoutAsserting() {
    $this->expectNotToPerformAssertions();
    $x = 5;
}

如果您有PHP SimpleTest,另一种方法是使用:

$this->pass();
这将标记测试已完成并通过

另一方面,对于您希望失败的测试,您可以使用:

$this->fail();
例如:

if (someComplicatedLogicValidation) {
    // Do more stuff and asserts
} else{
    $this->fail();
}
我在PHP5.5中尝试了这一点,效果如下:

function testThatWorks() {
    $this->pass();
}

function testThatFails() {
    $this->fail();
}
输出:

1) 在[…ExampleTest.unit.php第23行]失败 在测试中,它失败了 例如,测试 在…/ExampleTest.unit.php中 在3.02s中失败

也许方法pass仍然可以在PHPUnit中轻松实现。来源于SimpleTest:

function pass($message = "Pass") {
    if (! isset($this->reporter)) {
        trigger_error('Can only make assertions within test methods');
    }
    $this->reporter->paintPass(
    $message . $this->getAssertionLine());
    return true;
}

使用
$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);
    }
}
使用:


使用PHPUnit 7.2,您可以获得另一种选择:

public function testCodeWithoutUsingAssertions()
{
    $this->expectNotToPerformAssertions();
    // do stuff...
}

另请参见。

在我的例子中,测试没有断言,因为我只是使用
prophecy
lib来模拟并检查方法是否按预期被调用

默认情况下,PHPUnit期望测试至少有一个断言,并警告您将其放入

只要在测试的任何部分添加这一行就足够了

$this->expectNotToPerformAssertions();
但是,您也可以设计一个在某些情况下有断言或没有断言的测试

public function testCodeNoAssertions()
{
    if ($something === true) {
        $this->expectNotToPerformAssertions();
    }
    // do stuff...

    if ($somethingElse === true) {
    //do any assertion
    }
}

总之,这不是一个bug。。。这是一项功能。

PHPUnit 7.2+$this->expectnottoperformanceassertions()可以使用,如果您有条件,可能会禁用断言。似乎不在官方文件中,但可以在这里找到:

至少PHPUnit 7.0+@doesnotperformanceassertions可以使用。当前文档未说明何时添加此文档

如果您的测试只是不完整,那么应该使用$this->markTestIncomplete()

如果要临时禁用测试,应使用$this->markTestSkipped()


我创建了一个超类,并在其中放置了类似SimpleTest的函数:

/**
 * Pass a test
 */
public function pass()
{
    //phpunit is missing pass()
    $this->assertTrue(true);
}
那你可以叫它

$this->pass();

我怎么能不及格——严格?我刚刚运行了phpunit,所以我猜它在配置xml中是否正确?是的,在
元素中使用
strict=“false”
,或者干脆忽略它,因为默认值应该是
false
。一种更干净的方法是将这一行放在测试的开头
$this->expecttoperformanceassertions()
方法
pass()
在PHPUnit>=4.4中不存在。不确定它以前是否存在。@awei我们使用的是PHP5,它确实存在,我们以前使用的是4.X,它也存在。周一将使用正确的版本号和插件版本进行更新。@awei是的,确实有效,在我的回答中添加了编辑。您使用的是哪个版本的PHPUnit?在版本“phpunit/phpunit”:“~4.4”中,这不起作用。这可以解释它。可以粘贴SimpleTest->passs()方法吗?我很想看看里面有什么。注意,它并没有为这种方法生成覆盖率
/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{

}
public function testCodeThatIsIncomplete()
{
    $this->markTestIncomplete('Implementation of this test is not complete.');
}
public function testCodeThatIsDisabledTemporarily()
{
    $this->markTestSkipped('This test is disabled to test, if it is interfering with other tests.');
}
/**
 * Pass a test
 */
public function pass()
{
    //phpunit is missing pass()
    $this->assertTrue(true);
}
$this->pass();