Member-only story
Set a max timeout for your PHPUnit tests
๐ You can set your tests as risky if they reach a timeout
by adding an annotation on top of the method. (@large, @medium or @small)
Example:
๐ก But what about having the possibility to set a global timeout and allow to customize it for each test?
๐ธ 1- Create the timeout configuration
Iโll create a class with a static public property as itโs straightforward for configuration.
โ 2- Create the Assertion
PHPUnit allows us to hook the end of each test and recover the execution time, we will add our assertion in this hook ๐
๐ฅ 3-Register our extension
๐ฟ4- Use it
๐ Voilaaaa!
Testing
The test "App\Tests\DemoTimeoutTest::testItsTrue" was too long, it tooks "2.005027" seconds and the maximum allowed is "1"
Failed asserting that 2.005026703 is less than 1.
Even my test is true AssertTrue(true)
the test fails as itโs too long ๐
Thanks for reading, donโt forget to clap it ๐and reach me on twitter