How to Triage and Quarantine Flaky Tests
Find which tests fail without a code change, quarantine them so the suite stays trustworthy, and fix the root cause instead of retrying.
On this page
Introduction
A test that fails intermittently is worse than a test that fails always. A consistent failure tells you something is wrong; a flaky failure teaches the team to rerun until green, which means real failures get rerun away too. Flakiness corrodes the value of the entire suite: once people believe a red build might be noise, they stop acting on red. The response is to find flaky tests systematically, distinguish them from genuinely failing tests, quarantine the flaky ones so they stop blocking, and fix the cause rather than papering over it with retries. This article covers detection, quarantine policy, and the common root causes worth fixing.
Why Flaky Tests Are Expensive
Each flaky failure wastes the time of whoever investigates it, and worse, it trains a habit of distrust. Teams that rerun instead of read have effectively disabled their test suite while believing it is running. The cost compounds because flaky tests cluster in the areas with real concurrency or timing bugs, so the tests that would catch the hardest bugs are the ones most likely to be dismissed.
Flakiness is not random; it is a symptom of a specific kind of defect: shared state between tests, dependence on wall-clock time or ordering, real network calls, or a genuine race in the code under test. Each has a fix. Treating flakiness as unavoidable means never finding those fixes, and often means shipping the race to production.
Detecting Flakiness Systematically
You cannot fix what you cannot name, so measure it. Run the test suite repeatedly against the same commit and record which tests pass and fail across runs; a test that is sometimes red on unchanged code is flaky by definition. Do this on a schedule, not only when someone complains, because complaints surface only the flakiness that has already irritated someone.
# Run the same suite N times on the same commit and collect failures
for i in $(seq 1 20); do
npx playwright test --reporter=json > "runs/run-$i.json" 2>/dev/null || true
done
# List tests that failed in at least one run (candidates for quarantine)
jq -r '.suites[].specs[] | select(.tests[].results[]?.status=="failed") | .title' runs/*.json \
| sort | uniq -c | sort -rn
Quarantine, Do Not Delete
Once a test is identified as flaky, quarantine it: move it to a separate suite that runs on a schedule but does not block the main pipeline. Quarantine keeps the test running so you can still gather data and know when it is fixed, while removing the false red from the critical path. Deleting it loses the coverage permanently and removes the reminder to fix it.
Record the quarantine with an owner and a date. A quarantine without an owner becomes permanent, and the coverage silently disappears. Treat every quarantined test as a debt with an owner and a deadline, and report the count so it cannot grow unnoticed.
Retries Are a Signal, Not a Fix
Automatic retries make a pipeline green again but convert a flaky test into a silent one: the failure still happens, you just do not see it. Retries are acceptable as a temporary measure to keep the pipeline usable while you investigate, but they must be visible. Count retried passes and alert when the count rises, because that is the real measure of flakiness.
Reserve retries for infrastructure that is genuinely outside your control, such as a remote service that occasionally drops a connection, and for those cases isolate the retry to that call rather than to the whole test. A blanket retry of every failing test hides exactly the deterministic bugs you most want to see.
Common Root Causes and Fixes
Shared state is the most common cause: a test creates a record with a fixed id and another test assumes it is absent, so order decides the outcome. Fix it by giving each test its own data and cleaning up, or by making the assertion independent of order. Deterministic data beats clever cleanup.
Time dependence is next: a test that assumes a duration is a few milliseconds, or that formats a date in the local timezone, breaks under load or in another region. Inject a controllable clock and compare against a fixed instant. Real network calls are a third: a test that calls a live service fails when the service is slow. Replace it with a stub or a recorded response. The fourth is a genuine race: if the code has a race, the test should find it, and the fix is in the code, not the test. Rewriting the test to hide the race is how a production incident is born.
A Deterministic Test Skeleton
The fixes share a shape: replace every source of variability with an injected, controlled one. Time comes from a clock the test owns, identifiers come from the test, and the network is replaced by a stub. The test below isolates each of those so that the only thing that can make it fail is the code under test, which is exactly what a test should be able to say.
import { describe, it, expect, beforeEach } from 'vitest';
// Injected clock and id source make the test deterministic.
function createClock(start = new Date('2026-01-01T00:00:00Z')) {
let current = start;
return { now: () => current, advance: (ms) => { current = new Date(current.getTime() + ms); } };
}
describe('createOrder', () => {
let clock, deps;
beforeEach(() => {
clock = createClock();
deps = {
now: clock.now,
newId: () => 'id-1', // no randomness
charge: async () => ({ ok: true }), // no live network
};
});
it('stamps the order with the current time', async () => {
clock.advance(5000);
const order = await createOrder({ total: 100 }, deps);
expect(order.createdAt).toBe('2026-01-01T00:00:05.000Z');
expect(order.id).toBe('id-1');
});
});
A Policy That Keeps the Suite Trustworthy
State the rule: a red pipeline is a stop-the-line event, not a suggestion. A test that fails without a code change is quarantined within a day, with an owner. Quarantined tests are reviewed at a fixed cadence and either fixed or deleted with a decision recorded. Retried passes are counted and reported, so flakiness cannot hide behind retries.
Make the policy measurable by tracking the flaky rate over time. A suite whose flaky rate falls is one people trust, and a trusted suite is one they act on. The goal is not zero flakiness forever, which is unrealistic, but a rate low enough that red reliably means a real problem.
Conclusion
Flaky tests are a measurement problem and a trust problem at once. Detect them by running the suite repeatedly on unchanged code, quarantine the offenders with an owner and a deadline so they stop blocking without losing coverage, and treat retries as a visible signal rather than a fix. Then attack the root causes: shared state, wall-clock dependence, live network calls, and genuine races. Fixing the race fixes production; rewriting the test to hide it ships the race. Keep the flaky rate low so a red build means something, and the suite stays worth reading.
Sources: Google Testing Blog: flaky tests, pytest: flaky plugin, Playwright: test retries.
FAQ
- Is retrying failed tests an acceptable way to handle flakiness?
- Only as a visible, temporary measure, and only for genuinely external infrastructure. A blanket retry hides deterministic bugs and turns flaky failures into silent ones. Count retried passes so flakiness cannot disappear behind them.
- Should I delete a flaky test or quarantine it?
- Quarantine it. Moving it to a non-blocking scheduled suite keeps the coverage and the data you need to fix it, while removing the false red from the pipeline. Deleting loses the coverage permanently and removes the reminder.
Report a problem with this articleOpens a pre-filled issue on GitHub.