A flaky test gives different results on the same code. Re-running hides the problem and teaches the team to ignore red builds.
Common causes
- Timing: a fixed sleep, a race, a timeout that is too tight.
- Shared state: tests that depend on order or on data left by another test.
- Time and randomness: dates, time zones, random seeds.
- External services: network calls to something that is sometimes slow.
- Resources: ports, files and databases used by parallel tests.
Routine
- Reproduce: run the test many times in a loop, alone and with the whole suite.
- Classify the cause using the list above.
- Fix the cause: wait on a condition instead of a sleep, isolate state, freeze the clock, stub the service.
- If a fix is not possible today, quarantine the test with a ticket and a date, so it is not forgotten.
- Write the cause as a finding, so a similar flake is recognised faster next time.
With an agent
An agent can run the loop and propose a cause. Check that it fixed the cause and did not add a retry or weaken the assertion. Deleting or skipping the test is not a fix.