Write unit tests
Professional structure: role, context, objective, details, steps, format, criteria and self-check. Designed for better results, not guaranteed ones: always review the answer.
Never use real personal data. If you need to, run it through the data cleaner.
## Role Act as a software quality engineer specialising in automated testing. ## Context I have a function with no tests and want to cover it properly. ## Objective Generate clear unit tests that cover the expected behaviour. ## Details (fill in what is in square brackets) - [function or module] - [language and test framework] - [expected behaviour] ## Step-by-step instructions 1. List the behaviours to test. 2. Add edge cases: empty, null, extreme values and odd formats. 3. Write tests whose names describe the behaviour. 4. Say which cases cannot be tested without test doubles. ## Output format List of cases and the complete test file in a code block. ## Quality criteria - Each test checks one thing. - Tests do not depend on order or the network. - It uses the stated framework. ## Self-check Before answering, check every quality criterion. If information is missing, ask me instead of making it up, and at the end state what you have assumed.
Objective
Generate clear unit tests that cover the expected behaviour.
When to use it
This prompt is ideal when you have stable, pure functions needing comprehensive coverage or when you are refactoring legacy code and require a safety net. It excels at uncovering hidden edge cases you might have overlooked. However, it is less effective for complex integration scenarios involving external APIs, databases, or UI interactions, where mock objects and dependency injection become messy. Avoid relying on this for highly coupled, monolithic code; you should refactor those into smaller, testable units first for better results.
Worked example: input (fictional data)
Worked example: full response
Variants
Quick smoke tests
Focus only on the happy path and two critical edge cases. Provide a concise summary of the test logic rather than a full file. This is best for rapid prototyping when you need a quick sanity check before committing your code, ensuring core functionality holds up without writing an exhaustive suite.
TDD for beginners
Explain each test case in plain English before writing the code. Break down the logic so it is easy to understand for a junior developer. Focus on readability and standard naming conventions, ensuring the output serves as a learning tool for understanding how to structure tests effectively.
Security-focused testing
Prioritise tests that check for invalid inputs, boundary conditions, and potential type coercion errors. Include tests that attempt to inject unexpected data types to ensure the function fails gracefully. This approach is essential for public-facing modules where input sanitisation and robust error handling are critical for application stability.
Common mistakes
- Writing tests that copy the implementation.
- Trusting generated tests without running them.
- Ignoring the setup and teardown requirements for stateful tests. How to avoid it: Always include explicit setup and teardown steps to ensure tests remain isolated and reliable.
- Naming tests vaguely like test1 or functionCheck. How to avoid it: Use descriptive naming conventions, such as shouldReturnErrorWhenInputIsNull, to clearly communicate the test's intent.
- Testing implementation details instead of public outcomes. How to avoid it: Focus on the function's input and output, rather than its internal variables or specific logic flow.
- Failing to mock external dependencies or API calls. How to avoid it: Use test doubles or mocks to replace external dependencies, ensuring tests run quickly and independently.
Related guide
General and assistant-specific tips
Tips by assistant
- Claude: ask for the result as an artifact to view and edit it separately from the chat.
Getting the most out of it
- Paste the prompt as is and only replace what is in [brackets].
- If the answer does not fit, do not start over: ask for “shorter”, “more formal” or “as a table”.
- Always review the result before using it: AI can get facts, dates or names wrong.
Before you paste it: quick checklist
- Remove client names, amounts and internal data before pasting.
- Say who the text is for and which tone you want.
- Ask for a specific length: “120 words max” or “three bullet points”.
- Read the result aloud and fix anything you wouldn’t say yourself.
How to adapt it to your assistant
The template works the same in ChatGPT, Gemini, Copilot or Claude because it only uses plain language. If your assistant supports fixed instructions (custom instructions, Gems or projects), you can store your preferred tone and format there and paste only the variable part. On a free plan, split long requests into two or three shorter messages.