Member-only story

Set a max timeout for your PHPUnit tests

Smaine Milianni
1 min readAug 18, 2022

--

๐Ÿ“š 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

--

--

Smaine Milianni
Smaine Milianni

Written by Smaine Milianni

Engineering Manager- Technical Lead - certified Symfony 7,6,5,4 and certified AWS Solution Architect - Remote Worker

No responses yet