Skip to content

Three Runs See a 1% Flake 3% of the Time, and Retries Hide the Rest

I ran flaky tests 300 times each in pytest and Node. A rerun gate misses quiet flakes, retries turned 22 red runs green, and failures came in clusters.

· · 11 min read
Bar chart of how often three or five reruns see a flaky test at failure rates from 10 percent down to 0.1 percent

I've written "run it three to five times" in one post and "20 is a reasonable default" in another. Both sentences sounded sensible. Then I did the arithmetic and one of them stopped being true. So I built four small tests that really are nondeterministic, ran them a few thousand times, and kept the numbers.

What did I actually run?

Four pytest tests and one Node test, nothing synthetic. No random() anywhere. The failures come from three places that cause most of the real ones.

A lost update: two threads read a counter, one of them yields at a scheduling point, both write back. An order dependence: one test assumes a module-level cache is empty, another fills it. A timer budget: sleep 5 ms, assert it took under 8 ms. The same budget test exists in Node's built-in test runner.

Python 3.13.9, pytest 9.1.1, pytest-rerunfailures 16.7, pytest-randomly 5.0.0, Node 22.23.3, an 8-core Mac. Each condition ran 300 times in fresh processes. One caveat I can't scrub out: the Mac already had a load average around 14 on those 8 cores from other jobs when I started, so "no extra load" really means "no load from me". That turns out to be useful, because it looks a lot like a shared CI runner.

What did 300 runs say?

Bars showing failure rates of four flaky tests under file order, shuffled order and extra CPU load, out of 300 runs each
Failure rate per condition, 300 runs each. Whiskers are 95% intervals. Source: the measure.py script in the example folder.

The order test never failed in file order, 0 of 300, and failed 147 times in 300 once pytest-randomly shuffled things. Same code. A default CI run would have called it green for years.

The timer budget went from 2 failures in 300 to 33 when I added one busy loop per CPU. That's 0.7 percent to 11.0 percent from a change nobody would call a code change. Node's version moved from 4 to 11 failures, though the intervals overlap, so I wouldn't call that one proven.

The lost update surprised me. It failed 24 times in 300 in file order, 16 in shuffled order, and only 5 times with extra CPU load. More load, fewer failures. I didn't chase the cause. My guess is that with every core busy the two threads rarely overlap at the yield point, but that's a guess, not a result.

So the first thing to take from this is that a flake rate isn't a property of the test. It's a property of the test and the machine it runs on. A number you measured on your laptop is a number about your laptop.

How many runs does a rerun gate need?

The twelve-gate list I published earlier puts flaky-test detection at "three or five reruns". Run a test K times and the chance of seeing at least one failure is 1 - (1 - p)^K. The example's script checks that formula against a seeded Monte Carlo run and refuses to print if the two disagree by more than a point.

Failure rate p Seen in 3 runs Seen in 5 runs Seen in 20 runs Runs for 95%
10% 27.1% 41.0% 87.8% 29
5% 14.3% 22.6% 64.2% 59
1% 3.0% 4.9% 18.2% 299
0.1% 0.3% 0.5% 2.0% 2,995

Read the 1 percent row twice. A test that fails once in a hundred runs survives the "twenty times in a loop" advice from my coverage post about four times in five. The rerun gate finds the loud flakes, the ones that were already annoying someone. The quiet ones, which are the ones that fail one pull request in thirty, walk straight through.

And quiet isn't harmless, because flakes add up across a suite. With 50 independent flaky tests at 1 percent each, 39.5 percent of pull requests go red from flakes alone. At 200 tests and 0.5 percent each it's 63.3 percent. Nobody has 200 tests that bad, you say? Probably true. But nobody counts them either.

Line chart of the share of pull requests that fail from flaky tests alone, for 0 to 200 flaky tests at four failure rates
Share of pull requests that go red from flakes alone, from 1 - (1 - p)^n with no retries.

That's why the RICE example scored a flaky helper above a slow build. The slow build is loud, so someone fixes it. The flaky helper taxes every merge a little and gets blamed on nothing.

What does a retry policy do to the evidence?

Now the other half. pytest --reruns 2 re-executes a failed test up to twice and only reports red if all three attempts fail. It's one flag, and the plugin's own docs show the reruns as R in the progress line.

Here's the arithmetic if each attempt is independent: a test that fails 30 percent of the time shows red in 2.7 percent of runs with two retries. A test that fails 5 percent of the time shows red in 0.01 percent. Meanwhile a test that fails every time, a real regression, costs exactly three executions instead of one, which is the only price anyone sees.

I ran it for real, 300 runs under CPU load, with a small conftest plugin writing one line per attempt:

def pytest_runtest_logreport(report):
    if report.when != "call":
        return
    # "rerun" = a failed attempt that will be retried; the last one is passed/failed
    with open(LEDGER, "a") as fh:
        fh.write(json.dumps({"test": report.nodeid, "outcome": report.outcome}) + "\n")

The verdict: 274 runs green on the first try, 22 green only after a retry, 4 red. A different run of the same script gave 279, 17 and 4. Twenty of those 22 hidden failures were the timer budget. Two were the lost update, which in a real codebase would be a genuine race in the code, not a flaky test. The retry turned it green and nothing in the exit code said so.

A retry does not remove a flaky test. It deletes the only evidence that the test exists.

SHARE Post on X Bluesky

Do failures arrive independently?

No, and that's where my own table was too kind.

The timer test failed on its first attempt in 24 of 300 runs, 8.0 percent. If attempts were independent, the chance it fails all three times is 0.08 cubed, about 0.05 percent, which is 0.15 runs out of 300. I got 4. Looking at the ledger: of the 24 first-attempt failures, 7 failed again on the second attempt (29 percent, against the 8 percent independence predicts), and 4 of those 7 failed a third time.

The counts are small and I'd hold the exact percentages loosely. The direction isn't subtle, though. A retry fires milliseconds after the failure, under the same load, on the same CPU. Whatever caused the first failure is usually still there. Retries work best against exactly the kind of flake that doesn't need fixing, a one-off blip, and worst against a flake with a persistent cause, which is the one you'd want to know about.

I'd go further than "use retries carefully". Switch them off for unit tests entirely. A unit test that needs a second chance is telling you something about the test or the code, and the retry is just a way of not listening. Retries earn their place at the end-to-end layer, where a browser and a network are involved, and even there only if every retry is logged.

What should you track instead?

Every attempt, with the commit. A test that produced both a pass and a fail on the same commit is flaky by a definition you can compute without rerunning anything. If you retry, you already pay for the data. You just throw it away at the exit code.

The tools differ in how much they keep. Per the docs I read for this piece:

Tool Retry or repeat knob What it reports
pytest-rerunfailures --reruns N Reruns as R; -r aR lists each attempt
Playwright retries in config Pass after a failure is classed as flaky
Bazel --flaky_test_attempts Pass after a failure shows as FLAKY, counts as passed for the exit code
Bazel --runs_per_test with --runs_per_test_detects_flakes Mixed PASS and FAIL in one shard is flaky
Go go test -count=n, -shuffle Repeats and reorders; shuffle is off by default

I ran pytest and Node myself. I didn't run Go, Playwright or Bazel for this, so that column is what the documentation says, no more. What the column shows is that Playwright and Bazel both give a pass-after-failure its own label. Counting those labels per month is a job for you, since neither tool will schedule it.

Order dependence needs a different knob. pytest-randomly shuffles modules, classes and functions and prints its seed, and --randomly-seed replays an order. Go has -shuffle. Run it in a nightly job, not on every pull request, and when it fails you get a reproducible seed instead of a mystery. That 49 percent in my chart was a test that no ordinary run would ever have flagged.

What happens to a quarantined test?

Quarantine is the right move for a flake you can't fix this week, and it has the same disease as every temporary exemption: it becomes permanent unless something fails when it expires. Move the test out of the blocking job (in pytest, a marker and -m "not quarantine" does it), keep running it in a non-blocking one, and put it on a list with an owner and two dates.

Then gate the list. My example has a 40-line script that fails the build for four things: no owner, no dates, a window over 14 days, or an expiry in the past. Fed three deliberately bad entries, it says:

FAIL test_real_flakes.py::test_sleep_fits_its_budget: expired 2 days ago, fix it or delete it
FAIL test_real_flakes.py::test_b_assumes_an_empty_cache: no owner
FAIL test_real_flakes.py::test_b_assumes_an_empty_cache: window is 365 days, cap is 14
3 quarantined, 3 need a decision

The 14 days come from my earlier post and are my choice, not a standard. What matters is that the cap is a number a script can enforce, and a human reminder can't. It's the same mechanism as the suppression count I suggested tracking for lint: the exemption is fine, the unbounded exemption isn't.

What would I do this week?

Not a framework. A short list, in this order:

  • Find out how many of last month's green runs passed only on a retry. If your CI doesn't record that, that's the finding.
  • Write every attempt to a file, with the commit hash. Flag tests with both outcomes on one commit.
  • Run the suite once under pytest-randomly or go test -shuffle=on and read the failures that appear.
  • Turn retries off for unit tests, or cap them with a logged reason.
  • Give each quarantined test an owner and an expiry, and fail the build on the expired ones.

The lost update in my example is a stand-in for a harder thing I wrote about before, a spin-lock that never crashed and lost three quarters of its updates. A retry policy would have made that green too. If the same trick makes a lint gate or a test gate green without reading the first failure, what exactly is the gate for?

Which of your green runs passed on the second try, and who would know? The CI gate audit reads a pipeline file in your browser and shows which gates it's missing, including this one. The measurement code lives in the blog's examples/flaky-tests-retry-math folder, and it prints different digits on your machine, which is the whole point.

Frequently Asked Questions

How many times should you rerun a test to catch a flaky one?
It depends on the failure rate, and the numbers are bigger than most pipelines assume. Running a test K times sees a failure with probability 1 - (1 - p)^K. For a 5 percent flake, 59 runs give 95 percent confidence. For a 1 percent flake it takes 299, and for 0.1 percent about 3,000. Three or five reruns catch only the loud ones.
Do test retries hide real bugs?
Yes. In my pytest run with two retries allowed, 22 of 300 runs went green only because a retry passed, and 2 of those hid a genuine lost-update race in the code under test. A retry policy is correct for a flaky environment and wrong for an intermittent defect, and the pass/fail line can't tell them apart.
How do you find flaky tests without rerunning everything?
Log every attempt, not just the final verdict, and flag any test that has both outcomes on the same commit. If you already retry, you're already generating that data. pytest-rerunfailures marks reruns with R, Playwright reports a pass-after-failure as flaky, and Bazel reports it as FLAKY. The work is keeping the record.