A flaky test passes and fails on the same code. One or two are an annoyance. Twenty, and the team starts re-running pipelines until they go green, and then stops trusting red builds at all, which is when real bugs get through. Fixing flakiness is less about clever tricks and more about a steady process.
1. Measure before you fix
You cannot fix what you only know by feel. Store the result of every test in every pipeline run (JUnit XML is enough) and compute, per test:
- how often it failed and then passed on retry, on the same commit;
- how often it failed on the main branch, where code was already reviewed and merged;
- the pipeline time it costs in retries.
A test that fails and passes on the same commit is flaky by definition. Rank by impact; usually a handful of tests cause most of the pain. Reporting tools such as Allure and ReportPortal show this history, or a small script over stored results does the job.
2. Quarantine, with rules
A known-flaky test should stop blocking merges, but it should not disappear. A workable quarantine:
- moves the test to a separate, non-blocking job that still runs and still reports;
- creates a ticket with an owner automatically;
- has a time limit: fixed or deleted within, say, two weeks;
- caps the size; if quarantine keeps growing, flakiness is now the team's priority.
Automatic retries are fine as a short-term measure, if every retry is recorded. A retry that hides a failure without counting it makes the problem invisible.
3. Fix the usual causes
Most flaky tests come from a short list of causes:
- Timing: fixed sleeps, or acting before the page or service is ready. Use the framework's auto-waiting and wait for a specific condition (a response, an element state), never a number of seconds.
- Shared data: tests that use the same user or record and interfere when run in parallel. Give each test its own data, created through the API in setup.
- Order dependence: a test that only passes after another one ran. Run tests in random order to expose these.
- Environment: an unstable shared staging server, slow third-party sandboxes, rate limits. Ephemeral environments per run and mocked third parties remove a whole class of failures.
- Time and randomness: dates, time zones, random data without a seed, animations. Freeze the clock and seed generators in tests.
- Real bugs: race conditions in the application itself. Some "flaky tests" are the only thing reporting a real concurrency bug. Check before you delete.
4. Use AI to speed up triage
The slowest part of handling flaky failures is reading logs. This is where AI earns its place in a pipeline: grouping failures that share a cause, separating likely-flaky from likely-real based on history and error signature, and posting a short explanation with the relevant log lines on the pull request. The decision stays deterministic, a test still passes or fails, but the time to understand a red build drops a lot.
5. Keep new flaky tests out
The cheapest flaky test to fix is the one that never merges. Run new and changed tests many times in the pull request before they land:
# Playwright: run only tests changed in this PR, 10 times each
npx playwright test --only-changed=origin/main --repeat-each=10
# Pytest equivalent, with the pytest-repeat plugin
pytest $(git diff --name-only origin/main -- 'tests/*.py') --count=10Combine that with code review rules (no sleeps, no shared fixtures that write data) and the quarantine stays small.
What good looks like
A healthy suite has a flake rate on main well under one percent, a quarantine you can count on one hand, and a team that treats a red build as information. Getting there usually takes weeks of focused work, not months, because the worst offenders are few.
Flaky test detection and AI failure triage are part of our QA AI pipeline work. If the flaky tests are AI-generated, see AI-generated Playwright tests: where they help and where they don't.