How to measure flaky tests and identify instability patterns
Updated:
When a test fails on one occasion and passes on another, you need to know the conditions of each run. A final green report after a retry can hide the first failure. Measurement therefore starts with individual attempts, their context and clearly defined metrics.
An unstable or flaky result is not a diagnosis of a test defect. The cause may lie in the application, waiting logic, shared data or environment. Preserve the evidence and distinguish these possibilities before deciding on a repair or temporarily separating the test.
What exactly are you counting?
Distinguish a test scenario, its execution and its individual attempts. A sign-in scenario run in three browsers has three executions. If one fails and is retried, that adds an attempt rather than a new scenario. Repeated runs also need a run and project identifier.
Agree on a measurement unit first. A practical choice is the first attempt of a particular test in a particular project and run. Attach retries to that same execution. Report unique unstable scenarios separately so that one frequently executed test does not look like a large group of problems.
A name alone may not identify a test uniquely. Playwright TestCase computes an ID from the file name, test title and project, and guarantees uniqueness within a session. For longer histories, also record renames and moves. Otherwise, history may split across two records or merge unrelated scenarios.
Three percentages that describe different things
The following example is hypothetical, rather than a measurement of a real suite. Over a chosen period, you have 1,000 completed first executions of tests expected to pass. Forty first attempts fail. All forty are retried under the same policy, and thirty eventually pass.
| Metric | Calculation | Meaning |
|---|---|---|
| Failed first-attempt rate | 40 / 1,000 = 4% | How often the first attempt failed |
| Executions that passed on retry after failure | 30 / 1,000 = 3% | How many executions succeeded only after retrying |
| Failed executions recovered by retry | 30 / 40 = 75% | What happened to the initially unsuccessful group |
The 75% figure does not mean that three quarters of the whole suite are unstable. Nor does 3% describe the proportion of unique tests. One scenario may have run many times during the period. Each metric needs its own denominator, the count from which the percentage is calculated.
Retries are not a randomly selected sample. They usually go to unsuccessful tests, and another attempt may use different data or already warmed-up services. Adding retries can improve the final success rate without improving first attempts. Track the retry count and the time it adds to the pipeline too.
Separate report classification from cause
Playwright classifies a test as flaky when it passes on retry after failing. That describes the sequence of results. It does not establish that the defect is harmless or belongs to the test. An intermittent application defect may have serious consequences for users.
Do not count an intentionally expected failure as an unexpected failure. A test marked test.fail() can be considered correct by the runner precisely because it fails. TestResult also distinguishes an attempt’s actual status, such as failed, timedOut, skipped or interrupted, from the test’s overall outcome.
Show these groups separately in reporting:
- skipped tests and the reason they did not run,
- interrupted or cancelled executions without a complete result,
- setup failures that blocked further scenarios,
- confirmed infrastructure outages and undetermined cases,
- quarantined tests outside the blocking suite.
A timeout may count as a failed first attempt, but it still needs a diagnosis. If a job was cancelled before the test reached an outcome, do not automatically report it as a scenario defect. Document the inclusion rules and keep them consistent between comparisons.
What to record for each attempt
Store the test and run identifiers, attempt number, actual and expected outcome, start time and duration. Add the application and test commits, browser, environment version, worker count and data profile. An appropriate test-data identifier is enough, without recording personal details or passwords.
Diagnostic evidence should show the failure location, message, last actions and available log, screenshot or recording of the run, known as a trace. Link it to the specific attempt. A successful retry’s trace does not replace a missing recording of the first failure. Set access controls and retention for these artefacts too.
Include the period, execution count and retry policy in the summary. The same percentage from ten executions and from a thousand does not provide the same evidence. Even a failure-free sequence cannot guarantee zero risk, especially if it did not include the troublesome concurrency or data profile.
How to investigate a suspect test
1. Fix the conditions
Choose a specific commit, configuration and prepared data. Repeat the test under comparable conditions and retain every attempt. Reset data so that a successful first run does not leave a different situation for the next. If you cannot ensure this, identify the difference in the measurement.
2. Change one condition
Compare a standalone run with the complete suite running concurrently. You can then compare order, worker count or a particular browser. Passing alone and failing under concurrency suggests a conflict but does not establish where it occurs. Look for a shared account, record, capacity constraint or timing-dependent application behaviour.
Local and CI environments have their own differences. The investigation is covered in tests pass locally but fail in CI. Other causes are explained in random test failures.
3. Group evidence and choose the next action
Ten failures may stem from one unsuccessful sign-in. The same final message can also hide two causes. Check a representative test and a member that differs from the rest. Keep undetermined cases visible.
The next action may be a test or data repair, application investigation, environment stabilisation or justified temporary quarantine. Before deleting a test, confirm its purpose and replacement coverage. A longer timeout is appropriate only when it matches the operation’s expected behaviour. It cannot fix waiting for the wrong signal.
How to prioritise repairs
Consider frequency, the impact of the protected process, blocked releases and manual investigation time. A rare payment failure may take priority over a frequently failing, less important report. A universal percentage below which every suite is acceptable has little meaning without these conditions.
Quarantine needs an owner, a reason, a review deadline and visible results from a separate run. Arrange replacement checks for critical risks. Simply omitting a test may improve the failure rate while removing your ability to detect a defect.
How to verify a repair
After the change, compare the same scenarios, environment, data profiles and counting rules. Track first failures, successful retries, retry time and remaining coverage. If the application version or concurrency changed, state that and separate the comparison.
One successful run checks a particular attempt. Repeated measurement, including the conditions that previously triggered the problem, provides stronger evidence. Also check that the repair did not weaken the outcome assertion. A green test after removing an assertion does not confirm stabilisation.
Start with a small overview of first attempts and retries over a chosen period. Select a suspect test with meaningful impact, confirm its cause and repeat the same measurement after repairing it. This gives you both a repair order and evidence of what changed.