How to Prompt AI to Write a Regression Test for a Bug
A regression test pins a fixed bug so it cannot return. This prompt template makes the AI write the failing test first, then proves it fails for the right reason.
To prompt AI to write a regression test, ask for the test before the fix, require it to fail on the current code, and only then allow the fix. A regression test is a test that captures a bug you have already found, so the same bug cannot quietly come back later. The trick is making the AI prove the test fails for the right reason.
Why order matters
If you say "fix this bug and add a test", a coding agent usually fixes the code first, then writes a test that passes. That test has never failed, so you do not know whether it would have caught the bug. A test that has never been red is a guess.
The protocol below forces the red step, and it works in any chat tool or coding agent.
The prompt template
Bug: <one or two sentences describing what goes wrong>
How to reproduce: <exact input or steps>
Expected: <what should happen>
Actual: <what happens instead>
Do this in order, and stop after each step:
1. Write one test that reproduces the bug. Do not change any application code.
2. Tell me the command to run it and the exact failure message I should see.
3. Wait for me to confirm it fails with that message.
Only after I confirm, propose the smallest fix and re-run the test.Run the test yourself in step 3. If it fails with the message the AI predicted, you have a trustworthy regression test. If it fails for a different reason, such as a typo or a missing import, send that output back and ask for a correction.
What a good regression test looks like
**One bug, one test.** It uses the exact input from the report, not a generic case.
**A clear name.** Describe the behaviour, for example
refund_of_zero_amount_is_rejected, nottest_bug_42.**A failure message you can read.** When it breaks in six months, the name and assertion should explain why.
For the wider craft of test prompts, see prompting AI to write unit tests that catch bugs.
Three ways a regression test proves nothing
**It mocks the thing that was broken.** If the bug was in a database call and the test replaces that call with a fake, the test passes whatever the real code does. Ask: "Does this test run the real code path where the bug was?"
**It asserts too little.** A test that only checks the function did not throw would pass on wrong output. Ask for an assertion on the actual result.
**It was edited to pass.** When an agent hits a red test, it sometimes loosens the assertion. Compare the test before and after the fix. It should not change. If a failing test is the problem instead, see prompting AI to fix a failing test.
Give the AI the real evidence
The more precise the reproduction, the better the test. Paste the error output and stack trace. If you cannot read the trace, getting an AI coding agent to explain a stack trace helps. Then ask a follow-up: "What neighbouring inputs could break the same way?" A few of those become extra tests, and generating edge cases for testing goes deeper.
A worked example
Suppose a discount code lets a customer reduce an order total below zero. Your prompt states the bug, the input (a 15 dollar order and a 20 dollar code) and the expected result (total of 0, not minus 5). The AI writes one test creating that order and asserting a total of 0. You run it and see "expected 0, got -5". Now the fix is allowed. After it, the same test is green, and it stays in the suite so a later refactor cannot reintroduce the bug.
For more habits that make prompts reliable, see prompt engineering.
FAQ
What is a regression test?
A test that captures a bug you have already fixed, so it fails if the bug ever returns.
Should the test be written before or after the fix?
Before. A test that fails first and passes after the fix proves it detects the bug.
Can AI write regression tests reliably?
It can write them, but you need to run the test and see it fail for the expected reason. Do not rely on the AI's claim that it works.
Where should a regression test live?
In your normal test suite, so it runs every time. Name it after the behaviour it protects.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


