Flaky tests in CI: retries, quarantine, re-runs
A flaky test passes and fails on the same code, and the policy has three tools. A retry detects, quarantine isolates, a re-run copes with infrastructure failures. CI allows one or two retries, and a test that passed only on a retry gets a ticket. An unstable test that blocks others goes into quarantine with an owner and a deadline, but is not deleted.
CI/CD · Pipeline
A test failed, we re-ran it, it passed. Was that "all good" or an incident we just hid?
A red PR. The author clicks Re-run, the test passes, merge. A week later the same test fails for another developer, then a third. Nobody knows whether it is a product bug or a test bug, and the team starts treating red as noise. A flaky test costs nothing while nobody notices it, and costs a lot once ignoring it becomes a habit. That is why a policy is needed, not "let's re-run once more".
How it works and why. A flaky test passes or fails on the same code. There are three tools and each has its own job: a retry detects, quarantine isolates, a re-run copes with infrastructure.
Life of a flaky test
Retries allow 1-2 repeat attempts in CI and none locally, so a developer sees instability at once. A test that passed only on a retry is marked flaky by Playwright: it lands in the report and in metrics. Collecting a trace on the first retry is convenient. Config options are covered in the lesson “Parallelism, workers and sharding”.
import { defineConfig } from '@playwright/test';
export default defineConfig({
// two retries in CI, none locally
retries: process.env.CI ? 2 : 0,
use: {
// a trace is collected only on the first retry
trace: 'on-first-retry',
},
});
Quarantine. An unstable test that blocks others is moved out of the required suite: a tag or a separate project that the nightly run starts on its own (test.skip and test.fixme do not fit: such a test does not run at all). It is not deleted: it has an owner, a cause and a deadline, and the nightly run shows whether it recovered. Quarantine without a deadline becomes a graveyard of tests.
// a tag in the title: the blocking suite skips it
test('card payment @quarantine', async ({ page }) => {
// TODO QA-412: race after the bank redirect, owner: Ira, due 15 Dec
});
// PR: npx playwright test --grep-invert @quarantine
// nightly: npx playwright test --grep @quarantine
Re-run. When infrastructure fails (a runner, the network, a rate limit), GitHub Actions has Re-run failed jobs: only the failed jobs restart, for example one shard, not the whole pipeline. The same from a console: gh run rerun --failed. It saves time but does not replace analysis: a re-run with no entry in metrics hides the problem.
Retries: two approaches
retries: 5 for everything
Real race bugs pass on the fifth attempt and turn green. The pipeline gets longer and nobody sees the instability.
1-2 retries + flaky tracking
Every flaky test goes into metrics and a ticket, and one above a threshold goes to quarantine.
Limits and alternatives. Retries do not help when tests depend on shared state: a retry inherits the dirty data. Causes of flakiness are found in the trace and in execution order, not in the number of attempts; a method is in the lesson “CI metrics and failure triage”. Dedicated services (flaky analytics over run history) help in large suites, but to start the Playwright report and tickets are enough.
Typical mistakes.
- A global
retries: 5: it hides real race bugs. - Automatic re-runs of red runs with no entry in metrics.
- A skip with no ticket link, owner or deadline.
- A quarantine nobody reads at night.
- Retries locally: a developer stops seeing instability.
Check yourself
A test passed only on the second attempt and the job is green. Can we stop there?
Answer
No. A green job only means the final result succeeded. A test that needed a retry is marked flaky and needs a ticket, a cause and an owner, otherwise instability accumulates.
How does quarantine differ from deleting a test?
Answer
In quarantine a test stays in the suite but does not block PRs: it has an owner, a cause and a deadline and runs at night. A deleted test stops protecting the feature and nobody sees the problem.