The process of fighting flakiness: detect, isolate, fix
Fighting flakiness is a process with four states and an owner for each test: detect, isolate, fix, return. A flaky test is taken off the blocking path into quarantine, but with an owner and a deadline, otherwise quarantine becomes a graveyard. After the fix a test returns to the gate only after many runs: ten clean runs at a 10% failure rate prove almost nothing.
System design · Flaky tests
Ten flaky tests in the gate, and the team already hits "Re-run". How to stop the decay?
One engineer fixing tests one at a time does not scale: new unstable tests appear faster than old ones get fixed. So you need a process with four states and an owner for each test: detect, isolate, fix, return. This lesson shows how to set it up without a hand-kept list of "who knows what" and where the process usually breaks.
How it works and why. The flaky problem is not technical but social: as soon as a red run stops being read, the gate loses its meaning. So the process solves two tasks. First: take unstable tests off the blocking path so red again means "something broke". Second: do not let them get lost, so that each one has an owner and a deadline. Quarantine without that only postpones the problem.
Life cycle of a flaky test
Detection. Playwright marks a test that failed and then passed on a retry as flaky in the report; these results must be stored between runs, otherwise you only see today's noise. A test with several such marks in a week is a quarantine candidate. In addition, a nightly job runs the suite with --repeat-each=5 and finds unstable tests before they land in other people's PRs. The --fail-on-flaky-tests flag makes a run red if even one test passed only on a retry: useful in the nightly run where a retry exists for diagnostics, not for a green colour.
Quarantine. The simplest way in Playwright is a tag that excludes the test from the blocking run and a separate non-blocking job that runs it. The test stays visible, it just does not stop the merge:
test(
'transfer shows the new balance',
{ tag: '@quarantine', annotation: { type: 'issue', description: 'QA-412' } },
async ({ page }) => {
// ...
},
);
# blocking gate: everything except quarantined tests
npx playwright test --grep-invert @quarantine
# non-blocking job: only quarantined tests, with diagnostics
npx playwright test --grep @quarantine --retries=0 --trace=on
An annotation with a ticket number appears in the report, so the owner and the deadline are visible right next to the test. The alternative test.fixme() turns the test off completely and it is easier to forget it: a tag with a separate job keeps the test in sight.
Analysis. The trace from the non-blocking job, the video, backend logs by correlation id. The goal is not to "fix" but to classify: test, data, environment or product. The class decides who fixes it: the test author, the data owner, the infrastructure team or development. Write the class into the ticket: after a month you can see where most instability comes from.
Return. After the fix the test is run many times and only then returned to the gate:
npx playwright test transfer.spec.ts --repeat-each=30 --retries=0
How many runs are enough depends on the failure rate. If the test failed one run in ten, ten clean runs prove nothing: the chance of passing all of them at a true rate of 10% is about 35%. So thirty or more are used, and for rare failures also a run in CI, where they happened.
Rules that hold the process together.
- A quarantine limit, for example two weeks. After that the test is fixed or deleted: a test nobody fixes is needed by nobody.
- Flaky rate as a team metric on a dashboard, next to pipeline time.
- A new test is run several times before merge (
--repeat-each) so that instability does not enter the gate. --only-changedand--last-failedspeed up checking a fix but do not replace a repeated run.
Limits and alternatives. A tag-based quarantine works only while somebody watches it: the limit and the owner are organisational things a tool cannot enforce. Some teams automatically move a test to quarantine after N unstable results and open a ticket; it is convenient but needs result history in storage (Allure TestOps, a custom database, CI reports). Another approach is to have no quarantine at all and block merges until the flaky test is fixed. It works in small teams with quick fixes and breaks in a large suite where one unstable test stops everyone.
Common mistakes.
- Quarantine without an owner and a deadline: after half a year it holds a hundred tests nobody opens.
- Returning a test to the gate after one green run: the instability simply had no time to show.
- Not storing result history: "flaky" becomes a feeling, not a number.
- Deleting an unstable test without analysis: if it caught a real race condition, the risk is left uncovered.
Check yourself
A test that failed in 10% of runs passed ten times in a row after the fix. Can it go back to the gate?
Answer
Not yet. If the true failure rate is 10%, the chance of ten clean runs is about 35%, so the result proves almost nothing. You need thirty or more runs with --repeat-each and no retries, ideally also a run in CI.
Why is a @quarantine tag with a separate job better than test.fixme()?
Answer
fixme disables the test entirely, so the instability is no longer observed and the test is forgotten. A tag takes the test off the blocking path but it still runs in a separate job: you see how it behaves and can collect traces for analysis.
--repeat-each, quarantine by tag with an owner and a deadline, analysis from traces and logs with a cause class, return after a repeated stable run. The key is that every unstable test has an owner and a deadline, and the flaky rate is visible as a metric.