Write unit tests for a function

Generates focused unit tests covering normal cases, edge cases and errors, using your test framework’s conventions.

Prompt, 2 blanks to fill
Write unit tests for the code below using {{TEST_FRAMEWORK}}.

```
{{CODE}}
```

First, list the test cases you plan to write in three groups:
- Normal behaviour
- Edge cases (empty, zero, very large, unicode, boundaries)
- Errors and invalid input

Then write the tests.

Rules:
- One behaviour per test, with a descriptive name.
- No tests for private details that could change without breaking behaviour.
- Mock only external calls (network, time, file system).
- If the code has a bug you notice while writing tests, add a failing test for it and point it out.

Example output

Cases planned: 7 (3 normal, 3 edge, 1 error). describe("parsePrice") { it("parses £1,299.00 to 129900 pence") ... it("returns null for empty string") ... }

How to use

Include any types or helper functions the code depends on. Run the tests before trusting them; a test that passes on broken code is worse than no test.

Why it works

Planning the cases first gives you a checklist to review before the model writes any code. The rule about failing tests turns the model's attention to detail into bug reports, not just coverage.

Find a prompt for the task in front of you.

96 prompts in 8 categories. Copy one, fill in the blanks, paste it into any model.

Browse prompts

Promptlexity is a free library of 96 AI prompts and 8 downloadable AI skills across 8 categories, plus a glossary of 64 AI and prompting terms. Each prompt shows the exact text to paste, the variables to fill in, an example of what comes back, and why it works. Each glossary term has a plain-English definition, an example and a common mistake. 6 step-by-step guides cover the basics. No signup, works with ChatGPT, Claude, Gemini and other models.

Press Esc to close