PHPUnit严格模式 - 如何更改默认超时

Tom*_*zyk 9 php phpunit timeout

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

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

Sma*_*mar 26

可以通过在phpunit.xml中设置所需时间来启用该选项.时间以秒为单位.

例:

<phpunit
    strict="true"
    timeoutForSmallTests="1"
    timeoutForMediumTests="5"
    timeoutForLargeTests="10"
>
 // test suites
</phpunit>
Run Code Online (Sandbox Code Playgroud)

通过标记实际测试功能,可以将测试标记为中等或大

/**
 * @medium
 */
public function testTestThing() {
     $this->assertTrue(false);
}
Run Code Online (Sandbox Code Playgroud)

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

beStrictAboutTestsThatDoNotTestAnything="true"
checkForUnintentionallyCoveredCode="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true"
beStrictAboutChangesToGlobalState="true"
Run Code Online (Sandbox Code Playgroud)

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


Ric*_*tag 5

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

$this->getTestResultObject()->setTimeoutForSmallTests(1);
$this->getTestResultObject()->setTimeoutForMediumTests(5);
$this->getTestResultObject()->setTimeoutForLargeTests(10);
Run Code Online (Sandbox Code Playgroud)