Key takeaways
Fix every flaky test at its cause instead of re-running it until it passes.
A re-run leaves the cause in place, and once red builds are often noise, developers start re-running real failures too.
Wait for what a user would see before each step, instead of a fixed amount of time.
Timing is the most common cause of flaky tests, and a hard-coded wait is too short whenever the app is slower than usual.
Give every test its own data and a clean starting state.
Tests that share accounts or records, or need another test to run first, fail whenever they run in a different order or at the same time.
Retry a failed test once to spot a flake, and record every test that passes only on retry.
One retry on the same code tells a flake from a real failure, and the record lets you give each flake an owner instead of counting it as a pass.
Run a new or fixed test dozens of times before you trust it.
A test that flakes once in 20 runs usually passes a single run, so only repeated runs show the flake.
A test fails on a pull request, the developer re-runs it without changing a line, and it passes. With flaky tests in a suite, every failure has to be checked before anyone knows whether it means anything.
A flaky test is an automated test that passes on some runs and fails on others against the same code, because something besides the code changes between runs, such as timing, test data or the test environment. Flaky tests are most common in end-to-end tests, which run whole user flows through the app's real interface and depend on everything behind it. The fix is to find what changes between runs and remove it, since a re-run that passes leaves the cause in place.
What is a flaky test?
A flaky test is a test whose result changes from run to run while the app and the test stay the same. It can pass nine runs in a row and fail on the tenth. A flaky test is often called a flake, and its flake rate is the share of its runs that fail this way.
A test that fails on every run is different, because it points to a real change, either a bug in the app or a test that's out of date because the product changed on purpose. A flaky test fails only some of the time, so a single failure can't tell you whether it's a bug, an out-of-date test or a flake.
What is an example of a flaky test?
A checkout test that clicks Pay, waits one second and then checks for "Order confirmed" is a flaky test. If the payment reply takes anywhere from 0.3 to 1.7 seconds, the test passes when the reply is fast and fails when it's slow, about half the time. Checkout works on every run, and the test fails only because it reads the page before the reply arrives.
Other common examples are a test that passes alone but fails when another test changes the same account first, a test that fails whenever an outside API is slow to answer, and a mobile test that fails because an earlier test already granted a permission the app was supposed to ask for.
What flaky tests cost a team
Flaky tests cost a team its trust in the test suite, along with the time spent re-running and re-investigating failures.
Google reported on its testing blog in 2016 that about 1.5% of all its test runs gave a flaky result and almost 16% of its tests had some flakiness. Of the times a test went from passing to failing after a change was submitted, about 84% involved a flaky test.
- Real bugs get waved through. When most failures are flakes, a real failure looks like one more, and it gets re-run until it passes.
- Merges and releases wait. Every red build needs someone to re-run or investigate it before the change can ship.
- The same investigation happens again and again. Without a record of past flakes, each one is investigated from scratch by whoever hits it next.
- Re-runs take up CI time. Each re-run repeats the slowest part of the pipeline in CI, the continuous integration system that runs tests on every change.
What causes flaky tests?
Flaky tests are caused by something other than the code under test that changes from one run to the next. The six most common causes are:
- Timing and race conditions
- Shared test data and test order
- Unstable test environments
- Third-party services
- Resource contention in parallel runs
- Leftover device and app state on mobile
1. Timing and race conditions
Timing is the most common cause of flaky tests. A race condition is a result that depends on which of two things finishes first. In a test, it's usually a race between the test checking the page and the app finishing an update. The test clicks a button before it can be clicked, reads a value while an API call is still pending, or looks for an element that hasn't rendered yet. Fixed waits make it worse, since a one-second sleep is too short whenever the app takes longer. In QA Wolf's data, timing causes about 30% of test failures, ahead of changed selectors (how a test finds an element on the page) at about 28%. QA Wolf's guide to self-healing test automation breaks down the rest.
2. Shared test data and test order
Tests that share an account, a record or a database row interfere with each other. A test can pass alone and fail when another test runs first and changes the cart, signs the shared user out or buys the last item in stock. Tests that rely on an earlier test to set up their state fail whenever the order changes.
3. Unstable test environments
A correct test fails when the environment it runs against misbehaves, such as a staging server that slows down under load, a test database that falls out of sync, a deploy that lands in the middle of a run, or a network that drops a request.
4. Third-party services
Payment providers, sign-in services, email and SMS services and other outside APIs can time out, limit test traffic or answer slowly, and a test that depends on them fails with them.
5. Resource contention in parallel runs
Parallel testing runs many tests at once. When those tests share machines, browsers or database connections, they compete for CPU and memory, and the slowest ones time out.
6. Leftover device and app state on mobile
Mobile tests flake when a device keeps state from the previous test, such as a permission already granted, a cached feature flag or a signed-in session, or when backend data hasn't synced by the time the test checks the screen. QA Wolf resets the device and the app, and seeds fresh test data, before every mobile test. Android emulators run reliably side by side when each one gets its own CPU and memory and has the features tests don't use turned off, two of QA Wolf's five strategies for stable Android emulators.
How do you detect flaky tests?
A test that fails and then passes when it runs again, on the same code and environment, is flaky. To catch flaky tests reliably:
- Retry each failed test once. Run it again on the same commit and environment, and if it passes, record it as a flake instead of a pass. Playwright marks these tests as flaky in its report.
- Keep every test's result history. A test that both passed and failed on the same commit is flaky, even if its last run passed. Each test's flake rate shows which ones to fix first.
- Run new and changed tests many times before they merge. A test that flakes once in 20 runs usually passes a single run, so running it dozens of times in a row is what catches it.
- Run a failed test again with fewer tests at once. If it passes on its own, shared data, test order or resource contention caused the failure.
A test that fails on every retry points to a real change instead, either a product bug or an out-of-date test. Its trace (a step-by-step recording of the run), video and logs show which one. A product bug shows the app doing the wrong thing, while an out-of-date test looks for something the product changed on purpose.
How do you fix flaky tests?
Fix a flaky test by finding what changes between its passing and failing runs and removing that cause, then running the test many times to prove the fix. Compare a failed run's trace, video, logs and network activity with a passing run's to find the cause. QA Wolf's AI agents read the same kind of evidence, including the test's earlier failures and its Git history, before they change a test.
Wait for the app's state instead of a fixed time
For timing flakes, replace each fixed wait with a wait for what a user would see, such as an element appearing, a message changing or a network response arriving. Playwright's assertions keep checking until the page matches or a timeout passes, so the test moves on as soon as the app is ready.
Here's that checkout test, fixed, in Playwright and outlined in Arrange-Act-Assert steps. Its page stands in for a checkout whose payment reply takes 0.3 to 1.7 seconds.
import { test, expect } from '@playwright/test';
// A checkout page whose payment reply takes between 0.3 and 1.7 seconds,
// different on every run, like a real payment API.
const checkout = `
<button>Pay</button>
<p role="status"></p>
<p data-testid="total"></p>
<p data-testid="email"></p>
<script>
const show = (selector, text) => {
document.querySelector(selector).textContent = text;
};
document.querySelector('button').onclick = () => setTimeout(() => {
show('[role=status]', 'Order confirmed');
show('[data-testid=total]', '$42.00');
show('[data-testid=email]', 'Receipt sent');
}, 300 + Math.random() * 1400);
</script>`;
test('paying shows the order confirmation', async ({ page }) => {
// Arrange: open checkout
await page.setContent(checkout);
// Act: pay
await page.getByRole('button', { name: 'Pay' }).click();
// Assert: wait for the confirmation itself instead of a fixed time.
// A fixed wait, like page.waitForTimeout(1000), fails whenever
// the reply is slower.
await expect(page.getByRole('status')).toHaveText('Order confirmed');
// Soft checks for details nothing after them depends on,
// so one run reports every wrong detail
await expect.soft(page.getByTestId('total')).toHaveText('$42.00');
await expect.soft(page.getByTestId('email')).toHaveText('Receipt sent');
});
As written, the test waits as long as the reply takes, up to Playwright's five-second default. The confirmation is a hard check because there's nothing left to check if the order didn't go through. The total and the receipt message are soft assertions, checks that record a failure and let the test keep going, so one run reports every wrong detail.
Give every test its own data
Have each test create its own uniquely named account and records, delete anything an earlier run left behind before it starts, and never rely on another test running first. These are among QA Wolf's principles for writing tests that run in parallel, where shared data causes the most collisions.
Keep the test environment stable
Run tests against an environment that nothing else deploys to during the run, with its data reset to a known state and enough capacity for every test running at once. When several runs would share one environment, give each run its own, such as a preview deploy (a temporary copy of the app built for one pull request).
Use third-party services' test modes
Point tests at each provider's sandbox or test mode, such as a payment provider's test cards. Give each test its own email inbox, so messages sent to other tests don't get in the way.
Give each test its own browser or device
Run each test in its own container, an isolated copy of the operating system, with its own browser, device or machine, on infrastructure with enough capacity for every test at once.
Reset the device and app before every mobile test
Start every mobile test on a fresh device or emulator, or one wiped of the last test's permissions, settings, app data and sessions. Wait for backend data to sync before the test checks the screen.
Run the test many times to prove the fix
Run the fixed test dozens of times in a row before you trust it, for example 50 times with Playwright's --repeat-each=50.
Flaky test management: what your test system should do
Flaky test management keeps flakes from piling up by having the test system record every pass on retry as a flake, group failures by cause, and give every flake an owner who fixes it.
Record every pass on retry as a flake
Retry a failed test automatically, once or twice at most, on the same commit and environment, and log the retry with the result. A test that passes only on retry counts as a flake, so it gets fixed instead of disappearing into the passes. More retries make the suite slower and can hide real problems.
Group failures by cause
Log each failure with the test, the time, the error message and stack trace, the browser or device, and the commit or run ID. Then group failures with the same error and failing step into one signature, so 12 tests timing out on the same element show up as one problem. Track each test's flake rate and which signatures are growing.
Give every flake an owner who fixes it
Route each recurring flake by its cause. A problem in the test goes to whoever maintains the tests, a product bug to the team that owns the feature, and an environment problem to whoever runs the infrastructure. If a fix will take time, quarantine the test, which takes it out of the runs that block a merge, and give it an owner and a date, since a quarantined test checks nothing while it's out. Review old "won't fix" and "can't reproduce" flakes regularly, because they tend to come back.
How do you handle flaky tests in Playwright?
In Playwright, rely on its built-in waiting, retry a failed test once to spot a flake, and make the run fail when any test is flaky, so the retry finds flakes without hiding them. This configuration sets up the retry and the failing run, and keeps traces to compare:
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
// run a failed test once more to see if it's a flake
retries: 1,
// still fail the run when a test passes only on retry
failOnFlakyTests: true,
use: {
// keep traces of the failed attempt and the retry, to compare
trace: 'retain-on-failure-and-retries',
},
});
- Built-in waiting: Before it clicks or types, Playwright waits for the element to be visible, stable, enabled and able to receive events, and assertions like
toHaveTextkeep checking until the page matches. AwaitForTimeoutcall works around both, so look for one first when a Playwright test flakes. - Retries: With
retriesset, Playwright runs a failed test again and labels it flaky in the report if it passes. - Failing on flaky tests:
failOnFlakyTests, or--fail-on-flaky-testson the command line, fails the run when any test passed only on retry. - Traces: The
retain-on-failure-and-retriessetting keeps a trace of both the failed attempt and the retry, so the trace viewer can show where they differ. - Repeat runs:
npx playwright test --repeat-each=50runs each test 50 times, to check a fix.
How does QA Wolf handle flaky tests?
QA Wolf's AI agents investigate every failed test as soon as it fails, sort it into a product bug, an out-of-date test or a flake, and fix a flake's cause instead of re-running the test until it passes. Sign up and ask the agent to build tests for your most important flows, and from then on every failure in their runs gets investigated. You can also ask it why a run failed, or to fix a failing test, from Slack, Linear or GitHub. When an agent needs a question answered, such as whether a change was intended, it asks you. From QA Wolf's docs and engineering posts:
- Every failure gets investigated while the suite runs. QA Wolf's AI picks up a test the moment it fails, without waiting for the rest of the suite. Failures with the same cause go to one investigator, so one fix can clear several tests.
- A failed test runs again to rule out a flake. QA Wolf gives each test up to three attempts. The first runs with the whole suite at once, the second re-runs failures in batches of five, and the third runs what still fails one test at a time, which shows whether the environment caused the failure.
- Flakes get fixed at the cause. When a flake's cause is in the environment, the agent clears it up before it runs the test again. When the cause is in the test, such as a step that reads the page too early, the agent fixes the test.
- Real bugs are reported to you. The agent reproduces the bug in its own browser and files a single report for it, however many tests it broke, with a video of the reproduction and steps to reproduce.
- One run reports every failed check. The agent writes soft assertions for checks nothing later in a test depends on, and QA Wolf diagnoses each failure on its own, so failures with different causes become separate bug reports.
- The agents maintain the tests. When your product changes, they update the tests it breaks, with unlimited repairs.
An investigator can't loosen an assertion, change an expected value to match what the app shows, or add a fixed wait and call that a fix, and a repaired test has to pass before it counts as fixed.
Step 1 of 6Test 4, a checkout test, fails after its retries. The first agent on the scene becomes the lead investigator.
In QA Wolf's data on 127 recurring suites, the median time from every test having run to every failure having a conclusion fell from 2.3 hours with human investigation to 21 minutes with AI investigation (data through Oct 15, 2026). AI Investigation settled 75% of failing tests without anyone's help.
Metronome is a perfect example. With QA Wolf, its 400-plus end-to-end tests run in about 11 minutes with a flake rate under 1%, and it ships four fully tested production deploys a day.
On QA Wolf's full service, the same agents do the work and send their questions to QA Wolf's engineers instead of your team.
Start now and ask the agent to build tests for your most important flows, or talk to sales.
Frequently asked questions
What is a flaky test?
A flaky test is an automated test that passes on some runs and fails on others against the same code, because something besides the code changes between runs, such as timing, test data or the test environment. A flaky test is often called a flake.
What causes flaky tests?
The most common causes are timing and race conditions, shared test data and test order, unstable test environments, third-party services, resource contention in parallel runs, and leftover device and app state on mobile. Timing is the most common, as when a test checks the page before the app has finished updating it.
How do you fix flaky tests?
Compare the traces, logs and videos of a passing run and a failing run to find what changed, then remove that cause, for example by waiting for the app's state instead of a fixed time or giving each test its own data. Then run the test dozens of times in a row to prove the fix. Re-running a flaky test until it passes leaves the cause in place.
How do you reduce flaky tests?
Write tests that wait for what a user would see instead of a fixed time, create their own data, clean up before they start and can run in any order. Run every new test many times before it merges, and fix each flake when it first appears so flakes don't pile up.
How do you handle flaky tests?
Retry a failed test once on the same code and environment to see whether it's a flake, record every test that passes only on retry, and give each flake an owner who fixes its cause. If a fix will take time, quarantine the test with an owner and a date, since a quarantined test checks nothing while it's out.
How do you find and fix flaky tests in CI?
Turn on one retry in CI and record every test that fails and then passes on the same commit, since that's a flake. Make the run fail when any test is flaky, so flakes get fixed instead of retried forever, and run new and changed tests many times before they merge. In Playwright, the failOnFlakyTests setting fails the run on flaky tests.
How does AI detect and handle flaky tests?
AI can investigate a failure the way an engineer would, reading the test's traces, logs and history to tell a flake from a product bug or an out-of-date test, then fixing the cause. QA Wolf's AI agents investigate every failed test while the suite is still running, fix a flake's cause in the test or the environment, and report real bugs with a video and steps to reproduce.
How do you handle flaky tests in Playwright?
Rely on Playwright's built-in waiting instead of waitForTimeout, set retries to 1 so a test that passes on retry is labeled flaky, and keep traces of the failure and the retry to compare. Set failOnFlakyTests so the run still fails when a test is flaky, and check each fix by running the test many times with --repeat-each.
How do you fix flaky Selenium tests?
Replace fixed sleeps with explicit waits for the condition the test needs, such as an element becoming clickable, give each test its own data, and use locators that don't depend on page layout. Selenium leaves waiting to your code, while Playwright waits for elements on its own, so QA Wolf recommends Playwright for new suites.
How do you handle flaky mobile tests?
Start every test from a clean device and app, so no permission, setting, cached flag or signed-in session carries over from the last test, and wait for backend data to sync before the test checks the screen. QA Wolf resets the device and the app, and seeds fresh test data, before every mobile test.