Flaky Tests

Flaky Tests
Photo by Aaron Burden / Unsplash

If you're writing tests for your application, you've probably came across tests that both pass and fail. These are known as flaky tests and can be quite hard to track down, especially if they only fail 1 in 100 or 1 in 1000 times.

Flaky tests can definitely be a nightmare, especially if you're running your test suite as part of a CI/CD pipeline and the whole pipeline stops because of a test that was passing for you locally and will most likely pass if you re-run your pipeline.

I have a tip to help you deal with flaky tests, if you're using PestPHP, I highly recommend using the repeat() option on a test that you suspect is flaky. This has been an absolute life saver for me. I usually do repeat(100). This way you can see just how flaky a test is and then begin the steps of resolving it.

Don't forget to remove repeat() once you're done with it! I think it would be a good idea for PestPHP to offer automatic re-runs of failed tests!