In brief
A flaky test is one that passes on one run and fails on another with the same code. The most common causes are dependence on time and order, shared data, not waiting properly for the application state, and an unstable environment. Getting them under control takes three things: detection through result history, quarantine with an owner and a deadline, and fixing the cause rather than just rerunning the test. Without that, the team stops trusting red results.
Why flaky tests are more dangerous than no tests
A test that fails at random teaches a team to ignore failures. After a few weeks the reaction to a red run is “run it again”, and a real defect gets lost in the noise. The cost is double: the time lost on wasted runs and analyses, and the defects that slipped through because someone took a failure for just another fluke.
The problem grows with the number of tests. If each of several hundred E2E tests fails by chance once in a few hundred runs, a full run will almost always have at least one failure unrelated to the code. That is why flakiness mainly affects the upper layers of the test pyramid, where a browser, network, database and several services sit in the path.
Where flaky tests come from
The causes repeat in every project:
| Cause | What it looks like | Typical fix |
|---|---|---|
| Waiting a fixed time | `sleep(2000)` before clicking a button that appears after one second one time and after three the next | Waiting for a condition: element visible, request completed, state in the database |
| Shared data | Two tests use the same account and one changes the balance the other checks | Own data for each test, created before the test and cleaned up after it |
| Order dependence | A test passes only after another test that sets up state | Independent tests, random order in CI to reveal dependencies |
| Time and time zone | A date test fails at midnight, on the last day of the month or at a clock change | An injected clock, a fixed date in the test |
| Asynchrony | An assertion on an event processed by a background queue | Polling with a timeout instead of a single check |
| Environment | A slow test database, an overloaded CI server, an unavailable external system | Mocks of external systems, CI resources, a separate environment for E2E tests |
| Selectors | A locator based on element position or a generated CSS class | Test attributes, accessibility roles and labels |
In bank and operator systems there is often one more reason: integration with a partner's system on a test environment that has its own maintenance windows and limits. A test that calls a real external system is flaky by definition, because its result depends on someone else's infrastructure.
How to detect flaky tests
A single run will not show whether a test is flaky. You need history:
- recording each test's results over time. A test that has both failures and passes on the same code version is flaky. Most CI systems and test reports can collect such data;
- automatic retry on failure, but with labeling. According to its documentation, Playwright divides tests into three groups: passed on the first try, “flaky”, meaning failed and then passed on retry, and failed. Such labeling turns a retry from hiding the problem into measuring it;
- deliberate repeated execution. A new or changed test can be run dozens of times in a loop before merging. If it fails even once, it goes back to the author;
- random order. Running tests in random order and in parallel quickly reveals hidden dependencies between them.
A retry without labeling is a trap. A test that passes on the third attempt looks green in the report, and the problem grows silently.
Quarantine and repair
A detected flaky test goes into quarantine: it still runs, but its result does not block merging or release. Quarantine works only with two rules:
- Every test in quarantine has an owner and a deadline. Without that, quarantine becomes a graveyard of tests, and the area they protected is left unchecked.
- The number of tests in quarantine is visible and has a limit. When it exceeds a set threshold, the team stops new automation work and fixes old tests.
Repair starts with collecting evidence from the failed run: a screenshot, a recording, application logs, a network request trace. Tools such as Playwright record an execution trace with every test step, which lets you reproduce a failure without rerunning it. Only with this data can you tell a test defect from an application defect.
Keep in mind that some flaky tests point to a real defect. A race condition in the application that shows up once in a hundred CI runs will also show up for customers, just at higher traffic. Before a test is fixed by adding a longer wait, check whether the application behaves correctly.
How to write tests that do not wobble
A few rules prevent most problems:
- wait for state, not for time. Modern E2E tools, including Playwright, have built-in waiting for an element to be ready before an action. With Selenium WebDriver you need to use an explicit wait for a condition;
- every test prepares its own data through the API or directly in the database, not by clicking through the interface;
- replace external systems with mocks in functional tests, and check the real integration with a separate, small set;
- the application clock should be injectable, so a test can set the date;
- base selectors on roles, labels or test attributes, not on page structure;
- move checks lower. A rule verified by a unit test will not be flaky.
The choice of tool matters too, because they differ in built-in waiting and isolation. The comparison is covered in the article Playwright, Cypress or Selenium, and an example of E2E tests in a CI pipeline in the case study Test automation for a B2B SaaS product in a multi-customer pipeline.
Questions and answers
Does automatically retrying failed tests help?
As a measurement yes, as a cure no. A retry that marks the test as flaky gives you data on the scale of the problem and keeps a random failure from blocking the team. A retry without labeling hides the problem and lets through defects that show up irregularly. A reasonable practice is one or two retries in CI, a flaky test report after each run and an obligation to fix.
What level of flakiness is acceptable?
The goal is zero tests that fail for no reason, because each one weakens trust in results. In practice the team sets a threshold above which fixing flaky tests takes priority over new tests. More important than a specific number is that flakiness is measured, visible and has an owner.
Can a flaky test just be deleted?
You can, if it checks something already covered by another test, or something of little importance. If it protects a critical path, deleting it leaves a gap. Then it is better to rewrite it at a lower level, for example replace an E2E test with an API test and one smoke test, than to leave the area unchecked. The decision to delete is worth recording together with the justification.
Who should fix flaky tests?
The test's author or the team that owns the area. Passing fixes to a single automation engineer or a separate quality team means authors do not learn from mistakes and repeat the same patterns. A good practice is the rule that a flaky test goes back to the team that wrote it, with a fix deadline like any other defect.