如何指示 PHPUnit 测试预计会失败?
是否可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为在这些情况下,简单地将测试标记为跳过是相当标准的。您的测试仍将运行并且套件将通过,但测试运行程序将提醒您跳过的测试。
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
处理此问题的“正确”方法是使用
$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.我真的认为这是一个不好的做法,但是你可以这样欺骗 PHPUnit:
更干净:
I really think it's a bad practice, however you can trick PHPUnit this way:
More cleanly:
上面六十九的评论几乎完美地满足了我正在寻找的内容。
当您为预期异常设置测试并且它没有触发您希望测试失败的异常时,fail() 方法非常有用。
当然,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.
Of course the triggerException is replaced by something in your object.
如果您想让测试失败,但知道其失败是预期的,您可以 向断言添加一条消息,该消息将在结果中输出:
在结果中:
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:
In the results:
在 PHPUnit 8.2.5 中,您可以简单地预期抛出的断言异常:
In PHPUnit 8.2.5 you can simply expect the thrown assertion exception: