RETRIES HIDE THE BUG
A pass on the second try is still a failure
Your CI retries a failed test up to three times and counts any pass as a pass. A test exercises a race in your checkout code that fails about four runs in ten. How often does a build containing that bug go green?
A retry asks the same question again and reports only the best answer. For a deterministic bug that does nothing. For a race, a timeout or a shared resource, it turns “fails sometimes” into “passes”, and the failed attempts disappear. That is the opposite of what a flaky test needs, which is a record that it flaked.
The honest policy keeps retries and keeps the evidence. It records every attempt, treats a pass on retry as a signal, fails a vanishing assertion because it may be your product, and quarantines a repeating infrastructure flake with an owner and a deadline. The same idea sits behind retries in production code: a retry is only safe when something notices it happened.
# the runner
retry_failed_tests: 3 # any pass is a pass
# no record of the failed attempts
# the dashboard counts tests that failed all 3 times: 0
# a real race passes on attempt 2 and ships// verdict from the run history, not from the last attempt
function decide(history, ctx) {
// all attempts failed -> fail
// passed first time -> pass
// failed an assertion, then passed -> fail, flagged (a possible product race)
// infra failure, then passed -> pass, flagged
// 3 pass-on-retry builds in the last 10 -> quarantine, owner + deadline
} Compare the dashboard’s flaky count with the number of tests that actually needed a retry. In the first scenario they never meet.
The suite has been quiet for three nights under the auto-retry policy. Add one event before a build. Make a build go green over a real product race that nobody is told about.
GOAL: a build deploys over a product race, unflagged
Write decide(history, ctx) for one test. history is that test's builds, oldest first; each build is an array of attempts, in order, { ok, kind } (kind is 'assertion' or 'infra' on a failure). ctx is { owner, today }. Return { verdict, flagged, owner?, deadline? }. The latest build decides. All attempts failed: 'fail', not flagged. Passed first time: 'pass', not flagged. Passed only on a retry: flagged, and if any earlier attempt failed an assertion the verdict is 'fail', because a retry does not erase a product bug. Otherwise 'pass', unless three or more of the last 10 builds (this one included) passed only on a retry: then 'quarantine' with owner: ctx.owner and deadline: ctx.today + 14.
Any CI with a retry flag: Jest's jest.retryTimes, pytest-rerunfailures, Gradle's test retry plugin, GitHub Actions re-run buttons, Buildkite and CircleCI automatic retries. The safe settings record each retry and report tests that passed only on retry as their own category. Quarantine lists, with an owner and an expiry, replace skip annotations that nobody revisits. Slack's write-up above describes a suppression system that disables a test after a failure and opens a ticket for its owner.
“Our CI retries failures three times. Is that a problem?” Say it is fine for infrastructure noise and dangerous when it is silent. Explain why a race passes on retry, then describe the fix: record every attempt, flag a pass on retry, fail on a vanishing assertion until someone has looked, and quarantine with an owner and a deadline so the flaky count goes down instead of up.
USED IN
Chapters that build on this lesson.
- AI agents in the test loop: generation, triage and what to verifySoftware Testing and Quality
- Flaky tests: causes, quarantine and cureSoftware Testing and Quality
- Testing across services: fault injection and deterministic simulationSoftware Testing and Quality