# What are flaky tests? Causes, detection and retries

URL: https://sdclabs.com/glossary/flaky-tests/

> Flaky tests pass and fail on the same code. What causes them, how to detect them, and how Playwright and CodeceptJS retry, rerun and tag them, with runs.

# What are flaky tests?

[Mikhail Bodnarchuk](https://sdclabs.com/author/bodnarchuk/) 2026-12-01

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](https://arxiv.org/abs/2504.16777) (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](https://sdclabs.com/blog/what-is-test-automation/) .

## What causes flaky tests?

| Cause | How it shows up | Usual fix |
|---|---|---|
| Timing | A check runs before the page, job or API call has finished | Wait for a condition, never for a fixed number of seconds |
| Shared state and test order | A test passes alone and fails after another test | Create data inside each test; reset state in a fixture |
| Shared test data | Parallel workers edit the same user or record | Unique data per test or per worker |
| Network and external services | Timeouts, rate limits or a sandbox that is down | Mock the service, or move the check to a contract or API test |
| Environment | Time zone, locale, screen size or CPU load differ between machines | Pin 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](https://playwright.dev/docs/test-retries) 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](https://playwright.dev/docs/test-cli) 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](https://codecept.io/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.

| Need | Playwright | CodeceptJS |
|---|---|---|
| Retry a failed test | retries 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 step | Actions [auto-wait](https://playwright.dev/docs/actionability) until the element is actionable | step.retry(5), retryTo() for a group of steps, and the retryFailedStep plugin |
| Know a retry happened | testInfo.retry inside the test; "flaky" in the report | DEBUG_RETRY_PLUGIN=1 for retry logs |
| Keep a test out of the blocking run | Tag it @flaky, run with --grep-invert @flaky | Tag it @flaky, run with --grep "@flaky" --invert |

The [CodeceptJS retry guide](https://codecept.io/retry/) 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.
1. **Retries are capped and targeted.** One or two retries, applied by tag or pattern, never unlimited.
1. **The main branch fails on flaky.** Use --fail-on-flaky-tests there, even if pull request builds allow retries.
1. **Every quarantined test has an owner and a date.** Otherwise quarantine becomes a graveyard.
1. **A nightly stability run.** Repeat or shuffle the suite to find order and timing bugs early.
1. **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](https://sdclabs.com/glossary/self-healing-test-automation/) covers that risk.

If your suite flakes and nobody knows why, our [QA and test automation](https://sdclabs.com/services/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](https://playwright.dev/docs/test-retries) , Playwright docs (Microsoft), 2026-11-05
1. [Command line](https://playwright.dev/docs/test-cli) , Playwright docs (Microsoft), 2026-10-30
1. [Auto-waiting](https://playwright.dev/docs/actionability) , Playwright docs (Microsoft), 2026-11-02
1. [Retry Mechanisms](https://codecept.io/retry/) , CodeceptJS docs, 2026-11-18
1. [Commands](https://codecept.io/commands/) , CodeceptJS docs, 2026-10-08
1. [Systemic Flakiness: An Empirical Analysis of Co-Occurring Flaky Test Failures](https://arxiv.org/abs/2504.16777) , 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.

Services

- [QA and test automation](https://sdclabs.com/services/qa-and-test-automation/)
- [DevOps and TestOps](https://sdclabs.com/services/devops-and-testops/)

Read next

- [What is test automation? A guide from the founders of Codeception and CodeceptJS](https://sdclabs.com/blog/what-is-test-automation/)
- [Faster delivery needs stronger tests: QA and test automation in 2026](https://sdclabs.com/blog/qa-test-automation-challenges/)

Related terms

- [End-to-end testing](https://sdclabs.com/glossary/end-to-end-testing/)
- [Regression testing](https://sdclabs.com/glossary/regression-testing/)
- [Self-healing test automation](https://sdclabs.com/glossary/self-healing-test-automation/)
- [Shift-left testing](https://sdclabs.com/glossary/shift-left-testing/)

[Mikhail Bodnarchuk](https://sdclabs.com/author/bodnarchuk/) Founder of Codeception and CodeceptJSBoth are open-source testing frameworks: Codeception for PHP, CodeceptJS for JavaScript.[LinkedIn](https://www.linkedin.com/in/davert/) [GitHub](https://github.com/DavertMik)

## Building with AI agents? Talk to an engineer.

[Start a project](https://sdclabs.com/contact/) [Book a call with the CEO](https://calendly.com/sdclabs/60min)
