Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit strict mode - how to change default timeout

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.

like image 922
Tomasz Tomczyk Avatar asked Dec 05 '12 11:12

Tomasz Tomczyk


1 Answers

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.

like image 119
Smar Avatar answered Oct 02 '22 01:10

Smar