Good tests describe behaviour, cover the edges and fail for a clear reason. This prompt makes the model state what the code should do, plan the cases and then write small, readable tests, flagging suspected bugs instead of hiding them.
Write unit tests for this function
Thorough unit tests covering normal cases, edge cases and errors, with suspected bugs flagged; change the language, framework and focus.
Act as a senior Python engineer who writes clear, reliable tests. Write unit tests for the code below using pytest. Steps: 1. In two or three lines, state what the code is meant to do, based on its names, types and logic. 2. List the cases to cover: normal inputs, edge cases (empty, zero, negative, very large or missing values) and expected errors. 3. Write the tests: one behaviour per test, descriptive test names and the arrange, act, assert pattern. 4. Mock only external dependencies, such as network calls, files, the clock and randomness. Pay extra attention to: boundary values and invalid input. Rules: the tests must run as written, with every import included. Do not change the code under test. If the code looks buggy, write a test that exposes the bug, label it clearly and explain the problem in one line instead of making the test pass. Code: (paste your function here)
Click a highlighted part to change it.
Make it yours
Pick an option or type your own. The preview updates as you go: copy it when it reads right, or open it straight in your AI tool.
Your version stays in this browser. Ctrl or ⌘ + Enter copies it.
About this prompt
Why it works
- Behaviour first
Stating what the code should do before testing it catches misunderstandings early and gives every test a clear target.
- A case list
Listing normal, edge and error cases up front makes the coverage visible, so you can spot gaps before any test is written.
- One behaviour per test
Small, well-named tests tell you exactly what broke when one fails.
- Expose bugs, do not hide them
Telling the model not to bend tests around buggy code turns the test suite into a bug finder.
Tips for better results
- Remove API keys, passwords and real customer data from the code before you paste it.
- Include the types, interfaces or a sample input so the tests use realistic data.
- Run the tests straight away. If any fail, paste the output back and ask whether the test or the code is wrong.
- In Cursor, select the function and its imports so the tests match your project’s structure and existing helpers.
Variations to try
- Table-driven tests in Go
Act as a senior Go engineer. Write table-driven unit tests for the function below using the standard testing package. Give every row a case name, cover normal inputs, zero values, empty slices and error returns, and run each case as a subtest with t.Run. Do not change the function.
- Lock in behaviour before a refactor
I am about to refactor the TypeScript function below. Write Jest characterisation tests that lock in its current behaviour exactly, including odd edge cases, even where the behaviour looks wrong. Name each test after the behaviour it captures, and list any behaviour that looks like a bug so I can decide whether to keep it.
Questions
Will the tests pass first time?
Not always. Run them and paste any failures back: sometimes the test is wrong, and sometimes it has found a real bug, which is the point.
Does it replace deciding what to test?
No. Read the case list from step 2 and add any business rule only you know about, such as limits, special customers or legal requirements.
Did this prompt work for you?
Thanks. Your answer helps the next person.
Quick copy
Write unit tests for this function
Copies your version from the studio.
For prompt makers
Got a prompt that works every time?
Send it in. We review every submission by hand and credit you on the page when it goes live.