Skip to content
Prompt Words
History Member

Flaky test

Also called: intermittent test, nondeterministic test.

A test that sometimes passes and sometimes fails on the same code, because it depends on something it does not control, such as timing, the order tests run in, or the network. Retrying it until it passes turns the pipeline green but leaves the cause, and the cause may be a real race in the app, not only in the test. Fix it, or quarantine it with an owner and a date to fix it by, rather than deleting it.

Run it 10 times: same commit, same test, and a save that takes longer on a busy machine. Retry until it passes turns the job green but only moves the failures into the report; Wait for the real condition removes the guess.

Passed: 0 · Failed: 0 · Flaky (passed on retry): 0

One test, one commit, not run yet. The test waits a fixed 400 ms for a toast that shows after a save.

    Test not run yet

    Say it in a prompt

    The Playwright test 'shows the saved toast' fails about 3 times in 10 on CI with the same commit. Reproduce it with npx playwright test --repeat-each=20, replace page.waitForTimeout(400) with await expect(page.getByRole('status')).toBeVisible(), and check whether the app itself has a race when Save is pressed twice. Don't add retries to hide it. If it can't be fixed today, tag it @quarantine and run that tag in a non-blocking CI job (or test.fixme if it must be off), with an issue that has an owner and a date.

    Vague vs precise prompt

    Vague prompt

    the checkout test keeps failing randomly, make CI pass

    Typical resultTurns on retries: 3 in the CI config, or raises the sleep from 400 ms to 2 s. The pipeline goes green, every run gets slower, and the timing bug, or a real race in checkout, is still there for the next busy machine.

    Precise prompt

    Reproduce the flaky checkout test with --repeat-each=20, replace the fixed waitForTimeout with an assertion that waits for the toast, and check the app for a race. No new retries; if it can't be fixed today, tag it @quarantine, run that tag in a non-blocking CI job, and open an issue with an owner and a date.

    Typical resultThe test waits for what it checks, so it passes 20 of 20 on a slow machine too. A real race, if there is one, is found instead of hidden, and a quarantined test keeps running in its own non-blocking job, with someone and a deadline to bring it back.

    Seen on

    • Martin Fowler: Non-deterministic tests sometimes pass and sometimes fail. Quarantine them in their own suite, but fix them quickly and don't abandon them; a limit, such as a week in quarantine, stops them being forgotten. Never use bare sleeps to wait for an asynchronous response: use a callback or polling.
    • Google Testing Blog: About 1.5% of all test runs report a flaky result, and almost 16% of tests have some flakiness: they occasionally fail in a way not caused by a change to the code or the tests.
    • Playwright docs: Failing tests are not retried by default. With retries on, a test that fails first and then passes on a retry is reported as flaky, not as passed.
    • Playwright docs: page.waitForTimeout is discouraged: tests that wait for time are inherently flaky. Use locator actions and web assertions that wait automatically.
    • Playwright docs: Assertions such as toBeVisible retry until they pass or the assertion timeout, 5 seconds by default, runs out.

    You might describe it as

    • a test that fails now and then for no clear reason
    • it goes green if I just rerun the job
    • red in CI but it passes on my laptop

    Not to be confused with

    • Retry with backoff

      Retrying a flaky test until it passes hides a bug in the test or the code; retry with backoff handles a real, temporary failure of a network call.