9

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

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

4

2 回答 2

26

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

例子:

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

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

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

编辑:现代 PHPUnit 版本不再执行这些超时,并且通常通过为之前由严格模式覆盖的每件事引入单独的标志来改变严格模式的行为:

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

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

于 2012-12-05T11:43:42.377 回答
5

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

$this->getTestResultObject()->setTimeoutForSmallTests(1);
$this->getTestResultObject()->setTimeoutForMediumTests(5);
$this->getTestResultObject()->setTimeoutForLargeTests(10);
于 2013-02-01T08:00:17.800 回答