You push a change, the pipeline goes red, and you rerun the same build without touching a line of code. This time it goes green. Nothing changed except the result, and that is the whole problem. So what is a flaky test? It is a test that passes and fails on the same code, with the outcome decided by timing, ordering, shared state, or the environment instead of by the code under test. Once developers stop believing red builds, real failures start slipping through.
This article covers the definition, the usual causes, and a repeatable method to prove flakiness first. The proof matters because a test that fails once is not automatically flaky. It could be a genuine bug that only shows under certain conditions, and treating it as flaky lets that bug ship.
What Is a Flaky Test?
A flaky test is a test with a non-deterministic outcome. Run it ten times against identical code and the results mix passes and failures. Nothing changed, but something underneath shifted: a clock ticked past a boundary, two tests shared a database row, a network call timed out, or the test runner executed tests in a different order.
The key word is non-deterministic. A test that fails every time is broken, and a broken test is easy to deal with because the failure is reproducible. A flaky test fails sometimes, which makes it harder to reproduce, harder to debug, and harder to convince anyone to fix. Teams often respond by rerunning the pipeline until it goes green, which hides the problem and trains everyone to ignore red builds.
Why Flaky Tests Cost More Than Ordinary Failures
A normal failing test costs the time it takes to fix the bug it caught. A flaky test costs that plus the time spent deciding whether the failure is real. Every red build forces someone to look, rerun, and judge, and across a team those interruptions add up fast.
The deeper cost is trust. When developers learn that red often means nothing, they stop investigating. That is when a genuine regression hides behind a familiar flaky failure and reaches production. If your team has reached the point where a failing build gets a shrug instead of a look, the test suite has a credibility problem, and flaky tests are usually the cause. The same applies when a bug only shows up in production and the local suite insists everything is fine: the tests are not wrong, but the conditions they run under do not match reality.
Common Flaky Test Causes
Most flaky tests trace back to a small set of causes. Check these first before assuming something exotic is going on.
- Time dependence. Tests that compare against the current date, use fixed sleeps to wait for async work, or assert on timestamps will eventually hit a boundary. A test that passes at 10:00 and fails at midnight has a time bug, not a code bug.
- Shared state. Tests that read and write the same database rows, files, or in-memory singletons interfere with each other. The result depends on which test ran first, so the failure moves around from run to run.
- Test order dependence. A test that passes on its own but fails when run after another test is usually leaking state. The earlier test leaves the world in a shape the later test did not expect.
- Async and concurrency. Tests that wait a fixed 500 milliseconds for a background job, or assert on the order of concurrent operations, race against real timing. A flaky test in CI often fails this way: it passes on a fast laptop and fails on a loaded runner.
- Network and external services. Tests that hit a real API, a third-party sandbox, or a service with rate limits inherit that service’s availability. A slow response becomes a test failure.
- Randomness and floating point. Unseeded random data, map iteration order, and floating point rounding can all change results between runs. The test looks deterministic until the unlucky input arrives.
How to Prove a Test Is Flaky
Proving flakiness means showing the same code produces different results. Change nothing about the code, and vary only the conditions around the test.
- Rerun the single test repeatedly. Run the suspect test alone, 20 to 50 times, with no code changes. If it fails even once, you have proof: the test is non-deterministic. Most test runners support a repeat flag, or you can loop the command in a shell script.
- Rerun the whole suite. Some tests only fail when other tests run alongside them. If the test passes alone but fails in the full suite, run the suite a few times and note whether the same test fails in different positions. That points at order dependence or shared state.
- Shuffle the test order. Most runners can randomize execution order or accept a seed. Run the suite with different seeds. If the test fails under some seeds and passes under others, order is part of the cause. A fixed seed that reproduces the failure turns a flaky test into a reproducible one.
- Run it in isolation and in parallel. Compare the test alone, in the full suite, and with parallelism on and off. If parallelism changes the result, the test shares something it should not: a port, a file, a database, or a global variable.
- Run it on a clean environment. Run the test on a fresh checkout, a clean container, or a different CI runner. If it passes locally and fails in CI, the environment is part of the story, and the usual suspects are time zones, available memory, CPU count, and network access. That pattern is common enough that it has its own debugging playbook for tests that pass locally and fail in CI.
- Read the failure, not just the verdict. Save the output of a failed run and compare it with a passing run. Flaky failures often leave a clear signature: a timeout message, an assertion on a timestamp, a unique-constraint error, or a port already in use.
Document what you ran and what you saw. A short note, “fails 3 times in 50 runs, only when run after the billing tests, seed 42 reproduces it,” turns a vague complaint into a fixable bug report.
How Many Reruns Are Enough?
There is no magic number, but there is a practical way to think about it. A test that fails roughly one time in ten will almost always fail inside 20 runs. A test that fails one time in a hundred may need a hundred runs to show itself, and at that point the reruns themselves start costing real CI time. Repeated reruns also slow the pipeline down, so if your builds are already slow, it helps to speed up slow CI builds before you add a hundred-run loop on top of them.
Use the failure rate to set the effort. If a test fails often, a short rerun loop gives you proof in minutes. If it fails rarely, do not burn a day rerunning it. Instead, quarantine it, log every failure with its seed and order, and let the evidence accumulate from normal CI runs. To detect flaky tests that fail rarely, that log matters more than any single rerun session: the pattern only becomes visible when you keep records.
What to Do After You Prove It
Once you have proof, the options are fix, quarantine, or delete, and the right one depends on what the test is worth.
Fix it when the cause is clear and the test guards real behavior. Replace fixed sleeps with proper waiting, give each test its own data, seed the random generator, and mock the network. Most flaky test causes have a boring, mechanical fix, and the test becomes stable the moment the hidden dependency is removed.
Quarantine it when the cause is unclear or the fix is large. Move the test out of the main suite into a separate job that runs on a schedule, and file a ticket with the evidence you collected. Quarantine is a pause, not a pardon: the test still runs, still reports, and still has an owner and a deadline.
Delete it when the test covers nothing the rest of the suite does not already cover, or when it tests an implementation detail instead of behavior. Deleting a test feels wrong, but a test nobody trusts provides no coverage in practice. If you delete it, write down why, so nobody resurrects it in six months. Teams that write tests that catch bugs instead of chasing coverage find this decision easier, because every test has a stated reason to exist.
Whatever you choose, do it promptly. A proven flaky test left in the main suite keeps taxing every developer who sees a red build. The proof you collected turns “this test is annoying” into “this test failed 3 times in 50 runs on unchanged code, here is the seed,” and that is a case any team can act on.