A flaky test passes and fails on the same code. It fails on Tuesday, passes on the rerun, and fails again on Friday on a branch that didn’t touch anything near it. One flaky test is a nuisance. Dozens of them in a large suite teach the team that a red build doesn’t mean much, and that’s when real failures start slipping through.

This guide covers what makes tests flaky, why the usual fix (rerun it) hides the problem, and how to find flaky tests by looking across runs instead of at one.

What makes a test flaky

The test or its environment has something that isn’t the same from one run to the next:

The common thread: the failure says something about the test or the environment, not about the change being tested.

Why reruns hide flaky tests

The usual reaction to a flaky failure is to rerun the job, or to configure automatic retries. The build goes green, and the team moves on.

That treats the symptom. Each rerun costs CI time, and an automatic retry erases the evidence: the run that failed is replaced by the one that passed, so nobody sees how often the test really fails. A test that fails one run in five looks healthy in every individual build. You can’t see flakiness in a single run. It’s a pattern, and you only see it across many.

How to find flaky tests

1. Keep every result, not just the latest

You need the history of each test: every run, on every branch, with its outcome. The latest build’s report isn’t enough. Most CI systems keep logs for a while, but they’re per run and hard to compare. Publish each run’s test results (JUnit XML or similar) to somewhere that keeps them together.

2. Track each test from run to run

With the history in one place, look at each test’s sequence of outcomes, not its overall pass rate. Three tests can have the same 80% pass rate and be very different problems:

A practical rule: a test that fails, passes, and then fails again before it has passed several times in a row is flaky.

3. Compare across branches

A test that fails on several unrelated branches, with no change in common, is very likely flaky: the failures follow the test, not the code. A test that fails on one feature branch and passes everywhere else is more likely a real failure in that branch.

4. Confirm a suspect by repeating it

To check one suspect test, run it many times on the same code. Most frameworks can repeat a test:

If you keep automatic retries in CI, keep an eye on which tests passed only on a retry: Playwright’s report lists them as flaky, and pytest-rerunfailures shows them as reruns in its summary. A results file usually records only the final outcome, so a test that passed on its retry looks like a plain pass, there and in Testspace, and the retries hide exactly what you’re looking for.

5. Watch the count over time

Count the flaky tests at the end of each month. A rising count tells you the suite is getting less trustworthy before the team feels it, and it shows whether time spent fixing flaky tests is paying off.

Doing it with Testspace

Testspace does the tracking for you. Add two steps to your CI, one to set up the client and one to publish the results (see the GitHub Actions guide for the full workflow):

  - uses: testspace-com/setup-testspace@v1
    with:
      domain: ${{ github.repository_owner }}
  # ... run your tests ...
  - run: testspace "test-results/*.xml"
    if: always()

From then on, every failing test is tracked from one result to the next and marked New when it first fails, Flaky when it passes and then fails again, or Consistent when it keeps failing. It stays tracked until it passes five times in a row (or stops running), so a lucky pass doesn’t hide it. (How Testspace decides.)

The Failures view, with two tests marked Flaky

A flaky test's history, run by run and branch by branch

See Automated test results for the rest of what Testspace does with CI results, or look at a flaky test in the live demo: an autocomplete test that fails now and then on almost every branch, the pattern described above.

What to do with a flaky test

Finding a flaky test is half the job. Then:

  1. Decide who owns it. Link it to an issue in your tracker (Testspace links failures to Jira, GitHub, GitLab or Bitbucket issues), so the fix is planned work rather than something everyone ignores.
  2. Quarantine it if it can’t be fixed right away. Marking a known flaky test as Exempt (or as xfail in pytest) keeps it visible but leaves it out of the pass rate and Health, so it no longer turns the result, or the commit’s check, red. Keep the list of exempt tests short and reviewed, or quarantine becomes a place tests go to be forgotten.
  3. Fix the cause, not the symptom. Replace fixed waits with waits for a condition, isolate test data, stub external services, and make each test independent of the others’ order.
  4. Confirm the fix. A fixed test passes run after run. In Testspace, it’s resolved after five passes in a row, and the Flaky count in Trends goes down.

Summary

Try it on your own results

Start a free trial (14 days, no card needed), or explore the live demo, no sign-up needed. Questions? Contact us.