如何指示 PHPUnit 测试预计会失败?

发布于 2024-09-27 19:30:34 字数 84 浏览 3 评论 0原文

是否可以使用 PHPUnit 将测试标记为“预期失败”?这在执行 TDD 时非常有用,并且您想要区分真正失败的测试和由于尚未编写相关代码而碰巧失败的测试。

Is it possible to mark a test as "expected to fail" with PHPUnit? This would be useful when performing TDD, and you want to distinguish between genuinely failed tests, and tests that happen to fail because the associated code hasn't been written yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉风有信 2024-10-04 19:30:34

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

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

I think in these cases, it's fairly standard to simply mark the test as skipped. Your tests will still run and the suite will pass, but the test runner will alert you of the skipped tests.

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

失眠症患者 2024-10-04 19:30:34

处理此问题的“正确”方法是使用 $this->markTestIncomplete()。这会将测试标记为不完整。它将按已通过的方式返回,但会显示提供的消息。请参阅http://www.phpunit.de/manual/ 3.0/en/incomplete-and-skipped-tests.html 了解更多信息。

The 'correct' method of handling this is to use $this->markTestIncomplete(). This will mark the test as incomplete. It will come back as passed, but it will display the message provided. See http://www.phpunit.de/manual/3.0/en/incomplete-and-skipped-tests.html for more information.

檐上三寸雪 2024-10-04 19:30:34

我真的认为这是一个不好的做法,但是你可以这样欺骗 PHPUnit:

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

更干净:

  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');  
  }

I really think it's a bad practice, however you can trick PHPUnit this way:

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

More cleanly:

  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');  
  }
七秒鱼° 2024-10-04 19:30:34

上面六十九的评论几乎完美地满足了我正在寻找的内容。

当您为预期异常设置测试并且它没有触发您希望测试失败的异常时,fail() 方法非常有用。

$this->object->triggerException();
$this->fail('The above statement was expected to trigger and exception.');

当然,triggerException 会被对象中的某些内容替换。

The comment by sixty-nine above is nearly perfect for what I was searching for.

The fail() method is useful for when you set a test for an expected exception and if it did not trigger the exception you want the test to fail.

$this->object->triggerException();
$this->fail('The above statement was expected to trigger and exception.');

Of course the triggerException is replaced by something in your object.

怪我入戏太深 2024-10-04 19:30:34

如果您想让测试失败,但知道其失败是预期的,您可以 向断言添加一条消息,该消息将在结果中输出:

public function testExpectedToFail()
{    
    $this->assertTrue(FALSE, 'I knew this would happen!');
}

在结果中:

There was 1 failure:

1) testExpectedToFail(ClassTest)
I knew this would happen!

If you want to have a test fail but know that its failure was expected, you can add a message to the assertion that will output in the results:

public function testExpectedToFail()
{    
    $this->assertTrue(FALSE, 'I knew this would happen!');
}

In the results:

There was 1 failure:

1) testExpectedToFail(ClassTest)
I knew this would happen!
梦言归人 2024-10-04 19:30:34

在 PHPUnit 8.2.5 中,您可以简单地预期抛出的断言异常:

$this->expectException('PHPUnit\Framework\ExpectationFailedException');
$this->assertTrue(false);

In PHPUnit 8.2.5 you can simply expect the thrown assertion exception:

$this->expectException('PHPUnit\Framework\ExpectationFailedException');
$this->assertTrue(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文