沃梦达 / 编程问答 / php问题 / 正文

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

PHPUnit strict mode - how to change default timeout(PHPUnit 严格模式 - 如何更改默认超时)

本文介绍了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.

这篇关于PHPUnit 严格模式 - 如何更改默认超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHPUnit 严格模式 - 如何更改默认超时

基础教程推荐