CI/CD & Deployment interview question · Most asked

How do you handle flaky tests in CI?

Scenario Asked in DevOps Engineer · Site Reliability Engineer (SRE) · QA Engineer / SDET interviews

Members can save this page, take notes and track progress. See membership · Sign in

Summary

Quarantine flaky tests so they don't block the pipeline, track them in a dashboard, fix the root causes (timing, shared state, external dependencies) systematically, and never accept "just re-run" as a strategy.

Longer Answer

A flaky test passes sometimes and fails sometimes with no code change. Even a 1% flake rate on 500 tests means roughly one false failure per pipeline run, which trains the team to ignore red builds.

Immediate response:

  1. Quarantine: move the test to a non-blocking suite so it still runs and reports but doesn't fail the pipeline. Most frameworks support tags like @flaky or @quarantine.
  2. Track: log every flake with its test name, failure message and timestamp. A dashboard (or even a spreadsheet) makes patterns visible.
  3. Alert: treat a quarantine list that grows as a quality incident.

Root causes and fixes:

Cause Fix
Timing / race conditions Replace sleeps with explicit waits; use polling or event-based assertions
Shared state between tests Isolate test data; reset state before each test; use transactions
External service dependency Mock or stub external calls; use containers (Testcontainers) for databases
Resource exhaustion Increase CI runner resources; limit test parallelism; check for leaks
Non-deterministic ordering Randomise test order locally to surface hidden dependencies

What not to do:

  • Auto-retry failing tests in CI and count the second pass as green. This hides the problem and doubles build time.
  • Delete the test. If it tests real behaviour, fix it; if it doesn't, it shouldn't have existed.

A healthy CI pipeline has a flake rate below 0.1% — measure it and treat it as a metric like build time.

Key Points

  • Quarantine flaky tests immediately so they don't block the pipeline
  • Track flakes in a dashboard — patterns reveal root causes
  • Common causes: timing, shared state, external dependencies, resource exhaustion
  • Never auto-retry as a fix — it hides the problem and doubles build time

Explanation

What interviewers look for

Look for a structured approach: quarantine, track, fix root causes. Weak answers say "just re-run the pipeline." Strong answers mention isolation, measurement and treating the quarantine list as a quality metric.

More CI/CD & Deployment questions

← All CI/CD & Deployment questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake