Back To Blogs

How to Fix Flaky Tests: Causes, Diagnosis and Cures

A flaky test passes and fails on the same code. One flaky test is an annoyance; ten of them and the team starts ignoring red builds, which defeats the purpose of automation. Fixing flakiness is one of the most valuable things an automation engineer can do.

Step 1: Prove it's flaky

Before fixing anything, reproduce it:

# Playwright: run one test many times
npx playwright test cart.spec.ts --repeat-each=50 --workers=4

Note the failure rate and collect artefacts from failures: traces, screenshots, videos, logs. A test that fails 1 in 50 runs needs a different investigation from one that fails 1 in 3.

Step 2: Find the cause

1. Timing (the most common)

Symptoms: "element not found", "element not clickable", assertions on stale values. It fails more on slow CI machines.

Causes: hard waits that are sometimes too short; asserting before data has loaded; clicking during an animation or re-render.

Fixes:

  • Replace sleep() with retrying assertions (await expect(locator).toHaveText(...))
  • Wait for the condition you need, like a network response or a specific state, not an arbitrary time
  • Assert on the final state the user sees
// ❌ flaky
await page.click('#apply-coupon');
await page.waitForTimeout(1000);
expect(await page.textContent('#total')).toBe('$27.00');

// ✅ waits for the condition
await page.getByRole('button', { name: 'Apply' }).click();
await expect(page.getByTestId('cart-total')).toHaveText('$27.00');

2. Shared state and test data

Symptoms: passes alone, fails in the full suite or in parallel.

Causes: tests share an account, a cart or a database row; one test's leftovers break another.

Fixes:

  • Each test creates its own data (unique emails, fresh carts)
  • Use isolated browser contexts and separate accounts per worker
  • Clean up, or better, design tests that don't care about leftovers

3. Order dependence

Symptoms: fails when run alone or in a different order.

Fix: no test should rely on another having run. Randomise the order occasionally to expose hidden dependencies.

4. Environment instability

Symptoms: many unrelated tests fail at once; timeouts on third-party calls.

Causes: an overloaded test environment, a slow shared database, real third-party services (payments, email), deploys during test runs.

Fixes:

  • Mock or stub third-party services in most tests; keep a few contract tests against the real ones
  • Give CI its own environment, or at least its own data
  • Health-check the environment before the suite starts

5. Brittle locators

Symptoms: breaks after unrelated UI changes, or matches the wrong element sometimes.

Fixes: use role, label or test-ID locators, and make locators unique. See our locator strategy guide.

6. Real bugs that are intermittent

Sometimes the test is fine and the app is flaky: race conditions, double-submit bugs, caching. Before "fixing" the test, ask whether a user could hit the same failure. Intermittent failures around checkout or booking are often real defects.

Step 3: Manage flakiness as a team

  • Quarantine: move known-flaky tests to a separate, non-blocking job with a ticket and an owner
  • Measure: track the flake rate per test; fix the worst ones first
  • Use retries carefully: one retry to collect a trace is fine, but retries that hide flakiness permanently are not
  • Set a rule: a new test must pass many repeated runs before it joins the blocking suite

A quick diagnosis table

SymptomLikely cause
Fails on CI, passes locallyTiming, environment speed, missing data
Fails only in parallelShared accounts or data
Fails only when run aloneOrder dependence
Many tests fail togetherEnvironment or third-party outage
Fails after UI tweaksBrittle locators
Same flow fails for real users tooAn actual race condition in the app

Build better instincts

Flaky tests often reveal real timing bugs like double submits and stale totals. Practise spotting those by hand in QAUnlocked Practice Labs, where the store lab hides a duplicate-order defect that only appears when you submit twice, then think about how a stable automated test would catch it.

flaky teststest automationCIreliability

Comments

0 Comments

Be the first to comment.