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
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:
- 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
@flakyor@quarantine. - Track: log every flake with its test name, failure message and timestamp. A dashboard (or even a spreadsheet) makes patterns visible.
- 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
- How do you promote a single build artifact from development through staging to production?
- What stages does a typical CI/CD pipeline contain?
- How would you design CI/CD for a microservices architecture?
- What is shift-left testing, and what security scanning belongs in a CI pipeline?
- What's the difference between continuous integration, continuous delivery and continuous deployment?
- What are best practices for writing Dockerfiles?
- Compare rolling, blue-green and canary deployments.
- How do you run database migrations without downtime?