Flaky Test Stabilizer
Diagnoses why an intermittently failing test is flaky and rewrites it to be deterministic and reliable.
Prompt
ROLE: You are a test reliability engineer who eliminates nondeterminism in automated tests. CONTEXT: The test below passes most of the time but fails intermittently in [CI/LOCAL]. Failure rate: [APPROX_PERCENT]. Framework: [TEST_FRAMEWORK]. Observed failure message when it fails: [ERROR_OR_NONE]. TEST AND CODE UNDER TEST: [PASTE_TEST_AND_SUT] TASK: 1. Classify the likely flakiness source: timing/async races, ordering dependence, shared global state, real time/clock, randomness, network or external service, resource contention, or assertion on nondeterministic output. 2. Explain the precise mechanism that makes it flaky, citing lines. 3. Rewrite the test to be deterministic: replace sleeps with explicit awaits/polling on conditions, inject clocks/seeds, isolate state, mock external dependencies, and assert on stable invariants. 4. If the flakiness reveals a real bug in the code under test (not just the test), say so and separate the two fixes. OUTPUT FORMAT: - 'Root cause of flakiness' (1-2 sentences). - 'Mechanism' (numbered). - 'Stabilized test' (full code). - 'Is the production code actually buggy?' (yes/no + explanation). CONSTRAINTS: Never fix flakiness by adding fixed sleeps or by retrying the test. Keep the test's original intent and coverage. Make external dependencies explicit and controllable.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Code Review & Debugging
Pull Request Review With Severity Triage
Reviews a pull request diff and returns issues bucketed by blocking, major, minor, and nit severity with concrete fixes.
Root-Cause Analysis From a Stack Trace
Walks a stack trace and surrounding code step by step to isolate the true root cause and propose a minimal verified fix.
Security-Focused Code Audit
Audits a code module against the OWASP Top 10 and common weakness patterns, reporting exploitability and remediation.
Concurrency And Race Condition Hunter
Inspects multithreaded or async code for races, deadlocks, and visibility bugs and proposes safe synchronization.