Skip to content

What are flaky tests?

Mikhail Bodnarchuk

Definition

Flaky tests are automated tests that pass on one run and fail on the next with no change to the code or the test. The result depends on something the test does not control, such as timing, test order, shared data, the network or the machine, so a red build stops meaning that something broke.

Why are flaky tests worse than failing tests?

A failing test tells you something broke. A flaky test tells you nothing, because the same commit can go red on one run and green on the next. After a few weeks of reruns, people can stop reading red builds, and a real regression can get the same shrug as the noise.

Flakiness also clusters. In a study first published in April 2025 (an older source), Parry and colleagues analyzed 10,000 test suite runs from 24 Java projects on GitHub, with 810 flaky tests. They found that 75% of the flaky tests belonged to a cluster of tests that fail together, with a mean cluster size of 13.5. The authors name intermittent networking issues and unstable external dependencies as the predominant causes of these shared failures. The practical reading: fix one shared cause and a dozen tests may stop flaking at once.

For what flakiness costs large engineering teams, see our guide to test automation.

What causes flaky tests?

CauseHow it shows upUsual fix
TimingA check runs before the page, job or API call has finishedWait for a condition, never for a fixed number of seconds
Shared state and test orderA test passes alone and fails after another testCreate data inside each test; reset state in a fixture
Shared test dataParallel workers edit the same user or recordUnique data per test or per worker
Network and external servicesTimeouts, rate limits or a sandbox that is downMock the service, or move the check to a contract or API test
EnvironmentTime zone, locale, screen size or CPU load differ between machinesPin them in config, and run CI on the same image every time

The second row is the hardest to see, because each test looks correct on its own. Here is one, reproduced.

Example: a test that only fails after another test

Both tests in tests/cart.spec.js share one cart array at module level. Run in order on one worker, the second test sees the book the first one added.

// Two tests share one module-level cart. Each passes alone; together, order decides.
const { test, expect } = require('@playwright/test');

const cart = []; // shared state: lives as long as the worker process

test('adds a book to the cart', () => {
  cart.push('book');
  expect(cart).toEqual(['book']);
});

test('starts with an empty cart', () => {
  expect(cart).toEqual([]);
});

We ran it with Playwright 1.65.1 on Node 22.9, workers: 1 in the config, and one retry: npx playwright test --retries=1. Output, with the assertion diff block trimmed:

