Flaky tests: how do you track down one that only fails occasionally?
We have a test that passes almost always, but fails now and then in CI and never on anyone's laptop. Everyone just hits "re-run," which I know is a bad habit.
What I've tried:
- Running it in a loop locally to try to reproduce it
- Checking for shared state between tests
- Looking for anything time-based or dependent on test order
The usual suspects for me have been timing, test order and leftover data. What's the strangest root cause you've found for a flaky test?
Comments · 4
Log everything on failures: seed, timing, environment. Then run the test in a loop locally with the same constraints as CI, like limited CPU, and see if it fails. Timing and shared state are the usual culprits.
Check for order dependence. Run the test alone, then with the rest of the suite, and see if it behaves differently.
Order dependence was our culprit. One test left a record in a shared database that another test counted on. Running the test in isolation exposed it right away.
Quarantine flaky tests on a list so they don't block releases, but keep a deadline for fixing them. "Just re-run" slowly destroys trust in CI.