Sub topic 2
What makes a test worth keeping
This chapter assumes you can write and run a basic pytest test and read a Python function. It gives you the criteria you will use later to judge tests an assistant drafts. An assistant can produce many tests quickly, so the skill that matters is deciding which ones to keep.
Why keep only some tests
A test has a cost. Someone must read it, update it when behavior changes, and investigate it when it fails. A test is worth that cost only if it can catch a real defect and tell you clearly what broke. Generated tests often look correct but fail this standard.
The running example is this function, which has a documented contract and two error paths:
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Raises ValueError if price is negative or percent is outside 0-100.
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)Traits of a good test
A test worth keeping has these traits:
- It checks behavior, not implementation. It asserts on what the function promises, such as the return value or the exception, and not on how the result is computed.
- Its expected value is independent. You derive it from the docstring or from hand arithmetic. You do not compute it with the same logic as the code.
- It can fail. If you break the code, the test goes red. A test that passes no matter what protects nothing.
- It is deterministic. The same input gives the same result on every run. Clocks, random numbers, and network calls make tests flaky unless you control them.
- It is independent and fast. It does not rely on another test running first, and it finishes in milliseconds.
- It names one behavior. A failure message such as
test_apply_discount_rejects_negative_pricetells you where to look.
A tautological test and a better one
A tautological test repeats the implementation in the assertion. It passes even when the logic is wrong, because both sides share the same mistake.
from pricing import apply_discount
def test_discount_tautology():
price, percent = 100, 10
assert apply_discount(price, percent) == round(price * (1 - percent / 100), 2)If someone changes the formula to price * percent / 100, this test must be rewritten along with it, so it never signals a regression you did not already intend. Compare a test with a hand-derived value:
from pricing import apply_discount
def test_ten_percent_off_one_hundred_is_ninety():
assert apply_discount(100, 10) == 90.0Ten percent of 100 is 10, so the price drops to 90. You worked that out from the contract, so the test checks the code against something outside the code.
A quick filter
Ask these questions about each test, in order. A "no" at any step means the test needs revision or removal.
You can run the second question directly. This is called a mutation check. Temporarily change the function, for example replace <= with < in the percent check, and run the tests. If nothing fails, the boundary at 100 is untested.
Common ways tests fall short
| Weakness | Example | Effect |
|---|---|---|
| Tautology | Assertion repeats the formula | Passes when the formula is wrong |
| No assertion | Calls the function and checks nothing | Passes unless the code crashes |
| Too loose | `assert result` | Any non-zero value passes |
| Missing boundary | Never tests percent 0 or 100 | Off-by-one errors survive |
| Over-coupled | Asserts on internal calls that do not matter | Breaks on harmless refactors |
What this means for generated tests
An assistant does not know which behavior matters to you, and it cannot confirm its tests catch real defects. You supply the standard and you check the result. The next chapters show how to prompt for tests that meet these traits and how to review them.
Exercise
A generated test for applydiscount reads `assert applydiscount(50, 20) == round(50 * (1 - 20 / 100), 2)`. Name the problem and write a replacement assertion.
Show answer
The test is tautological because the expected value reuses the function's own formula, so a wrong formula would still pass. Replace it with a hand-derived value: 20% of 50 is 10, so assert apply_discount(50, 20) == 40.0.