Image from WikimediaImages on Pixabay

Flaky tests can give you a lot of trouble. They may pass today but fail tomorrow, randomly blocking your CI pipeline from successful execution, your merge requests from merging, and causing general aggravation.

Finding the reason for flakiness is never simple, it’s hard to estimate how long it will take, or how deep down the rabbit hole of race conditions and concurrency issues you’ll have to go.

Oftentimes you may see the CI pipeline configured to retry the failing tests multiple times and report success once they pass at least once. This is a terrible practice and should be avoided, and if it’s already established, you should gradually drag yourself out of it.

Why is it a bad idea to retry flaky tests?

There are some obvious performance issues related to flaky tests:

  • they waste a ton of CI resources, especially the UI-based tests that tend to be slow and heavy;
  • being a part of a merge pipeline, they break your merge requests, and if you retry, it slows down the merge pipeline significantly.

But that’s not even the main reason to fix the flaky tests as soon as possible. The deadliest problem is, test retries often hide real issues in the code. The bugs are sometimes flaky as well, e.g. due to race conditions or concurrency issues.

Test flakiness is an error, regardless of whether it’s an error in the tests or the production code. So you should treat it as one:

  • If you don’t know the reason for the test flakiness, it might as well be an issue with the application code, so it needs to be fixed.
  • If you know the reason for the test flakiness, why not just fix it?
Image from Dr Carl Russell at Pixabay

But what if I already have a thousand flaky tests with retries in my pipeline?

Sometimes retrying tests is part of an established practice. You simply find yourself in the middle of a green-red-green CI pipeline with a hundred randomly failing tests. Merge requests are being pushed through acceptance by dozens of retries a day, hopefully automated. How to pull yourself out of this?

The first thing you’ll want to do is to accept that there is a problem. Does everyone in your team agree that something needs to be done? Good. Otherwise, it may take some time and perseverance to reach a common agreement.

Second, establish a time budget for fixing the flaky tests. Improving the tests may take time. Regularly dedicate a specific amount of time or, if you’re in a team, select a teammate that will take care of test stability regularly.

Finally, for the technical part. Create a separate stage in your CI execution, and make sure it doesn’t have any retries, let’s call it the “Stable Stage”. Extract all stable tests to this stage. Whenever a test in the “Stable Stage” becomes flaky, treat it as a critical bug and fix it with high priority.

All flaky tests go to the “Flaky Stage”, this one should have retries. Ideally, it should only retry the tests that failed on the previous iteration. Now, start fixing all other tests one by one, gradually moving them from the Stable Stage to the Flaky Stage.

Finally, once all tests are in the Stable Stage, remove the Flaky Stage. All of your tests are stable now, but they still may become flaky later. Treat any flaky test as a critical bug and keep fixing the new flaky tests with high priority.

Image from Jeon Sang-O at Pixabay

Conclusion

Hopefully, this article gets you on the right path to get rid of flaky tests and prevent them in the future. Let me know in the comments if flaky tests are a problem for you, and how you manage to cope with them.