Write Unit Tests

Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.

testingunit testsquality All models

Prompt

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

<code>
{code}
</code>

<expected_behavior>
{expected_behavior}
</expected_behavior>

<test_style>
{test_style}
</test_style>

Cover these groups, and label them in the test names or comments:
1. Normal cases: typical inputs with correct results.
2. Edge cases: empty values, zero, very large values, boundaries, special characters.
3. Failure cases: invalid input and the errors that should be raised.
4. Any behavior in the code that does not match the expected behavior I described. If you find one, write a test that shows it and explain it separately.

Rules:
- Write tests from the expected behavior, not just from reading the code, so the tests can catch bugs in the code.
- Work out the expected result of each test by hand. Do not run any calculation you cannot explain.
- Do not test private implementation details. Do not invent functions or modules that are not in the code.
- If the code needs setup, such as test data or a mock, show it and say why it is needed.

Give the full test file first. Then list the cases you chose not to cover and why, and tell me how to run the tests.

Variables

VariableWhat to put inExample
{language}The programming language.Python
{test_framework}The test framework you use.pytest
{code}The code to test.Paste the function or class here.
{expected_behavior}What the code should do, including rules and limits.Returns the total price with discounts; raises an error for negative quantities.
{test_style}How tests should look in your project.One assert per test; descriptive test names; no mocks unless needed.

How to use

  1. Describe the expected behavior in your own words. Tests written only from the code can repeat its bugs.
  2. Run the tests yourself. If one fails, check whether the test or the code is wrong before changing either.
  3. Add the cases from your own bug reports; these are usually the most valuable.

Example result

Illustrative example

# Normal cases def test_total_applies_ten_percent_discount_over_100(): ... # Edge cases def test_total_is_zero_for_empty_cart(): ... # Failure cases def test_negative_quantity_raises_value_error(): ... Mismatch: the code rounds down, while the expected behavior says "rounded to the nearest cent". See test_total_rounds_to_nearest_cent.

Model notes

Claude

Claude tends to keep to the test style you describe. For large modules, ask for tests function by function.

ChatGPT

Works as-is. If your plan offers code execution, ask it to run the tests; otherwise run them locally.

Gemini

If you can attach the source file, name it and the test file you want extended, so new tests match the existing ones.

Grok

Name the exact framework and version. Verify that the test helpers it uses exist in the version you have.

Refine the result

  • Prompt
    Add property-style tests for the main function.
  • Prompt
    Reduce the suite to the ten most valuable tests.
  • Prompt
    Rewrite the failing test names so they describe the rule they check.

Related templates

  • Code Review

    Get a prioritized code review that covers correctness, security, readability and tests.

    code reviewqualitysecurity All models
  • Debug an Error

    Diagnose an error by giving the AI the message, the code, what you tried and what you expected.

    debuggingerrorstroubleshooting All models
  • Refactor for Readability

    Improve the structure and naming of code without changing what it does, with a list of changes.

    refactoringreadabilityclean code All models

Related guide