Running 2 tests using 1 worker

  ✓  1 tests/cart.spec.js:6:1 › adds a book to the cart (4ms)
  ✘  2 tests/cart.spec.js:11:1 › starts with an empty cart (3ms)
  ✓  3 tests/cart.spec.js:11:1 › starts with an empty cart (retry #1) (4ms)

  [assertion diff trimmed: expected [], received ["book"]]

  1 flaky
    tests/cart.spec.js:11:1 › starts with an empty cart ────────────────────────────────────────────
  1 passed (651ms)

The exit code was 0, so CI would go green. The retry passed for a reason the Playwright retries docs explain: after a failure, Playwright discards the whole worker process and starts a new one, and the retry runs in the fresh process, where the module-level cart is empty again. The bug in the test did not go away. The retry stepped around it.

Two more runs show the rest of the story. npx playwright test --grep "empty cart" runs the second test alone, and it passes. Adding --fail-on-flaky-tests to the retry run makes the same suite exit with code 1. The fix is one line: create the cart inside each test, or in a fixture.

How do you find flaky tests before they find you?

Run the suite more than once, on purpose, and look at the tests whose result changes.

  • Playwright labels each test "passed", "flaky" (failed first, passed on retry) or "failed" when retries are on. Its command line adds --repeat-each <N> to run every test N times, --shuffle [seed] to shuffle test files (and individual tests in parallel mode) and print the seed so you can replay it, and --fail-on-flaky-tests to make any flaky result fail the run.
  • CodeceptJS has run-rerun, which repeats whole runs to detect flaky tests, configured with minSuccess and maxReruns (CodeceptJS commands). It also has run --shuffle and run --last-failed.

To see run-rerun work, we wrote a stub that fails on every fourth run, standing in for a timing bug:

// report_test.js: a stub for a timing bug. It counts runs in a file and fails on every fourth one.
import assert from 'node:assert';
import { appendFileSync, readFileSync } from 'node:fs';

Feature('Report export');

Scenario('export finishes before the check', () => {
  appendFileSync('runs.txt', 'x');
  const run = readFileSync('runs.txt', 'utf8').length;
  assert.ok(run % 4 !== 0, `run ${run}: export still running`);
});

codecept.conf.js sets rerun: { minSuccess: 10, maxReruns: 10 }, which runs the whole suite exactly 10 times. CodeceptJS 4.2.5 needs Node 22.12 or later, so we ran node node_modules/codeceptjs/bin/codecept.js run-rerun with Node 24.22 (what npx codeceptjs run-rerun runs). The output, filtered with grep -E "of max|Flaky":

Process run 1 of max 10, success runs 1/10
Process run 2 of max 10, success runs 2/10
Process run 3 of max 10, success runs 3/10
Fail run 4 of max 10, success runs 3/10
Process run 5 of max 10, success runs 4/10
Process run 6 of max 10, success runs 5/10
Process run 7 of max 10, success runs 6/10
Fail run 8 of max 10, success runs 6/10
Process run 9 of max 10, success runs 7/10
Process run 10 of max 10, success runs 8/10
Error: Flaky tests detected! 8 success runs achieved instead of 10 success runs expected
Flaky tests detected! 8 success runs achieved instead of 10 success runs expected

The command exited with code 1. A nightly job like this can catch a test that fails one run in four long before it blocks a release.

Should you retry flaky tests or quarantine them?

Both, with limits. Retries keep a pipeline moving while you investigate. Quarantine keeps a known flaky test from blocking merges. Neither fixes anything.

NeedPlaywrightCodeceptJS
Retry a failed testretries in config or --retries; per group with test.describe.configure({ retries: 2 })Scenario('...', { retries: 3 }, ...), or retry in config with a grep pattern such as '@flaky'
Retry one unstable stepActions auto-wait until the element is actionablestep.retry(5), retryTo() for a group of steps, and the retryFailedStep plugin
Know a retry happenedtestInfo.retry inside the test; "flaky" in the reportDEBUG_RETRY_PLUGIN=1 for retry logs
Keep a test out of the blocking runTag it @flaky, run with --grep-invert @flakyTag it @flaky, run with --grep "@flaky" --invert

The CodeceptJS retry guide states the limit plainly in its best practices: "Retry timeouts, not bugs." It also notes that CodeceptJS retries failed steps by default through retryFailedStep, and that step retries are disabled by default when a scenario has its own retries, to prevent excessive execution time.

A tag only quarantines if someone owns it. Run the quarantined tests in a separate, non-blocking job, give each one an owner and a deadline, and delete the tag when the cause is fixed.

What should a flaky test policy cover?

  1. Flaky is a result, not a pass. Report flaky tests separately and review them weekly.
  2. Retries are capped and targeted. One or two retries, applied by tag or pattern, never unlimited.
  3. The main branch fails on flaky. Use --fail-on-flaky-tests there, even if pull request builds allow retries.
  4. Every quarantined test has an owner and a date. Otherwise quarantine becomes a graveyard.
  5. A nightly stability run. Repeat or shuffle the suite to find order and timing bugs early.
  6. Healing is reviewed. An AI step that repairs a locator can also hide a real change in the UI; our page on self-healing test automation covers that risk.

If your suite flakes and nobody knows why, our QA and test automation team starts with a test audit: feedback on speed, stability and maintainability from the founders of Codeception and CodeceptJS.

Sources

  1. Retries, Playwright docs (Microsoft), 2026-11-05
  2. Command line, Playwright docs (Microsoft), 2026-10-30
  3. Auto-waiting, Playwright docs (Microsoft), 2026-11-02
  4. Retry Mechanisms, CodeceptJS docs, 2026-11-18
  5. Commands, CodeceptJS docs, 2026-10-08
  6. Systemic Flakiness: An Empirical Analysis of Co-Occurring Flaky Test Failures, arXiv (Parry, Kapfhammer, Hilton, McMinn), 2025-04-23

Frequently asked questions

Is a flaky test a problem in the test or in the product?

It can be either. Order and timing bugs in test code cause flakiness, and so can a race condition in the product, so read the failure before you add a retry.

Should I delete a flaky test?

Only if it checks nothing you need. Otherwise tag it, move it to a non-blocking job with an owner, and fix or rewrite it, often at the API level instead of through the UI.

Why does my test pass locally but fail in CI?

Look at what differs between the two machines: worker count, hardware load, time zone, locale and screen size. Reproduce with the same worker count, then pin the rest in config.

Can AI fix flaky tests?

AI can suggest a better wait or locator, and CodeceptJS can heal a failed step at run time when AI is enabled. Review every change, because a repair that makes a test pass can also hide a real defect.

Building with AI agents? Talk to an engineer.