如何表明PHPUnit测试预计会失败?

mjs*_*mjs 18 php tdd phpunit unit-testing

是否可以使用PHPUnit将测试标记为"预期失败"?这在执行TDD时很有用,并且您希望区分真正失败的测试和由于尚未编写相关代码而导致失败的测试.

Rya*_*ard 25

我认为在这些情况下,简单地将测试标记为跳过是相当标准的.您的测试仍将运行且套件将通过,但测试运行器将提醒您跳过的测试.

http://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html


Tom*_*m B 12

处理此问题的"正确"方法是使用$this->markTestIncomplete().这将标记测试不完整.它会在返回时返回,但会显示提供的消息.有关更多信息,请参见http://www.phpunit.de/manual/3.0/en/incomplete-and-skipped-tests.html.

  • 问题是关于TDD,你可以在主代码之前编写(理想的完整)测试.但是,`markTestIncomplete`适用于"未实现的测试"(http://phpunit.de/manual/3.7/en/incomplete-and-skipped-tests.html首先描述未实现的测试的空测试方法,然后解释这是如何导致错误的成功). (2认同)

小智 9

我认为这是一个不好的做法,但你可以用这种方式欺骗PHPUnit:

/**
 * This test will succeed !!!
 * @expectedException PHPUnit_Framework_ExpectationFailedException
 */
public function testSucceed()
{
    $this->assertTrue(false);
}
Run Code Online (Sandbox Code Playgroud)

更干净:

  public function testFailingTest() {  
    try {  
      $this->assertTrue(false);  
    } catch (PHPUnit_Framework_ExpectationFailedException $ex) {  
      // As expected the assertion failed, silently return  
      return;  
    }  
    // The assertion did not fail, make the test fail  
    $this->fail('This test did not fail as expected');  
  }
Run Code Online (Sandbox Code Playgroud)