PHPUnit strict mode - how to change default timeout

时光总嘲笑我的痴心妄想 提交于 2020-01-02 01:47:08

问题


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?

This is to avoid PHP_Invoker_TimeoutException: Execution aborted after 1 second that happens once in a while.


回答1:


The option can be enabled by setting wanted times in phpunit.xml. The times are in seconds.

Example:

<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);
}

EDIT: 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"

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.




回答2:


Alternatively you can set them also in your setUp() method like this:

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


来源:https://stackoverflow.com/questions/13722125/phpunit-strict-mode-how-to-change-default-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!