PHPUnit 严格模式 - 如何更改默认超时
我想继续在严格模式下运行我的单元测试,这样我就可以轻松地发现任何异常长的测试,但同时默认的 1 秒超时是不够的.我可以为所有测试更改它吗?我知道我可以使用 @short/@medium/@long
注释为每个类(和单个测试)设置超时,但是所有测试都有类似的东西吗?也许在 phpunit.xml 中?
I'd like to keep running my unit tests in strict mode so that I'm aware of any exceptionally long tests easily, but at the same time the default timeout of 1s is not enough. Can I change it for all tests? I know I can set timeout for each class (and individual tests) using @short / @medium / @long
annotations, but is there something like that for all tests? Perhaps in phpunit.xml?
这是为了避免 PHP_Invoker_TimeoutException: Execution aborted after 1 seconds
偶尔发生.
This is to avoid PHP_Invoker_TimeoutException: Execution aborted after 1 second
that happens once in a while.
推荐答案
可以通过在 phpunit.xml 中设置想要的时间来启用该选项.时间以秒为单位.
The option can be enabled by setting wanted times in phpunit.xml. The times are in seconds.
例子:
<phpunit
strict="true"
timeoutForSmallTests="1"
timeoutForMediumTests="5"
timeoutForLargeTests="10"
>
// test suites
</phpunit>
可以通过如下标记实际测试功能来将测试标记为中型或大型
Tests can be marked to be medium or large by marking actual tests functions like following
/**
* @medium
*/
public function testTestThing() {
$this->assertTrue(false);
}
现代 PHPUnit 版本 不再执行这些超时,并且通常通过为每个事物引入单独的标志来改变严格模式的行为 之前被严格模式覆盖:
modern PHPUnit versions does not do these timeouts any more, and also changes the behaviour of strict mode generally by introducing separate flags for each thing previously covered by strict mode:
beStrictAboutTestsThatDoNotTestAnything="true"
checkForUnintentionallyCoveredCode="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestSize="true"
beStrictAboutChangesToGlobalState="true"
不相关的警告:它还将 XML 配置中的测试路径更改为相对于 XML 配置文件,而不是旧的默认路径相对于当前工作目录.
Unrelated warning: it also changes paths to tests in the XML config to be relative to the XML config file, as opposed to old default that paths are to be relative to current working dir.
相关文章