PHPUnit strict mode - how to change default timeou

2019-04-21 19:58发布

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.

2条回答
Emotional °昔
2楼-- · 2019-04-21 20:44

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

$this->getTestResultObject()->setTimeoutForSmallTests(1);
$this->getTestResultObject()->setTimeoutForMediumTests(5);
$this->getTestResultObject()->setTimeoutForLargeTests(10);
查看更多
爷的心禁止访问
3楼-- · 2019-04-21 20:54

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.

查看更多
登录 后发表回答