Flaky Tests: 9 Root Causes and a Practical Fix Strategy

A flaky test produces different results without a relevant change to the code or test conditions. It may pass on a rerun, fail in CI but pass locally, or break only when the suite runs in parallel. The cost is larger than one red build: every unexplained failure weakens trust in the whole test suite.

Why flaky tests are a delivery problem

When engineers expect failures to be noise, they spend less time investigating them. Teams start rerunning pipelines, ignoring alerts, or adding broad retries. Eventually a real regression can look like another transient failure. The objective is not to achieve a perfect-looking dashboard. It is to make every failure worth investigating.

Nine common root causes

  1. Timing assumptions. Fixed sleeps assume the application will always respond within a chosen number of milliseconds. Prefer waiting for a meaningful state or condition, with a bounded timeout.
  2. Shared mutable data. Tests update the same user, order, or record. Parallel execution then creates collisions. Give each test unique data or isolate resources.
  3. Order dependencies. A test passes only because another test created state first. Make setup explicit and verify tests can run independently.
  4. Uncontrolled network dependencies. External services, rate limits, and unstable third parties introduce failures unrelated to the feature. Stub deterministic boundaries where appropriate, and keep a smaller set of real integration checks.
  5. Environment drift. Local browsers, CI images, dependencies, time zones, and feature flags differ. Pin versions where practical and document environment assumptions.
  6. Weak selectors. Tests depend on generated classes, layout position, or text that changes frequently. Prefer stable, user-facing locators such as accessible roles and labels.
  7. Incomplete cleanup. Tests leave sessions, queues, files, or records behind. Cleanup should be reliable and should not depend on a later test.
  8. Resource contention. Excessive parallelism can overload a database, runner, or shared service. Measure throughput and failure rates as concurrency changes.
  9. Unclear assertions. Assertions check transient intermediate states or implementation details. Assert the outcome that matters to the user and wait for the application to reach that state.

Measure flakiness before changing everything

Start by collecting a stable baseline. Track the number of test executions, failures, retries, and final outcomes over time. One useful operational measure is the flaky-test rate: tests that show inconsistent outcomes under comparable conditions divided by tests executed in the observation window. Define the denominator clearly and avoid mixing infrastructure outages with product failures.

Also track:

A practical remediation workflow

Step 1: Capture evidence

Keep traces, screenshots, video where useful, console output, network logs, and environment metadata. A failure without context is expensive to reproduce.

Step 2: Classify the failure

Separate product regressions, test-code defects, test-data collisions, environment problems, and external dependency failures. Do not label every intermittent failure as an infrastructure issue.

Step 3: Reproduce under controlled conditions

Run the test repeatedly, then vary one factor at a time: order, worker count, browser, environment, or test data. This helps distinguish a race condition from a deterministic defect.

Step 4: Fix the cause and add a guardrail

Replace fixed sleeps with condition-based waits, isolate test data, stabilize selectors, or constrain concurrency. Add a regression check for the failure mode when possible.

Step 5: Quarantine transparently

If a test must be quarantined, record an owner, reason, issue, and review date. Keep it visible in reporting and prevent quarantine from becoming permanent storage for neglected tests.

What about retries?

Retries are useful for gathering evidence and reducing the impact of a known transient infrastructure issue. They are not a fix for nondeterministic tests. Report the first attempt separately from the final result, alert on rising retry rates, and never let a retry erase the original failure from the record.

For framework-specific guidance, see the Playwright documentation on retries and Trace Viewer.

Prioritize by impact

Fix tests that block the main branch, cover revenue-critical journeys, or create frequent false alarms first. A small number of high-noise tests can consume more engineering time than a much larger set of stable checks. Review the suite as a system, not as a list of isolated red rows.

If flaky failures have become routine, a focused QA automation review can map the causes across test design, data, and CI and turn them into a prioritized 30-day plan.

QA AUTOMATION HEALTH CHECK

Know what to improve first.

Get an independent review of test reliability, automation architecture, and CI with prioritized findings and a practical 30-day plan.

Explore the review →