Bookmarks

Flaky test

A test that sometimes passes and sometimes fails with no change to the code or the runtime environment: the result depends on order, timing and environment state, not only on the code.

Also: flaky, unstable test, test flakiness, non-deterministic test

checked 9 Nov

All terms

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

CauseWhat happens
Shared statethe test uses data left behind by a previous one; on its own it passes because it starts in a clean environment
Order and parallelismthe test relies on the ones that run before it, or conflicts with its neighbours over a port, a file or a database record
Timinga sleep(2) pause instead of waiting for an event: on a slow machine the event arrives later and the check fails
Overly strict assertionscomparing floating-point numbers without a tolerance, exact time, a rigid order of elements
Environmentnetwork, 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

  1. isolate the test: it creates and cleans up its own data, takes nothing from its neighbours and leaves nothing for them
  2. replace pauses with waiting for an event, and relax overly strict assertions: a tolerance for floating-point numbers, free ordering
  3. reproduce it and find the culprit: run the test alone, then many times in a row, then in random order together with its neighbours
  4. 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
  5. put a chronically flaky test in quarantine: mark it so it does not block merges, and fix it separately
  6. if the functionality is already covered by other tests, delete it; otherwise rewrite it at a lower level
bash
pytest --reruns 2 tests/e2e  # pytest-rerunfailures plugin: up to two retries of a failed test

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • regression testing

    Regression testing is a kind of check: did something that used to work break? Flakiness is not a kind of testing but a defect of an individual test: it fails for no visible reason.

In depth in the references

where the topic is covered in detail