Code Review & Debugging5.0 · 0 ratings

Flaky Test Stabilizer

Diagnoses why an intermittently failing test is flaky and rewrites it to be deterministic and reliable.

Role-Based

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. 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. 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. 3

    Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.

Techniques in this prompt

Role-Based

Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.

Learn this technique

Recommended models

claudegpt-4ogemini

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