Black Friday: 40% off ThatPrompt. $23.40 instead of $39, until 30 November. Get the deal

ThatPrompt

Prompt to write unit tests for existing code

Get unit tests for a function or class that cover the normal path, the edge cases and the failures, written for your test framework and ready to run.

Category
Coding
Tags
Testing
Added
Added

Fill in the prompt

The prompt below changes as you type, and Copy prompt copies it filled in. What you type stays in this browser. Leave a box empty to fill it in after you paste.

The prompt

Write unit tests for the {{language}} code below, using {{test_framework}}.

{{code}}

Before writing any tests, list the behaviours worth testing as short sentences, grouped into:
- the normal path
- edge cases: empty input, boundaries, very large values, unusual characters, time zones if dates are involved
- failures: invalid input, dependencies that throw or time out

Then write the tests:
- One behaviour per test, named after the behaviour, not the method.
- Arrange, act, assert, with a blank line between each part.
- Use real collaborators where they're cheap. Use a fake only for slow or external things like the network, the clock or the file system, and say why for each one.
- No snapshot tests. No assertions on private details.

If the code is hard to test as written, say what change would make it easier. Still write the tests against the code as it is now.

Replace each {{highlighted part}} with your own text before you send it.

How to use it

Paste the function or class into {{code}}, along with the types or interfaces it uses. If a test needs an object you haven't pasted, the model will invent one, and invented constructors are the main reason generated tests fail to compile.

Name the language in {{language}} and the tool in {{test_framework}}, with versions if your setup is unusual. "pytest with pytest-asyncio" or "xUnit and FluentAssertions" tells the model which assertions and fixtures exist in your project.

Example for {{test_framework}}: "Jest 29, with test files next to the source."

Read the list of behaviours before you read the tests. It's the fastest way to spot a case the model missed, or one your code was never meant to support. If it says the code is hard to test, that's design feedback worth keeping even if you take the tests as they are.

All coding prompts