Home / Insights / QA and Software Testing

QA and Software Testing

Flaky Tests: Finding, Fixing and Preventing Unreliable Automated Tests

A test suite that fails randomly teaches a team to ignore failures. This guide explains how to detect flaky tests, diagnose the usual causes, and set policies that keep an automation suite trustworthy.

TopicsQA and Software TestingWeb and SaaS Development

Key takeaways

  • A flaky test passes and fails on the same code. Its real cost is lost trust: teams start rerunning pipelines instead of reading failures.
  • Retries are a detection tool, not a fix. Track every test that only passes on retry and treat it as a defect.
  • Most flakiness comes from a handful of causes: timing and waits, shared test data, test order, environment instability and unstable selectors.
  • Quarantine flaky tests quickly, with an owner and a deadline, so they stop blocking releases without disappearing.
  • Prevention is mostly design: isolated data, explicit waits on application state, stable selectors and fewer end-to-end tests for logic that can be checked lower down.

Every team with a sizeable automated suite eventually meets the same problem. A pipeline fails, someone reruns it, and it passes. Nobody changed the code. After a few weeks of this, red builds stop meaning anything, real regressions slip through because “it’s probably that flaky test again”, and the investment in automation quietly loses its value.

Flaky tests are not a sign that automation was a mistake. They are a predictable maintenance problem with well-understood causes. This article covers how to measure flakiness, how to triage and fix it, and the team habits that stop it coming back. Examples refer to browser automation with tools such as Playwright and Cypress, but the principles apply to API and mobile suites too.

What a flaky test really costs

A flaky test is one that produces different results on the same code and environment. The direct cost is pipeline time spent on reruns. The larger costs are indirect:

  • Ignored failures. Once a team expects random failures, it stops investigating red builds, and genuine regressions reach production.
  • Slower releases. Release candidates wait while someone decides whether a failure is real.
  • Eroded confidence in automation. Stakeholders begin to ask for more manual regression “just in case”, which defeats the purpose of the suite.
  • Hidden product bugs. Some flakiness is real: race conditions or timing bugs in the application that users will eventually hit.

That last point matters. Before deciding a test is badly written, check whether it has found an intermittent defect in the product.

Detecting and measuring flakiness

You cannot fix what you do not see. Make flakiness visible before trying to reduce it.

Use retries as a signal

Most modern frameworks support automatic retries. Playwright, for example, classifies a test that fails first and then passes on retry as “flaky”, distinct from “passed” and “failed”. That classification is useful, but only if someone reads it. A retry policy that silently turns flaky tests green hides the problem.

Track results over time

  • Record every test result per run, not just the pipeline outcome.
  • Calculate a per-test flake rate: runs that passed only on retry, or failed and passed on identical code.
  • Review the worst offenders weekly in the same forum where you review bugs.

Run suspicious tests repeatedly

Running a single test many times in a loop, in parallel and in isolation, quickly shows whether it is order-dependent, timing-dependent or genuinely stable.

Common root causes and how to fix them

In our experience, most flaky tests fall into a small number of categories. The table below maps symptoms to likely causes and fixes.

CauseTypical symptomFix
Fixed sleeps and poor waitsFails on slower CI machines or under loadWait for an observable state (element visible, request finished) instead of a fixed time; use the framework’s auto-waiting assertions
Shared or leftover test dataFails when run after another test or in parallelCreate unique data per test; clean up or use disposable environments
Test order dependencyPasses alone, fails in the full suiteMake each test set up its own preconditions; randomise order to expose dependencies
Unstable selectorsBreaks after unrelated UI changesUse roles, labels or dedicated test IDs rather than CSS paths or text that changes
External servicesFails when a third-party sandbox is slow or downMock or stub third parties in most tests; keep a small set of real-integration tests separate
Environment instabilityMany unrelated tests fail togetherMonitor the test environment itself; fix resource limits, deployments mid-run or shared databases
Time, dates and time zonesFails at midnight, month end or in a different regionControl the clock in tests; pin time zones in CI
Real application race conditionsIntermittent failure that users can also reproduceLog a product bug; do not hide it in the test

For API suites, many of the same causes apply; our API testing strategy article covers contract and failure-handling tests that are less prone to timing issues.

A quarantine policy that does not become a graveyard

When a test is confirmed flaky, it should stop blocking releases, but it should not simply be deleted or skipped forever. A workable policy looks like this:

  1. Quarantine quickly. Move the test to a non-blocking job or tag it so failures are reported but do not fail the pipeline.
  2. Assign an owner. Every quarantined test has a named person and a ticket in the tracker.
  3. Set a deadline. If it is not fixed within an agreed period, the team decides explicitly: rewrite, move the check to a lower test level, or delete it and accept the coverage gap.
  4. Limit the quarantine. Cap the number of quarantined tests. When the cap is reached, fixing flakiness takes priority over writing new tests.
  5. Check the coverage gap. If a quarantined test covered a critical journey, add a temporary manual check to the release checklist.

Preventing flakiness by design

The most reliable suites are designed to resist flakiness from the start.

  • Push checks down the pyramid. Validate business rules with unit and API tests. Keep browser tests for genuine end-to-end journeys. Our guide to test automation frameworks discusses how to structure layers.
  • Own your test data. Seed data through APIs or fixtures per test rather than relying on a shared, long-lived dataset.
  • Agree selector conventions with developers. Stable test IDs or accessible roles are cheap to add while building a feature and expensive to retrofit.
  • Treat tests as code. Review them, refactor them and apply the same standards as production code.
  • Keep environments boring. Stable, reproducible test environments, ideally created from infrastructure as code, remove a whole class of failures.

Flakiness also interacts with how and when tests run in the pipeline. Continuous testing in DevOps explains where different test types fit.

When an outside review helps

If a suite has reached the point where nobody trusts it, a short independent review is often faster than incremental fixes. A useful review measures flake rates, groups failures by root cause, and recommends what to fix, rewrite, move or delete. PoliteTech’s QA and testing team builds and maintains UI and API automation in client pipelines and can start with a test strategy review of an existing suite.

Frequently asked questions

Should we turn on automatic retries in CI?

Retries are useful for detection, as long as tests that pass only on retry are reported and tracked as defects. Retries that silently turn failures green hide the problem.

Is it acceptable to delete a flaky test?

Sometimes. If the check can be done more reliably at the unit or API level, or the journey is low risk, deleting it can be the right decision. Make it a deliberate choice and record the coverage gap.

Are flaky tests always a test problem?

No. Some intermittent failures reveal genuine race conditions or timing bugs in the application. Investigate before assuming the test is at fault.

How do we stop new flaky tests appearing?

Review tests like production code, agree selector and data conventions, run new tests repeatedly before merging, and keep the quarantine visible and capped.

Sources and methodology

Guidance reflects PoliteTech delivery experience. The external references below informed specific points in this article.

  1. Playwright: Retries — Supported the classification of tests as passed, flaky or failed when retries are enabled, and retry configuration options.

Has your team stopped trusting its test suite?

We can measure flakiness across your suite, group failures by root cause and recommend what to fix, move or retire.