Code Review
Get a prioritized code review that covers correctness, security, readability and tests.
Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.
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.| Variable | What to put in | Example |
|---|---|---|
| {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. |
# 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.
Claude tends to keep to the test style you describe. For large modules, ask for tests function by function.
Works as-is. If your plan offers code execution, ask it to run the tests; otherwise run them locally.
If you can attach the source file, name it and the test file you want extended, so new tests match the existing ones.
Name the exact framework and version. Verify that the test helpers it uses exist in the version you have.
Add property-style tests for the main function.Reduce the suite to the ten most valuable tests.Rewrite the failing test names so they describe the rule they check.Get a prioritized code review that covers correctness, security, readability and tests.
Diagnose an error by giving the AI the message, the code, what you tried and what you expected.
Improve the structure and naming of code without changing what it does, with a list of changes.