Errors, Validation, and Debugging · practice
Reproduce a Bug Reliably
A report such as “the price is sometimes wrong” is hard to investigate. A reproduction turns it into a concrete experiment:
the exact starting code;
the exact input or
call; the expected result;
the actual result.
If the same steps cause the same problem each time, you can test possible fixes without guessing whether the bug disappeared by chance.
Choose the smallest failing case
Suppose a function classifies package weights:
def shipping_label(weight):
if weight < 10:
return "standard"
return "heavy"
A useful report is:
Calling
shipping_label(10)should return"standard"because standard shipping includes weights up to 10. It actually returns"heavy".
The number 10 sits exactly on the rule’s boundary. Testing it reveals whether the comparison includes or excludes that
Test around a boundary
For a boundary at 10, check:
print(shipping_label(9))
print(shipping_label(10))
print(shipping_label(11))
The values immediately below, at, and above a boundary often expose mistakes involving <, <=, >, and >=.
This is more informative than trying many unrelated values. You are testing one assumption at a time.
Keep the reproduction while you repair
A focused example can become a regression check: after the fix, run the same example again. Also check nearby values to ensure the repair did not move the bug somewhere else.
The workflow is:
State the requirement precisely.
Find a small input that violates it.
Confirm the wrong result.
Narrow the relevant code.
Change one cause.
Rerun the failing case and nearby cases.
That final rerun turns “I changed something” into evidence that the behavior now matches the requirement.
The rule says children age 12 and younger receive a child ticket. Which test set checks the boundary most directly?
Task
The venue’s pricing rule is:
visitors younger than 18 pay
$8;visitors age 65 and older pay
$10;everyone else pays
$14.
The starter
The final output must be:
14
10
10
Do not add a special case just for the prepared calls. Make the rule work for any age.