The prompt
Write unit tests for this function using [framework]. Cover normal cases, edge cases and error handling. Explain each test in one line.
[code] What it is
This is a short AI prompt template with two placeholders: [framework] and [code]. It asks the model to write unit tests for the supplied function using the named framework. The tests must cover normal cases, edge cases and error handling, and each test must come with a one-line explanation.
Who it's for
- Developers who want a quick first draft of unit tests for a function
- Engineers adding test coverage to existing code
- Developers who want each test explained briefly so it is easy to review
Requirements
Requirements
- An AI assistant or chat model to run the prompt
- The function code to paste in place of [code]
- The name of the test framework to use in place of [framework]
Examples
Python slugify function with pytest
PromptWrite unit tests for this function using pytest. Cover normal cases, edge cases and error handling. Explain each test in one line.
def slugify(text):
if not isinstance(text, str):
raise TypeError('text must be a string')
return '-'.join(text.lower().split())Expected output: A set of pytest tests for slugify covering typical strings, edge inputs such as empty strings and extra whitespace, and the TypeError for non-string input, each with a one-line explanation.
JavaScript discount calculator with Jest
PromptWrite unit tests for this function using Jest. Cover normal cases, edge cases and error handling. Explain each test in one line.
function applyDiscount(price, percent) {
if (price < 0 || percent < 0 || percent > 100) {
throw new RangeError('Invalid input');
}
return price - (price * percent) / 100;
}Expected output: Jest tests for applyDiscount covering normal discounts, boundary values such as 0% and 100%, and the RangeError cases, each explained in one line.
Java string utility with JUnit 5
PromptWrite unit tests for this function using JUnit 5. Cover normal cases, edge cases and error handling. Explain each test in one line.
public static String reverse(String input) {
if (input == null) {
throw new IllegalArgumentException("input must not be null");
}
return new StringBuilder(input).reverse().toString();
}Expected output: JUnit 5 tests for reverse covering ordinary strings, edge cases such as an empty string or a single character, and the IllegalArgumentException for null, each with a one-line explanation.
Pros & cons
Pros
- Pro:Short and easy to reuse with any function
- Pro:Explicitly asks for normal cases, edge cases and error handling, so coverage is not limited to the happy path
- Pro:One-line explanations make the tests easier to review
- Pro:Works with whichever framework you name
Cons
- Con:Provides no context beyond the code, so the model may miss intended behavior that is not visible in the function
- Con:Gives no guidance on test style, naming or mocking
- Con:Output still needs human review to confirm the tests are correct and meaningful
Tips
- Paste the complete function, including any validation or error handling, so error cases can be tested
- Name the framework precisely in place of [framework]
- Check that each one-line explanation matches what its test actually asserts
Variations
- Add a request for a specific number of tests or particular scenarios to be included
- Ask for the tests to be grouped by normal, edge and error categories
- Ask for more detailed explanations in place of the one-line limit