Example result: Write unit tests for your code
AI-generated example · Test Coverage Boost
Updated

Write unit tests for your code

This prompt asks a senior engineer to write thorough unit tests for your code in the language and framework you name. It covers normal cases, edge cases such as empty, null and very large input, and error handling. Run the tests yourself.

text-unit-tests — claude — 61 words

Act as a senior engineer. Write thorough unit tests for this [LANGUAGE / FRAMEWORK, e.g. Python with pytest] code:

[PASTE YOUR CODE]

Cover normal cases, edge cases (empty, null, very large, and invalid input), and error handling. Use clear test names that describe the behaviour. Point out any bug you find while writing the tests, and explain how to run them.

Run the tests yourself — if one fails, paste the output back and ask why.

Get a better result

What you will get

A set of unit tests for your code covering normal cases, edge cases such as empty, null, huge and invalid input, and errors, with descriptive test names, any bugs found and steps to run them.

Common mistake

The tests pass but test nothing real.

Fix: Ask for tests that fail if the code is wrong, and try breaking the code on purpose to see them catch it.

Also watch for
  • The framework does not match your project. Name the language and the exact test framework in the bracket.
  • A test fails and you are unsure why. Run it yourself, then paste the full output back and ask for the cause, as the tip says.
Try a variation: ask for a test plan first

Before any code, ask for a list of the behaviours to test in plain English, and edit the list, then generate the tests.

Still not right? Reply with this

Write a test that reproduces the bug you pointed out, so it fails now and passes once the bug is fixed.

Guides that use this prompt

More chat & writing prompts

See all 465 →