ChatGPT Prompts for Writing Unit Tests (That Actually Catch Bugs)
Ask an AI assistant to "write tests for this function" and you will get tests. They will look professional. They will usually pass. And too many of them will only prove that the code does what the code already does.
Good tests are different. They describe what the code should do, including the awkward inputs you did not think about. The trick is to stop asking for "tests" and instead walk the assistant through the same steps a careful developer would. Below is a four-step workflow with a prompt for each step. Replace the [BRACKETS], paste into any chat assistant, and read what comes back before you trust it.
Step 1: List the cases before any test code exists
If you jump straight to code, the assistant tests the happy path and stops. Ask for a list of cases first, so you can see what is missing and correct it.
Here is a function:
[PASTE FUNCTION]
List the test cases it needs, grouped as:
- Normal cases
- Edge cases (empty, zero, negative, very large, duplicates, unusual characters, wrong type)
- Error cases (what should happen with invalid input)
Format as a table: Input | Expected result | Why this case matters.
Don't write test code yet. If the expected result is unclear, say so.
Look at the table it returns. If an expected result is wrong, that is useful: it means the assistant (or you) misunderstood the function. Fix the table before moving on.
Step 2: Write the tests, with rules
Now ask for the tests, and give the assistant rules that keep them readable and honest.
Write unit tests for this [LANGUAGE] code using [pytest / unittest / Jest / Vitest / other]:
[PASTE CODE]
- One test per behaviour, with descriptive test names.
- Arrange / act / assert layout.
- No real network, files or databases.
- Include the exact command to run the tests.
If the right behaviour for some input is unclear, write that test as a commented-out question instead of guessing.
The last line matters. When the right behaviour is unclear, a guess becomes a test that locks in a bug. A commented-out question is better than a confident wrong answer.
Here is the shape of what you should expect, for a small apply_discount(price, percent) function in Python with pytest:
import pytest
from pricing import apply_discount
def test_ten_percent_off_reduces_price():
assert apply_discount(100.00, 10) == 90.00
def test_zero_percent_leaves_price_unchanged():
assert apply_discount(100.00, 0) == 100.00
def test_negative_percent_is_rejected():
with pytest.raises(ValueError):
apply_discount(100.00, -5)
# QUESTION: should a discount over 100% raise an error or clamp the price at 0?
Your assistant's wording and code will differ. The pattern to look for is one behaviour per test, names that read like sentences, and open questions flagged instead of guessed.
Step 3: Turn every bug you find into a test
The most valuable tests are the ones that come from real bugs. When you fix something, ask for a test that fails before the fix and passes after.
I found this bug: [DESCRIPTION, including the input that fails].
Code: [PASTE CODE]
1. Write a test that reproduces the bug and FAILS on the current code.
2. Show the fix.
3. Explain how this test now guards against the bug coming back (a "regression test").
Run the new test against the old code and watch it fail. A regression test you have never seen fail has not proven anything.
Step 4: Ask the assistant to review its own tests
Fresh eyes help, even artificial ones. Paste the tests and the code back in and ask what is weak.
Review these tests:
[PASTE TESTS]
for this code:
[PASTE CODE]
1. Which important behaviours aren't tested?
2. Which tests are fragile (they depend on timing, order, randomness or exact formatting)?
3. Which tests check internal details instead of behaviour?
Finish with the 3 changes that would add the most confidence.
Fragile tests (those that depend on timing, order, randomness or exact formatting) are the ones that fail on a Tuesday for no reason. Catching them early saves a lot of confusion.
A quick way to check that tests can fail
Pick one test. Break the code on purpose: change a + to a -, or return the wrong variable. Run the tests. At least one should fail. If nothing fails, your tests are not looking at the behaviour you care about. Undo the change afterwards.
Habits that keep AI-written tests honest
- Run them. An unrun test is only a suggestion.
- Read every assertion. Ask yourself whether you would be surprised if it failed.
- Keep secrets out of the chat. Replace real keys, tokens and customer data with
XXXXbefore pasting. - Avoid real network, files and databases in unit tests. Ask for fakes or mocks instead.
- Treat AI code as a draft. Always read and test AI-generated code before relying on it.
Want the full set?
These four prompts are a small slice of the testing section in the Prompt Pack for Coding, which also covers manual test checklists, tricky test data, mocks and fakes, and explaining failing tests. Another good starting point is our free prompt library, where you can copy more prompts with one click.
Prompt Pack for Coding
40 prompts to learn, debug and ship small projects. 22-page PDF, $9. Code LAUNCH20 = 20% off.
Not ready to buy? Start with the free PDF.
Get 10 of our most-used prompts as a free PDF from Gumroad. You will be asked for your email so Gumroad can deliver it.