【问题标题】:PHPUnit strict mode - how to change default timeoutPHPUnit 严格模式 - 如何更改默认超时
【发布时间】:2012-12-05 11:21:03
【问题描述】:

我想继续在严格模式下运行我的单元测试,这样我就可以轻松地发现任何异常长的测试,但同时默认的 1 秒超时是不够的。我可以为所有测试更改它吗?我知道我可以使用@short / @medium / @long 注释为每个类(和单个测试)设置超时,但是对于所有测试都有类似的东西吗?也许在 phpunit.xml 中?

这是为了避免PHP_Invoker_TimeoutException: Execution aborted after 1 second 偶尔发生。

【问题讨论】:

  • 我已经看过了,但它没有回答如何全局更改此设置 - 无需编辑 PHPUnit 源代码,我想避免这种情况。
  • 你只能从 phpunit.xml 来做。据我所知,PHPUnit 没有全局配置文件。
  • 那么如何在保持严格模式的同时更改默认超时?
  • 从接受的答案中,将 timeoutForSmallTests 设置为您想要的值。它转到其他配置值(例如:P 的严格模式)所在的标题。

标签: php timeout phpunit


【解决方案1】:

可以通过在 phpunit.xml 中设置想要的时间来启用该选项。时间以秒为单位。

例子:

<phpunit
    strict="true"
    timeoutForSmallTests="1"
    timeoutForMediumTests="5"
    timeoutForLargeTests="10"
>
 // test suites
</phpunit>

可以通过如下标记实际测试功能将测试标记为中型或大型

/**
 * @medium
 */
public function testTestThing() {
     $this->assertTrue(false);
}

编辑:modern PHPUnit versions 不再执行这些超时,并且通常通过为每个事物引入单独的标志 previously covered by strict mode 来改变严格模式的行为:

beStrictAboutTestsThatDoNotTestAnything="true"
checkForUnintentionallyCoveredCode="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true"
beStrictAboutChangesToGlobalState="true"

不相关的警告:它还将 XML 配置中的测试路径更改为相对于 XML 配置文件,而不是旧的默认路径相对于当前工作目录。

【讨论】:

    【解决方案2】:

    或者,您也可以在 setUp() 方法中设置它们,如下所示:

    $this->getTestResultObject()->setTimeoutForSmallTests(1);
    $this->getTestResultObject()->setTimeoutForMediumTests(5);
    $this->getTestResultObject()->setTimeoutForLargeTests(10);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2011-01-15
      • 1970-01-01
      • 2011-12-05
      • 2015-08-15
      • 2018-12-02
      相关资源
      最近更新 更多