有没有办法关闭“失败/错误时停止”?对于 PHPUnit 中的特定测试?
我正在开发一个 API 文档系统,并且想要动态检查每个命令是否附加了文档。 最简单的方法是动态循环每个命令并检查现有文档是否与其匹配。
我的代码如下所示:
public function testMissingDocs()
{
foreach ($aCommands as $sKey => $aOptions)
{
$this->assertNotNull($oDocs->get($sKey));
}
}
问题在于 PHPUnit 的 StopOnFailure/Error 功能,它会在第一个断言失败后停止测试。我了解此功能的原因,并且我希望在大多数测试用例中保留它,但对于动态断言/测试,它使事情变得有点困难。
有没有办法在每次测试的基础上禁用它,以便我可以检查此测试中的每个命令?
I am developing an API documentation system, and want to dynamically check that each command has documentation attached.
The easiest way to do this is dynamically loop through each command and check for existing documentation to match it.
My code looks like this:
public function testMissingDocs()
{
foreach ($aCommands as $sKey => $aOptions)
{
$this->assertNotNull($oDocs->get($sKey));
}
}
The problem with this is the StopOnFailure/Error feature of PHPUnit which stops the test after the first assertion fails. I understand the reasons for this functionality and I want to keep it on for the majority of my test cases, but for dynamic assertions/tests it makes things a bit hard.
Is there a way to disable it on a per-test basis so I can check each command in this test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用数据提供程序 将单个测试拆分为与命令一样多的测试。
You can use a data provider to split the single test into as many tests as you have commands.
如果特定类或方法的文档丢失,则表示该类有问题,而不是检索文档的方法有问题。
尽管将所有文档合并到一个测试中可能更容易,但这并不遵循单元测试最佳实践(因此,PHPUnit 框架对您不利,而不是为您服务)。
我建议采用两种方法之一来纠正该问题:
If the documentation for a particular class or method is missing, that would represent a problem with that class, not with the method to retrieve the documentation.
Although it is probably easier to combine all of the documentation into a single test, that does not follow unit testing best practices (and hence is why the PHPUnit framework is working against you rather than for you).
I would suggest one of two approaches to rectify the issue: