Flakiness: diagnosis & cure
The root causes of non-deterministic tests, how to diagnose each, and how to run a quarantine.
The root causes of flaky tests
Timing, interdependence, shared state, environment, data drift, real bugs and infrastructure — diagnose before acting.
A flaky test passes and fails without the code changing, and flakiness rather than slowness is what kills automation programmes, because it destroys trust and an untrusted suite gets ignored and then deleted. Nearly every flake belongs to one of seven categories. Timing and race conditions are the largest group by far and come from waiting for a duration instead of a state. Test interdependence shows up as passing alone and failing in a suite. Shared mutable state under parallel execution produces failures that move between tests. Environment differences cover timezone, locale, fonts and browser version. Data drift is a suite that worked for months until a record was deleted or a coupon expired. Genuine product bugs are the most valuable category and the most often discarded — an intermittent failure can be an intermittent bug, and before quarantining anything the question to ask is whether a user could hit this. Infrastructure is the smallest group and the only one where a bounded transport-level retry is legitimate.
Key points
- Timing is the largest category and comes from waiting for a duration instead of a state
- Failures that move between tests under parallel execution point at shared mutable state
- An intermittent failure can be an intermittent bug — ask whether a user could hit it before quarantining
Code
# A worked example of category three, from a real suite.
#
# Symptom: intermittent ConnectionRefusedError, roughly one run in four,
# in whichever tests happened to be running at the time.
#
# Cause: the demo application was started by a session-scoped autouse fixture.
# "Session" means once per WORKER, so the first worker to finish killed the
# server the other three were still using.
#
# Fix: a resource shared by all workers belongs to the controller process.
def pytest_configure(config):
if not hasattr(config, "workerinput"): # controller, not a worker
_start_sut(config)
def pytest_unconfigure(config): # after every worker has finished
_stop_sut(config)
# Generalise it: for every session-scoped fixture, ask what breaks if four
# copies exist and any one of them tears down first.This looked like a network blip and was a lifecycle bug; a retry would have hidden it permanently.
See it in the framework
- Flakiness notes:
docs/08-flakiness.md— live · reviewed - Used by · Root fixtures:
conftest.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Treating every flake as noise, and discarding the small number that are genuine race conditions in the product
- Raising every timeout, which makes the suite slower without addressing the missing wait condition
Practice exercise
Take the last five flaky failures in a pipeline you have access to and assign each to one of the seven categories. Count how many were genuinely infrastructure — usually far fewer than people assume.
Diagnosing a flake methodically
Repeat it, run it alone, shuffle the order, vary the parallelism, change the environment — then read the artifacts.
Diagnosis is a short sequence of experiments, each of which eliminates categories, and following it is much faster than guessing. Begin by establishing that the test is actually flaky rather than simply failing, by repeating it many times in isolation. If it always passes alone, run the full suite and then run it again with the order shuffled; a failure that appears only in a suite points at interdependence, and one that appears only in a particular order confirms it. Next compare a single worker against several: failures that appear only in parallel, and that move between tests from run to run, point at shared state or a shared resource. Then vary the environment by running in a container, which isolates timezone, locale, fonts and browser version. Finally slow things down deliberately, because many races only appear under load. Once the category is known, read the evidence the framework already collects — screenshot, page snapshot, console output and the request log usually make the cause obvious in under a minute.
Key points
- Establish flakiness first by repeating the test in isolation many times
- Alone versus in a suite isolates interdependence; one worker versus several isolates shared state
- Once the category is known, the artifacts you already collect usually show the cause immediately
Code
# 1. Is it actually flaky, or just failing?
pytest path::test_name --count=20 # pytest-repeat
# 2. Order dependent?
pytest path::test_name # alone
pytest tests -p randomly # shuffled
# 3. Parallel only?
pytest tests -n 1 vs pytest tests -n 4
# 4. Environment dependent?
docker compose run tests pytest path::test_name
# 5. Load or latency dependent?
pytest path::test_name --slowmo 1000
# Then read what the framework already captured on failure: the screenshot,
# the page snapshot, the browser console, and the full request/response log.Five experiments, each eliminating categories; guessing is slower than this every time.
See it in the framework
- Flakiness diagnosis playbook:
docs/08-flakiness.md— live · reviewed - Used by · Root fixtures:
conftest.py— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Fixing before diagnosing, which usually means raising a timeout and moving the symptom rather than removing it
- Not capturing artifacts on failure, so every diagnosis starts by trying to reproduce the problem from nothing
Practice exercise
Plant a deliberate fifty-percent race in an application you test, then diagnose it using only this sequence and the artifacts the framework produces — without looking at the code you changed.
Running a quarantine that actually works
A ticket, a non-blocking job, an expiry date and a cap — otherwise quarantine becomes a graveyard.
Quarantine is a legitimate tool for keeping a pipeline usable while a flake is investigated, and it becomes a graveyard the moment its rules are informal. Four rules keep it honest. A ticket number is mandatory, because a quarantined test with no owner is an abandoned test. Quarantined tests must keep running in a non-blocking job, since a quarantined test that stops running is a deleted test with the added harm of a false sense of coverage. There must be an expiry, typically two weeks, after which the test is fixed or removed. And there must be a cap: once more than a small percentage of a suite is quarantined, nobody trusts any result and the right response is to stop feature work until the suite is repaired. Automatic reruns deserve the same discipline. They are a legitimate measurement tool, since a test that passes on rerun is flaky by definition and should be logged, and they are legitimate for keeping a pipeline usable during a fix. Left on permanently, they automate the act of ignoring bugs.
Key points
- A quarantined test needs a ticket, keeps running in a non-blocking job, and has an expiry date
- Cap the quarantine list; past a small percentage nobody trusts any result
- A test that passes on rerun is flaky by definition — log it rather than celebrating the green run
Code
@pytest.mark.flaky
@pytest.mark.xfail(reason="FLAKE-231: race in the cart badge update", strict=False)
def test_badge_updates_immediately(app_as_user):
...
# The process around that marker is what makes it work:
# 1. Flake observed -> ticket opened the same day
# 2. Marked -> still runs, in a NON-BLOCKING job
# 3. Two-week clock -> fixed, or deleted
# 4. Reviewed every sprint -> the list is read out loud
# 5. Capped at ~2% -> above that, stop feature work
# Reruns belong in the CI command, never in shared configuration, so the
# choice stays visible to everyone:
# pytest --reruns 1 --reruns-delay 2Reruns in the pipeline command rather than shared config, so nobody hides flakiness by accident.
See it in the framework
- Implementation · Quarantine marker config:
pyproject.toml— live · reviewed - CI · CI execution:
.github/workflows/ci.yml— live · reviewed - Flakiness notes:
docs/08-flakiness.md— live · reviewed
Reviewed against commit f9ada16 (2026-08-14).
Common pitfalls
- Leaving automatic reruns on permanently, which quietly automates ignoring real defects
- Excluding quarantined tests from every run, which loses the coverage while keeping the illusion of it
Practice exercise
Write a one-page quarantine policy for a team you work with: entry criteria, where quarantined tests run, the expiry, the cap and who reviews the list. Then check how many current exclusions would survive it.