Flaky tests: why they happen, and how to tell a broken site from a broken test

Why a test passes, then fails with nothing changed: timing, shared test data, the network and the device. How to sort each failure, and fix the test or the site.

Checked 1 December 20264 min read

A flaky test passes, then fails, then passes again, with nothing changed in between. One flaky test is a nuisance. A few of them teach a whole team that red means "run it again", and then the real failure, the day the site breaks, gets run again too.

Every flaky result has one of two causes: the site really does fail some of the time, or the test, or what it runs on, does. The work is telling them apart.

Sort the failure first

Before you change any code, put the failure in one of three places:

Where it belongsWhat it looks like
The siteThe same step fails on more than one device, or a page shows an error of its own.
The testThe step looked too early, or for words that changed, or for data another run used.
The labThe phone was locked, the browser didn't start, the network dropped. Nothing about the site was judged.

Only the first is your site's problem. The second is a test to fix. The third says nothing about the site at all, and counting it as a failure is how good tests get a bad name.

Timing: the test looked too early

The most common cause. The page is still loading, a request hasn't come back, or an animation hasn't finished, and the test checks for something that will be there a moment later. On a fast laptop it's always there in time; on a mid-range phone on a slow connection it isn't.

Fix: wait for what a person waits for: the words or the button that only appear once the step is done. Never a fixed pause: two seconds is too long on your laptop and too short on an older phone.

Test data that a run used up

A flow that signs up test@example.com passes once. The next run finds the address taken and fails. Two runs at once, on two phones, can do the same to each other: one empties the basket while the other is checking it.

Fix: give each flow data it can't collide on, or have it put things back as it found them: whatever it makes, it removes at the end. Run it twice in a row before you trust it. Test accounts explains how to keep accounts you can reset.

Words and selectors that move

A test that finds a button by its position, or by a class name the build tool generates, breaks every time the page is rearranged, and the site works fine. A test that finds it by its words breaks only when a visitor would be confused too.

Fix: find things by what a person sees: the button's words, the heading, the field's label. When the words change on purpose, change the test in the same pull request.

The network and the outside world

Ads, chat widgets, analytics, a payment provider's test mode, a map: each is someone else's server, and each has slow days. A test that waits for all of them fails when any of them does.

Fix: decide which outside services the flow is really about. Wait for and check those; for the rest, check your own page around them. If the payment step is what you're testing, its outage is a real failure and worth knowing.

The device itself

A phone that went to sleep, an update that needs a restart, a low battery, a browser left with twenty tabs open: on a real device these happen, and they look like failures if nothing tells them apart.

Keep the history, not only the last result

One failure tells you little. The same check passing, failing, passing, failing over its last ten runs tells you it's flaky. A check that passed for weeks and fails since Tuesday's deploy tells you something changed.

Fix it or remove it

A flaky test you keep re-running costs more than no test: it takes time every day and it trains people to ignore it. When one is flaky, find which of the causes above it is and fix that. If you can't, remove it, and write the check again from what a person would see.

Related guides