Flaky tests found and fixed instead of rerun: a Claude + Cursor stack
The struggle: Your build fails at random, everyone clicks rerun until it goes green, and real bugs hide behind the noise.
What you end up with: A list of the flaky tests with their likely causes, and fixes that make them reliable without deleting them.
Claude finds which tests fail at random and in what pattern from your CI logs. Cursor plans and applies a fix for one test at a time, and you prove it stable by running it many times.
Tools: Claude, Cursor. Level: Free to start. Time: 2 hours for the first test.
Use it if
- The same tests fail and pass on rerun
- CI logs from many runs are available
- You want a fix, not a quarantine forever
Skip it if
- The failures are real and consistent
- You would delete tests to get green
The method
Collect the failure history (Claude: File uploads)
Export the names of failed tests from the last few dozen CI runs, with the date, branch and error message. Remove tokens and internal hostnames, then attach the file to a Claude chat.
Prompt to copy:
Give me a checklist to export failed-test history from [CI tool] for the last [number] runs, with test name, date, branch, commit and error message. Say which fields to strip, such as tokens and internal hostnames.
You get: A file of failures across many runs.
Tip: Include passing reruns of the same commit; that is the proof of flakiness.
Find the flaky ones and the pattern (Claude: Chat)
Ask Claude to separate tests that fail on one commit and pass on rerun from real failures, and to look for patterns.
Prompt to copy:
From this CI failure history, list tests that failed and then passed on the same commit (likely flaky), with count of runs and failure messages. Group them by likely cause: timing, order dependence, shared state, network or external service, resource limits. Separate tests that fail consistently. Say what the data cannot show.
You get: A ranked list of flaky tests with a pattern hypothesis for each.
Tip: Patterns to look for: time, order, shared state, network, parallel runs.
Plan a fix for one test (Cursor: Plan Mode)
In Cursor press Shift+Tab for Plan Mode, paste the top flaky test name and its failure messages, and ask for the cause and a fix plan.
Prompt to copy:
This test is flaky: [test name]. Failure messages: [paste]. Read the test and the code it touches, find the likely root cause (timing, shared state, order, network, randomness), and propose a fix that removes it. Do not add retries or sleeps and do not delete or skip the test. Explain how to prove the fix.
You get: A plan that names the root cause, not a retry.
Tip: Say to avoid adding retries or longer sleeps as the fix.
Apply the fix and run it many times (Cursor: Agent)
Build the plan in Agent, then run the test repeatedly yourself, in random order and in parallel if your CI does, to see that it stays green.
Prompt to copy:
Write a script or command to run [test name] 100 times in the same way our CI does, in random order and in parallel if it does, and count failures. Tell me what result would count as fixed.
You get: A test that passes a long streak.
Tip: A single green run proves nothing for a flaky test.
Keep a flaky list (Claude: Chat)
Paste your results and ask for a table of fixed, open and quarantined tests with owner roles. Review it at each sprint.
Prompt to copy:
Turn my results into a table: test, cause, status (fixed, open, quarantined), owner role, ticket, date. A quarantined test needs a ticket and a review date. Results: [paste]
You get: A short list the team keeps current.
Tip: Quarantine only with a ticket and a date, never as a silent skip.
Where this goes wrong
- Never delete or skip a test to get green. Review each fix before you merge.
- A retry or a longer sleep hides the cause and returns later.
- Strip tokens and hostnames from the CI data before you upload it.
- Some flakiness is the product, such as a real race condition. Treat that as a bug.
Sources
More like this
- Fix a broken app without breaking more
- Short videos that open with a real hook
- Check if anyone pays before you build
Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.