Sub topic 7
Reviewing generated tests before you trust them
This chapter assumes you can read pytest code, you know what parametrize and pytest.raises do, and you have already generated tests from a prompt as in the earlier chapters. Generated tests usually run and pass on the first try. A passing test only shows that the code and the test agree. It does not show that either one is correct.
Why a green run proves little
An assistant writes tests by reading your code. If the code has a bug, the assistant may copy the bug into the expected values. It may also write assertions that can never fail. Review is the step that finds these problems.
Use the apply_discount function from earlier chapters as the subject. Its contract is: return the price reduced by the percent, rounded to two decimals, and raise ValueError for a negative price or a percent outside 0-100.
Problem 1: tautological assertions
A tautological test computes the expected value with the same logic as the code under test.
from pricing import apply_discount
def test_apply_discount_matches_formula():
price, percent = 100, 10
assert apply_discount(price, percent) == round(price * (1 - percent / 100), 2)This test passes for any version of the formula, including a wrong one, because it repeats the formula. Replace it with a value you worked out from the docstring: 10% off 100 is 90.0.
Problem 2: tests that cannot fail
Look for these patterns:
- A test with no
assertand nopytest.raises. It passes as long as nothing crashes. pytest.raises(Exception), which accepts any error, including aTypeErrorcaused by a typo.- An assertion on a mock's own configuration, such as asserting that a mock returns the value you just told it to return.
assert resultwhere any non-empty result is accepted.
Tighten error checks by naming the exception and matching the message:
import pytest
from pricing import apply_discount
def test_negative_price_is_rejected():
with pytest.raises(ValueError, match="non-negative"):
apply_discount(-1, 10)Problem 3: missing cases
Compare the tests against the contract, line by line. For apply_discount, a complete set covers:
| Contract rule | Case to look for |
|---|---|
| Normal discount | At least two ordinary price and percent pairs |
| Percent from 0 to 100 inclusive | Both `0` and `100` |
| Percent outside the range | A value below 0 and a value above 100 |
| Negative price | A price below 0 |
| Rounded to two decimals | An input whose exact result has more than two decimals |
If the tests compare floats, check how. Exact == works for values like 90.0. For results of other arithmetic, pytest.approx is safer.
Check that the tests can fail
The strongest review step is to break the code on purpose and run the tests. This is a small, manual form of mutation testing. If the tests still pass, they do not protect that behavior.
Make a temporary change in a copy or a scratch branch. For example, change the range check from 0 <= percent <= 100 to 0 < percent <= 100:
if not 0 < percent <= 100:
raise ValueError("percent must be between 0 and 100")A good suite fails here, because 19.99 with 0 percent should be valid. Try a few more changes:
- Change
<= 100to< 100. - Change
price < 0toprice <= 0. - Change
round(..., 2)toround(..., 1).
Revert each change before the next one. If a change survives, ask the assistant for a test that kills it, and say which change you made.
Review checklist
- Every test has at least one assertion that can fail.
- Expected values come from the contract, not from the code.
- Error tests name the exception type and, where useful, the message.
- Boundaries and each error path appear in the tests.
- Mocks replace only external dependencies, and the test still asserts on real behavior.
- Test names say what behavior is checked.
- The function under test was not edited to make tests pass.
Exercise
A generated test calls applydiscount(100, 10) and asserts the result equals 100 * 0.9. You change the formula in applydiscount to price * (1 - percent / 10) and the test still reports a failure only by luck of rounding. Which two review steps would show that this style of test is weak, and what would you replace the expected value with?
Show answer
Reading the assertion shows that the expected value restates the formula, so it is tautological. Breaking the code on purpose and running the suite shows whether the tests catch the change. Replace the expected value with a figure worked out from the docstring, such as 90.0 for 10% off 100, and add a second pair such as (80, 25, 60.0).