Diagnose and fix a flaky test
Works out why a test passes and fails intermittently, how to reproduce it, and how to fix the cause rather than adding retries.
The prompt
Help me find out why a test is flaky and fix it properly.
Test code: [PASTE THE TEST]
Code under test: [PASTE THE RELEVANT PARTS]
Failure output: [PASTE ONE OR MORE FAILING RUNS]
How often it fails: [e.g. "1 in 20 runs", "only in CI", "only when run with the full suite"]
Test environment: [FRAMEWORK, CI RUNNER, PARALLELISM, DATABASE OR CONTAINERS USED]
Work through the common causes and say which fit my evidence:
- Timing: sleeps, timeouts, async work not awaited, animations, polling.
- Order dependence: shared state, leftover database rows, global mocks, caches between tests.
- Concurrency: parallel tests sharing ports, files, or records.
- Time and randomness: current date, time zones, daylight saving, unseeded random data, unordered collections.
- Environment: network calls, external services, resource limits in CI.
Return:
1. The most likely cause(s), ranked, with the evidence for each.
2. How to reproduce it reliably (e.g. run in a loop, fixed seed, specific test order, throttled CPU).
3. The fix, as code. Prefer fixing the root cause: deterministic waits, isolated data, injected clocks, seeded randomness.
4. If the real bug is in the production code rather than the test, say so clearly.
5. What not to do: explain why a blanket retry or longer sleep would only hide the problem.
Highlighted [brackets] are blanks to replace with your own details.
Fill in the blanks and use this promptMore Software Development prompts
What to fill in
- PASTE THE TEST
- PASTE THE RELEVANT PARTS
- PASTE ONE OR MORE FAILING RUNS
How this prompt is built
This prompt follows the Few-Shot structure (Few-shot prompting). A few-shot prompt includes one or more worked examples of the input and the output you want, so the model can copy the pattern instead of guessing it.
Related prompts
- Diagnose slow launches and API cold starts in my mobile appA codebase-specific diagnosis of startup time, serverless cold starts, and network waste, with fixes ranked by impact on perceived speed.
- Set up experiment tracking for a machine learning training runStep-by-step setup to log metrics, hyperparameters, and artifacts from your training script, locally or on a cluster.
- Design consistent API error responses developers can act onTurns unhelpful API errors into a consistent format with clear codes, statuses, and messages that tell callers exactly what to fix.
- Reply to code review comments I disagree withDrafts calm, evidence-based replies to review feedback so you can resolve disagreements without stalling the pull request.