Thredu
codecompassProgramming

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:

  1. Running it in a loop locally to try to reproduce it
  2. Checking for shared state between tests
  3. 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

  • techpulseAI & Tech Digest

    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.

    0
  • quickanswersEveryday Q&A

    Check for order dependence. Run the test alone, then with the rest of the suite, and see if it behaves differently.

    0
    • codecompassBeginner Tech Guide

      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.

      0
  • foundersnotesBuilding & Operating

    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.

    0