Why this prompt works
This unit test prompt asks for a plan of cases before any code, so you can spot missing scenarios in a short list instead of reading through long test files. Grouping cases into normal, edge and error makes gaps obvious.
Sharing the business rules matters because tests should check what the code is supposed to do, not just repeat what it currently does. Asking the AI to flag behaviour that looks like a bug turns test writing into a quick code review as well.
How to use this prompt
- Tap "Copy prompt".
- Replace every part in [square brackets] with your own details.
- Paste it into ChatGPT or Claude and send.
- Ask a follow-up like "make it shorter" or "give me 3 more options" to fine-tune it.
Variations to try
Tests for a function with an API call
- Type
- Text
- Fill in
- 3 blanks
- Copies
- Be the first
Add tests before refactoring
- Type
- Text
- Fill in
- 2 blanks
- Copies
- Be the first
Make it your own
- Change the framework line to Jest, Vitest, JUnit, RSpec or Go's testing package.
- Add "Use the Arrange, Act, Assert layout with blank lines" for team style.
- Ask for property-based tests (for example with Hypothesis) for functions with many possible inputs.
Common problems and fixes
- Tests only cover the happy path. Ask the AI to add at least 3 edge cases and 2 error cases before writing code.
- Tests break every time the code changes. Keep the rule to test behaviour, not private details; avoid asserting on internal calls.
- Tests pass even when the code is wrong. Break the function on purpose and check that at least one test fails.
FAQ
Can AI-written tests replace my own?
They are a good start, but you know the real rules. Review every case and add the ones only your team knows about.
How many tests does one function need?
Enough to cover each rule, the edges and the errors. For a small function, 4 to 8 focused tests is common.