How do you know you've fixed a flaky test?

May 8, 2025

Key takeaway: Count a flaky test as fixed only when the reproduction loop that failed before now passes hundreds of runs.

You don’t know you’ve fixed a flaky test until you can make it fail on demand, name the cause, and show it stopped failing.

One green build doesn’t prove anything. If a test fails 2% of the time, there’s an 82% chance it passes ten runs in a row with no fix at all. The rule of three helps: zero failures in n runs puts the true failure rate below about 3/n, with 95% confidence. Zero in 300 puts it under 1%.

So reproduce it first. Run the test alone, many times:

go test -run TestCheckout -count=500 -race ./checkout/

If it never fails alone, something another test does is leaking in. Run the suite in random order and keep the seed. rspec --bisect narrows an order-dependent failure to the smallest set of examples that reproduces it. Then make conditions worse with parallel workers, a loaded machine, or the clock frozen at 11:59 PM on the last day of the month. You want a 1-in-500 failure to become 1-in-5.

Then name the cause in one sentence for the commit message, like “The test assumed users came back sorted by id, and the query never asked for that.” Most flakes are shared state, ordering assumptions, time, async work, or the network. A fix you can’t explain is a guess.

Sleeps and retries don’t count. A sleep(2) changes the odds and leaves the race. A retry plugin turns a 5% failure into 0.25%, the build goes green, and you’ve thrown away what may be the only signal of a real race in production code.

Run the same loop after the fix. Fourteen failures in 500 before and zero in 500 after is evidence, so put both numbers in the pull request. Then track per-test flake rates over weeks. If the test starts flaking again, the first fix was a guess.