A flaky test behaves as if it were both working and broken: on the same code one run is green and another is red. For continuous integration this is the most unpleasant kind of failure. A red run stops being a signal that the code is broken, and real defects get lost among false ones.
Why it hurts
In a CI pipeline tests are a checkpoint: changes are not merged until everything is green. When a test fails every other run, trust in the runs is lost. Developers get used to re-running and ignoring red builds, and sooner or later a real defect slips through with them. There is also direct loss of time: re-running the suite and investigating false failures instead of working on tasks.
Typical causes
| Cause | What happens |
|---|---|
| Shared state | the test uses data left behind by a previous one; on its own it passes because it starts in a clean environment |
| Order and parallelism | the test relies on the ones that run before it, or conflicts with its neighbours over a port, a file or a database record |
| Timing | a sleep(2) pause instead of waiting for an event: on a slow machine the event arrives later and the check fails |
| Overly strict assertions | comparing floating-point numbers without a tolerance, exact time, a rigid order of elements |
| Environment | network, external services, the system clock, locale: anything that changes without the test knowing |
The higher the level of a test, the less stable it tends to be: a unit test keeps state under control, while an end-to-end test depends on the browser, the network and the whole test environment. End-to-end tests through the interface are known for flakiness (animations, timings, unexpected dialogs), and their failures are often false.
What to do
- isolate the test: it creates and cleans up its own data, takes nothing from its neighbours and leaves nothing for them
- replace pauses with waiting for an event, and relax overly strict assertions: a tolerance for floating-point numbers, free ordering
- reproduce it and find the culprit: run the test alone, then many times in a row, then in random order together with its neighbours
- enable retries of failed tests in CI: a test that passes on the second attempt is marked as flaky, but a retry is only a safety net and does not remove the cause
- put a chronically flaky test in quarantine: mark it so it does not block merges, and fix it separately
- if the functionality is already covered by other tests, delete it; otherwise rewrite it at a lower level
pytest --reruns 2 tests/e2e # pytest-rerunfailures plugin: up to two retries of a failed testSources
the term is written from these pages- Flaky tests — pytest documentationdocs.pytest.org/en/stable/explanation/flaky.html(opens in a new tab)
- Manage flaky tests — Azure Pipelineslearn.microsoft.com/en-us/azure/devops/pipelines/test/flaky-test-management(opens in a new tab)
- Ham Vocke — The Practical Test Pyramid (martinfowler.com)martinfowler.com/articles/practical-test-pyramid.html(opens in a new tab)
Verified 9 Nov