有没有办法关闭“失败/错误时停止”?对于 PHPUnit 中的特定测试?

发布于 2024-11-17 09:59:16 字数 431 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(2

泛泛之交 2024-11-24 09:59:16

您可以使用数据提供程序 将单个测试拆分为与命令一样多的测试。

/**
 * @dataProvider getDocsForAllCommands
 */
public function testEveryCommandHasDocs($sKey)
{
    $this->assertNotNull($oDocs->get($sKey));
}

public function getKeysForAllCommands()
{
    return array_keys($aCommands);
}

You can use a data provider to split the single test into as many tests as you have commands.

/**
 * @dataProvider getDocsForAllCommands
 */
public function testEveryCommandHasDocs($sKey)
{
    $this->assertNotNull($oDocs->get($sKey));
}

public function getKeysForAllCommands()
{
    return array_keys($aCommands);
}
勿挽旧人 2024-11-24 09:59:16

如果特定类或方法的文档丢失,则表示该类有问题,而不是检索文档的方法有问题。

尽管将所有文档合并到一个测试中可能更容易,但这并不遵循单元测试最佳实践(因此,PHPUnit 框架对您不利,而不是为您服务)。

我建议采用两种方法之一来纠正该问题:

  1. 重构您的测试,以便每个类和/或方法都有自己的文档检查(有几种方法可以在一次执行中运行多个测试)。
  2. 请使用诸如 PHP_CodeSniffer 之类的工具,因为缺乏文档可以被视为一种约定 - 而不是而不是功能性缺陷。

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:

  1. Refactor your tests so that each class and/or method has its own documentation check (there are a few ways you can run multiple tests in one execution).
  2. Use a tool such as PHP_CodeSniffer instead, as lack of documentation could be considered a convention – rather than a functional – defect.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文