How I systematically investigate a flaky test
A flaky test is investigated as a search for the cause: reproduce the raw failure rate without retries and narrow the hypotheses one by one. The sources are few: time, shared state, order, environment and real application races; compare --workers=1 with several workers and a red trace with a green one. Until fixed, the test is quarantined with an owner and a deadline; force: true and long pauses cure nothing.
Playwright · Debugging
A test fails once in ten runs and the code has not changed. Where do you start?
The payment test passes nine times out of ten. The team already reruns the pipeline out of habit, and red is no longer taken as a signal. That is the real harm of flakiness: not one red run but the loss of trust in the whole suite. "Just rerun it" is not an answer; an answer is a process that leads from a symptom to a cause in predictable time. Below is such a process and an example where a second cause remains after the first is fixed.
How it works and why. A test is unstable when its result depends on something it does not control. There are few such sources: time (a response came earlier or later, an animation did not finish), shared state (data or an account touched by another test or worker), order (a test relies on leftovers of the previous one), the environment (CI is slower, a different font, time zone or browser version) and real races in the application itself. The investigation is built as a narrowing of hypotheses: each step rules out one source.
How the search for the cause narrows
Example: two layers of one problem. First reproduce without retries to see the raw failure rate:
npx playwright test checkout.spec.ts --repeat-each=20 --retries=0
npx playwright test checkout.spec.ts --repeat-each=20 --retries=0 --workers=1
npx playwright test checkout.spec.ts --repeat-each=20 --retries=0 --workers=4
npx playwright test -g "card payment" --repeat-each=30 --retries=0 --trace on
Suppose with one worker it fails in 3 runs out of 20, and with a shared account and four workers more. These are two signals. The first, failing even sequentially, points to waiting. Compare the trace of a red and a green run: in the red one the API response arrived in three seconds, in the green one in half a second. Meanwhile the test reads the status once:
// before: a fixed pause and a one-time check of the state
await page.getByRole('button', { name: 'Pay' }).click();
await page.waitForTimeout(2000);
expect(await page.getByTestId('order-status').textContent()).toBe('Paid');
// after: a web-first assertion that retries until the timeout
await page.getByRole('button', { name: 'Pay' }).click();
await expect(page.getByTestId('order-status')).toHaveText('Paid');
The first variant either wastes two seconds or does not make it when the backend is slower. The second waits exactly as long as needed. Likewise waitForLoadState('networkidle') is not recommended: applications with polling or analytics do not "go quiet". It is better to wait for a visible result or a specific response through page.waitForResponse.
The second layer: with four workers the test still fails sometimes, because all workers log in with one account and empty one cart. Data isolation per worker through a fixture:
export const test = base.extend<{ account: string }>({
account: async ({}, use, testInfo) => {
// an own account per worker: parallelIndex is stable within a run
await use('qa-user-' + testInfo.parallelIndex + '@test.com');
},
});
After both fixes repeat --repeat-each=20 on one and on four workers: zero failures on both. A single green run is not enough, because instability returns at random.
Typical causes and what to do.
| Symptom | Probable cause | Fix |
|---|---|---|
| An element is "not clickable" or covered | An animation, a spinner, a modal | Wait for the state with a web-first assertion, not force: true |
| Fails only in parallel | Shared data or account | Data per test or worker through a fixture |
| Passes alone, fails in the suite | Order dependence, leftovers | Independent tests, a clean state |
| Fails only on CI | A slow environment, a different font or time zone | The same image, explicit waits |
| Fails randomly on a value | Time-dependent or random data | Fix the time (page.clock) and the data |
The last cause matters: some flaky tests are real bugs that a user will also meet. If a double click creates two payments, it is not a test problem but a race condition in the application, and that is what must be fixed.
Quarantine. Until the cause is fixed, the test must not block the team. It is moved into a quarantine project with a tag that does not block the merge. The alternative test.fixme skips the test completely, whereas in quarantine it keeps running and gives statistics:
test(
'card payment',
{ tag: '@quarantine', annotation: { type: 'issue', description: 'QA-482' } },
async ({ page }) => {
// …
},
);
// playwright.config.ts
projects: [
{ name: 'stable', grep: /^(?!.*@quarantine)/ },
{ name: 'quarantine', grep: /@quarantine/, retries: 3 },
],
How not to block the team while a test is unstable
Raise retries for everything
Less red, but nobody sees the instability and the pipeline gets slower. A month later there are twice as many flaky tests.
Quarantine with a ticket, an owner and a deadline
The test does not block the merge, its failures are counted, and the owner knows when to return it to stable.
Limits and alternatives. The algorithm finds a cause only if the failure reproduces. If a test fails once in a hundred CI runs and never locally, you go for reproducing the environment (the same Docker image, CPU limits, parallelism as on CI) and for retain-on-failure, so that the trace of the first failure is not lost. For control at the level of the whole suite there is the failOnFlakyTests option, which makes the run red if any test passed only after a retry: turn it on when the team is ready to tolerate no flakiness. A metric worth tracking is the share of flaky tests in runs: the HTML report marks such a test separately. In Cypress a similar role is played by cloud detection of flaky tests, in Selenium you have to find flaky tests from CI history yourself.
Common mistakes.
- Raising retries instead of looking for the cause: the instability stays, but the report is silent.
force: trueor long pauses as a "cure": the test stops checking what a user sees.- Starting an investigation with
--retries: a retry hides the failure you are looking for. - Quarantine without an owner and a deadline: half a year later it holds hundreds of tests nobody checks.
- Fixing the symptom in the test when the cause is in the application: the bug reaches users.
Check yourself
A test passes alone through -g but fails in the full suite. Which hypothesis do you check first, and how?
Answer
Order dependence or leftover data: a previous test leaves a state this one relies on. Check it by running the test after a specific neighbour, looking at the data in the trace, and make the tests independent (a clean state per test through a fixture).
With --workers=1 the test is green 20 times, with --workers=4 it turns red. What does that say about the cause?
Answer
The problem is shared state between workers: the same account, data or file. Each worker needs its own data, for example through a fixture with testInfo.parallelIndex. Longer waits will not help here.
Why is the investigation run with --retries=0 although retries are on in CI?
Answer
A retry hides the first failure: the test is marked flaky and passes. To see the raw failure rate and compare changes before and after a fix, retries are turned off for the duration of the investigation.
--repeat-each, look at the trace of the failure and look for the cause, such as an unneeded pause or shared data. Middle/Senior: I narrow hypotheses: the raw failure rate without retries, then isolation by changing the number of workers, order by running the test alone, a comparison of the trace of a red and a green run. I classify the cause: the test, data, environment or a real race condition, because some flaky tests are bugs a user will see. Until it is fixed the test goes into quarantine with a ticket, an owner and a deadline, and I track the share of flaky tests separately.