Flaky tests can make a healthy CI/CD workflow feel unreliable, but indiscriminate retries and permanent quarantine only hide the problem. This playbook provides a repeatable method to classify intermittent failures, collect useful evidence, quarantine tests with guardrails, assign ownership, and return repaired tests to the required pipeline.
Overview
A flaky test produces different results from the same code and environment without a meaningful change to the behavior under test. The cause may be timing, shared state, network dependence, resource contention, an unstable test dependency, or a defect that appears only under particular conditions. The important distinction is that “intermittent” does not mean “unimportant.” A flaky test can conceal a real regression, waste investigation time, and reduce confidence in every CI result.
Effective flaky test management treats each failure as an engineering signal. The goal is not simply to make the pipeline green. The goal is to determine whether the test, product, environment, or pipeline is responsible, then make the smallest safe change that restores trustworthy feedback.
Use this workflow whenever a test fails intermittently:
- Preserve the failed run and record its context before rerunning it.
- Classify the failure using evidence rather than intuition.
- Attempt a focused reproduction and separate product defects from test or infrastructure faults.
- Quarantine only when the risk is understood and an owner and review date are recorded.
- Repair, replace, or retire the test, then verify it in the required pipeline.
A retry may be useful as a diagnostic tool, but it is not a fix. If retries are enabled, report both the original failure and the eventual result so that the workflow does not erase useful information.
Checklist by scenario
When a test fails for the first time
- Capture the commit, branch, test name, runner or agent, operating system, runtime version, dependency versions, and relevant feature flags.
- Save the complete logs, screenshots, traces, artifacts, and failure messages before rerunning the job.
- Check whether the failure is deterministic on the same commit. Run the smallest relevant test scope first, then broaden the scope if needed.
- Compare the failure with recent code, configuration, dependency, and infrastructure changes.
- Decide whether the failure is a likely product regression, test defect, environment problem, resource problem, or unknown.
- Open or link a tracked issue rather than relying on a chat message or an informal retry.
When the same test passes and fails across reruns
- Record the number of attempts and preserve the result of every attempt.
- Look for timing clues: fixed sleeps, polling without a timeout, race conditions, asynchronous jobs, eventual consistency, and cleanup that runs too late.
- Check for shared data, reused accounts, static ports, temporary files, database records, queues, or containers that another test can modify.
- Compare runner load, available memory, CPU pressure, network latency, and parallel test groups when those signals are available.
- Run the test in isolation and in its normal parallelized position. A test that passes alone but fails in a suite may have an isolation or ordering defect.
- Use a bounded diagnostic retry if appropriate, but keep the first failed result visible to reviewers.
When the failure points to the test or test environment
- Replace arbitrary delays with condition-based waits and explicit timeouts.
- Make setup and cleanup idempotent so a partial failure does not contaminate the next attempt.
- Give parallel tests isolated data, namespaces, ports, credentials, and temporary directories.
- Stub or control external services when the test is intended to validate application logic rather than a third-party system.
- Pin or deliberately manage test dependencies where an unplanned version change can alter behavior.
- Add diagnostics that expose state at the point of failure, not only a generic assertion message.
When quarantine is necessary
Quarantine should be a controlled exception, not a second test suite where failures disappear. Before moving a test out of the blocking path, confirm that the team has documented:
- the test identifier and owning team or individual;
- the observed failure pattern and supporting run links;
- the suspected cause and the risk of losing this signal;
- the issue, proposed fix, and review or expiration date;
- the replacement protection, if the test covers a critical release path.
Keep quarantined tests running in a visible, separate job when practical. Track their pass and failure results, notify the owner, and prevent a quarantine label from becoming permanent without an explicit review. Critical security, data-integrity, migration, and release-gating tests may require a stricter policy than ordinary regression tests.
When restoring a repaired test
- Reproduce the original failure or demonstrate why the suspected cause was removed.
- Run the test repeatedly across the environments and parallel conditions that previously exposed the problem.
- Run the surrounding test group to detect ordering and shared-state effects.
- Verify that the assertion still checks the intended behavior and was not weakened to avoid the failure.
- Remove the quarantine marker, close or update the issue, and monitor subsequent pipeline results.
- Document the root cause and preventive change so similar tests can be reviewed proactively.
What to double-check
A useful triage record answers enough questions for another engineer to continue the investigation without repeating guesswork. Use this intake template:
| Field | What to record |
|---|---|
| Test | Stable test name, suite, component, and source location |
| First observed | Commit, pipeline, branch, and approximate time |
| Pattern | Failure frequency, whether reruns pass, and whether failures cluster by runner or suite |
| Evidence | Logs, traces, screenshots, artifacts, environment details, and links to failed jobs |
| Classification | Product, test, environment, dependency, resource, or unknown |
| Impact | Whether the test protects a release, security boundary, data path, or customer-critical behavior |
| Owner | Named team or person responsible for investigation and communication |
| Next action | Reproduce, fix, isolate, quarantine, replace, or retire |
| Review date | Date for checking progress and deciding whether quarantine remains justified |
Also verify the CI workflow itself. Confirm that test reports are uploaded when a command fails, cleanup runs even after an assertion failure, retries do not overwrite the initial result, and parallel workers have distinct resources. If a test depends on time, randomness, network access, or external services, make those dependencies observable and controllable where possible.
Use team-level signals carefully. Useful measures include the number of unique flaky tests, repeat offenders, time spent investigating, quarantine age, and the proportion of failures that are actionable on the first run. These measures should guide prioritization, not encourage teams to suppress failures or chase an arbitrary target.
Common mistakes
Relying on retries as the solution
Retries can reduce disruption while an investigation is active, but they can also hide a genuine regression. Preserve the original attempt, set a clear retry limit, and make retry outcomes visible in test reporting.
Quarantining without ownership
An unowned quarantine has no natural path back into the pipeline. Assign an owner, record the reason, and set a review date at the moment the test is quarantined.
Changing assertions until the test passes
Weakening an assertion may remove noise while also removing protection. Review whether the expected behavior, test data, and failure message remain correct after every test change.
Ignoring test order and shared state
Suite-wide failures often come from leaked state rather than the named test alone. Run the test first, last, in isolation, and alongside the group that normally precedes it. Inspect cleanup, fixtures, database records, files, ports, and service accounts.
Treating infrastructure failures as test flakiness
A saturated runner, unavailable dependency, expired credential, or network interruption may be a pipeline reliability issue. Label it accurately so the test owner is not asked to repair a problem outside the test.
When to revisit
Review flaky test health on a regular cadence and whenever the delivery system changes. Revisit the inventory before seasonal planning cycles, release freezes, major dependency upgrades, runner or operating-system changes, test-framework migrations, and significant increases in parallelism. A workflow change can alter timing, isolation, artifact handling, or retry behavior even when the application code is unchanged.
During a weekly or otherwise agreed cleanup, inspect quarantined tests by age, impact, owner, and recent results. Escalate tests that protect critical behavior or have remained quarantined beyond the team’s defined review window. During quarterly pipeline health reviews, check whether quarantine labels, retry settings, test-reporting formats, and ownership rules still match the CI platform and repository structure.
Before acting on the next intermittent failure, use this short recovery checklist:
- Save the first failed run and all relevant artifacts.
- Classify the failure and test it in isolation and normal parallel execution.
- Record the suspected cause, impact, owner, and next action.
- Use retries only as bounded diagnostics or temporary containment.
- If quarantine is required, keep it visible and assign a review date.
- Return the test to the blocking pipeline only after repair evidence is available.
For related release-engineering work, pair this playbook with the CI/CD Pipeline Maintenance Playbook and the Production Release Readiness Checklist. Together, they help teams treat test reliability as part of delivery reliability rather than as an isolated testing task.