Flaky tests: common causes and how to fix them
Flaky tests pass and fail without any code change. Learn the most common causes in web app testing, how to find them and practical ways to fix them for good.
A flaky test is a test that sometimes passes and sometimes fails when nothing in the code has changed. Flaky tests are expensive: people re-run builds, stop trusting red results, and real bugs slip through because "it is probably just flaky".
The most common causes
Timing and waiting
The number one cause in web testing. The test clicks a button before it is ready, or checks text before the page has finished loading. Fixed delays ("wait 2 seconds") make it worse: too short on a slow machine, wasted time on a fast one.
Shared or leftover test data
Tests that depend on data created by other tests, or that run in parallel against the same account, fail depending on the order they run in.
Unstable selectors
Selectors based on long CSS paths or generated class names break when the layout changes slightly. Tests should target stable attributes such as roles, labels or dedicated test IDs.
External services
Payment providers, email services and third-party APIs can be slow or rate-limited in test environments. A timeout there looks like a bug in your app.
Environment differences
Different browsers, screen sizes, time zones or locales can change behavior. A test that passes on a developer laptop may fail in a headless browser in CI.
How to find flaky tests
- Track the pass/fail history of every test, not just the latest run.
- Re-run failed tests automatically once and record when the retry passes; that test is likely flaky.
- Look for patterns: the same test failing at the same time of day, in the same browser, or only in parallel runs.
How to fix them
- 1Wait for conditions, not time. Wait until an element is visible, enabled or until a network request finishes.
- 2Isolate test data. Each test creates what it needs and cleans up after itself.
- 3Use stable selectors such as accessible roles, labels or test IDs.
- 4Stub or mock unreliable external services in most tests, and keep a few end-to-end tests that use the real ones.
- 5Quarantine, then fix. Move a known flaky test out of the blocking suite, open a ticket, and fix the root cause instead of adding retries forever.
How reporting helps
Flaky tests are much easier to deal with when results are collected over time. Tebugging uses AI-powered analysis to identify patterns and predict failures across test runs, and gives actionable recommendations, so recurring problems stand out instead of getting lost in individual runs.
Writing better tests from the start helps too. See our guide on how to write a test plan for a web application.
Frequently asked questions
What percentage of flaky tests is acceptable?+
Aim for as close to zero as possible. Even a few percent of flaky tests means most full test runs will contain at least one false failure.
Should I just add retries to flaky tests?+
Retries are a short-term workaround. Record every retry and fix the underlying cause, otherwise flakiness grows and real failures get ignored.
Plan, run and analyze your web tests with AI.
Start on the free plan. No credit card required.
Start Testing Free