Notes
Why it hurts
Why do flaky tests hurt so much? Because people learn to ignore red. Developers re run the pipeline until it goes green, and soon they re run real failures too, so real bugs ship. Hours go into investigating failures that were not bugs. Retries slow every pipeline down. A suite nobody trusts can be worse than no suite at all, because it gives false confidence and still costs money to run.
- devs re-run until green → real failures get re-run too → bugs ship
- time lost investigating "failures" that are not pipelines slow down with retries
- a suite nobody trusts is worse than no suite
Cause 1: timing
The most common cause is timing. A test searches for mug, sleeps for two seconds, then checks for two results. Usually the search is fast and it passes. Switch on ShopRung's slow search bug, or run on a busy CI machine, and the search takes two and a half seconds: the test checks too early and fails. The fix is never a longer sleep. Wait for the condition itself, the result count showing two, with a timeout. Part five shows how.
page.fill("#q", "mug");
page.click("text=Search");
Thread.sleep(2000); // guess
assert count == 2;
bug "slow-search": ~2.5 s
→ count not updated yet: FAIL
fast machine, quiet CI: PASS
fix: wait FOR the condition,
not for a time Cause 2: data and order
The second cause is shared state. Test A buys all three diaries; test B expects three in stock. B passes alone and fails after A, so its result depends on the order the runner happens to choose. Shared state hides in many places: static variables, shared browser sessions, leftover cookies, caches and files written to disk. The fixes come from the test data episode: own your data, reset, and use unique values.
- test A buys all 3 diaries
- test B expects stock 3 → passes alone, fails after A
- also: static variables, shared browser sessions, leftover cookies, caches, files written to disk
- fix: own data, reset, unique
Cause 3: environment
The third cause is the environment. A third party API times out. Someone redeploys the shared staging environment in the middle of your run. The CI machine is overloaded, so everything is slower. Rate limits kick in. The CI machine has a different time zone or language setting, so dates print differently. Fixes include stubbing third parties, giving each run its own environment, and setting sensible timeouts with clear error messages.
- a third-party API times out shared staging redeployed mid-run
- CI machine overloaded: everything is slower
- DNS, certificates, rate limits
- different time zone or locale on the CI machine
- fix: stub third parties, own environments, timeouts
Cause 4: time and random
The fourth cause is time and randomness. A test about a coupon expiring today fails when it runs just after midnight in universal time. Month ends, leap years and daylight saving changes break date logic. Random data without a seed produces a different case every run. Assuming hash set order, as you saw in the collections episode. And parallel threads touching one object. Inject the clock, seed random generators, and compare collections by content.
- "expires today" tests that fail after midnight UTC
- month-end, leap years, DST
- random data without a seed
- HashSet order assumed
- parallel threads touching one object
- fix: inject clock, seed
- random, compare sets as sets
Prove it
First prove the flake. Run the test many times in a row: Playwright has repeat each, and pytest has a plugin that adds a count option. Forty nine passes out of fifty is a flaky test, not a passing one. Run the suite in a random order to expose order dependence: recent Playwright versions have a shuffle option, and pytest has random order plugins. Then isolate: does it fail alone, or only after a certain other test?
# repeat 50 times npx playwright test search --repeat-each=50 pytest -q --count=50 (pytest-repeat) # random order to expose # order dependence npx playwright test --shuffle pass 49 of 50 = flaky
Diagnose
Then diagnose with evidence, not guesses. Keep a trace, video or screenshot from the moment of failure. Look at the network log for slow or failed calls, and at timestamps for what was slow. Note which tests ran just before. Check the CI machine's load and time. Comparing a passing run and a failing run side by side usually shows the difference within minutes. Part five shows Playwright's trace viewer, which records exactly this.
- trace / video / screenshot at the moment of failure
- network log: slow or failed calls?
- timestamps: what was slow?
- which tests ran just before CI machine load and time
- compare a passing and a failing run side by side
Fix or quarantine
Fix the cause: wait for conditions, own the data, stub third parties, inject the clock. If you cannot fix it today, quarantine it: move it out of the suite that blocks merges, but keep running it so you collect evidence, file a ticket, name an owner, and set a date. Never delete it silently, and never hide it behind endless retries. A quarantined test that nobody owns is just a deleted test that still costs money.
- fix the cause: wait for conditions, own data, stub third parties
- cannot fix today? quarantine: move it out of the blocking suite, keep running it, file a ticket, name an owner, set a date
- never: delete silently or retry forever
Retries
Retries are useful as a signal, not as a cure. Many teams allow two retries in CI and none locally. Playwright then marks a test that failed and then passed as flaky in its report, separate from passed and failed. Track the flake rate per test, and treat every flaky mark as a bug to fix. With zero retries on your own machine, you see flakes while you are still writing the test, which is the cheapest time to fix them.
// playwright.config.ts retries: process.env.CI ? 2 : 0 report shows: "flaky" = failed, then passed track flake rate per test treat every "flaky" as a bug zero retries locally, so you see flakes while developing
Common mistakes
Responses that make things worse. Adding longer sleeps, which makes the suite slower and still flaky. Re running until green. Setting retries to five and forgetting. Quietly deleting the test. Saying it is just the environment without evidence. Quarantine without an owner or a date. And blaming the tool instead of the test design. Most flakes come from how the test was written, which is good news: you can fix them.
- adding longer sleeps
- re-running until green
- retries: 5 and forget
- deleting the test quietly
- "it is just the environment" with no evidence
- quarantine with no owner
- blaming the tool, not the test design
Say it like this
In the interview: first I prove it is flaky, by repeating the test and running the suite in random order. Then I find the cause from traces and logs, usually timing, shared data, the environment or time. I fix the cause: wait for conditions instead of sleeping, isolate data, stub third parties, inject the clock. If I cannot fix it today, I quarantine it with a ticket and an owner, and we track flake rates so they go down.
- "First prove it: repeat runs
- and random order. Then find
- the cause from traces and logs: usually timing, shared
- data, environment or time.
- Fix it: wait for conditions, isolate data, stub third
- parties. If not today, quarantine with an owner, and track flake rate."