← Back to libraryQuestion 228 of 273
🔧CI/CDIntermediate

Flaky-Test Strategy in CI (Retries, Quarantine, Tracking)

📌 Definition:

A flaky test passes and fails non-deterministically on the same code. A flaky-test strategy is the disciplined approach to detecting, containing and eliminating flakiness so it neither blocks good code nor hides real bugs.

📖 Detailed Explanation:

Flakiness is corrosive: it erodes trust until developers reflexively re-run red builds, at which point the suite stops catching real regressions. A mature strategy has four parts. Detect: mark tests that pass only on retry as 'flaky' and track the rate as a first-class metric. Contain: quarantine chronically flaky tests into a non-blocking lane so they don't gate merges, while explicitly tracked for repair — not deleted and forgotten. Diagnose: capture traces on first retry so each flake ships evidence. Eliminate: fix root causes (bad waits, shared state, timing/animation races, order dependence) rather than papering over with ever-higher retries. The key judgment is that retries are a diagnostic safety net and a temporary bridge, never a permanent cure — a rising flaky count is a signal to invest, not to raise the retry ceiling.

🔑 Key Points:
  • Flaky = non-deterministic result on unchanged code; it destroys suite trust
  • Detect: mark pass-on-retry as 'flaky' and track the rate as a metric
  • Quarantine chronic flakes into a non-blocking lane, tracked for repair (not deleted)
  • Diagnose with trace-on-first-retry; fix root causes (waits, shared state, order)
  • Retries are a safety net/bridge, not a cure — rising flakiness means invest, not raise retries
🌍 Real-World Example:

A team's flaky dashboard shows 15 tests flaking weekly. Rather than bumping retries, they quarantine the worst offenders into a non-blocking job (so PRs aren't blocked), open a ticket per test, and fix them using first-retry traces. Within two sprints the flaky rate is near zero and the quarantine lane is empty.

📎 Code Example:
# playwright.config.ts — modest retries + trace on first retry
#   retries: process.env.CI ? 2 : 0,
#   use: { trace: 'on-first-retry' }

# Quarantine lane: run known-flaky tests separately, non-blocking
jobs:
  stable:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npx playwright test --grep-invert @flaky   # blocks merge
  quarantine:
    runs-on: ubuntu-latest
    continue-on-error: true                                       # does NOT block merge
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npx playwright test --grep @flaky
🎯 Scenario-Based Interview Question:

Scenario: A manager proposes deleting every test that has ever flaked 'to get a green pipeline'. QA objects. What's the risk of that approach, and what strategy do you propose instead?