See why a test misses a bug
Optional arithmetic and percentage refresher — return to this activity
A bug is behavior that violates a stated rule. A test supplies an input, an expected result and a comparison. A traceback identifies where Python failed; the final exception explains the immediate failure, not necessarily its original cause.
Python workshop: public teaching inputs
All people, records and numbers in this pack are authored teaching examples. They are not records or performance claims about real organizations.
- PY1 · Price rows are [[2, 3], [4, 2]]: each row contains quantity then credits per item. Expected line totals are 6 and 8 credits. Lists use zero-based indexing: the first item has index 0.
- PY2 · A function accepts input and returns a result. The supplied workshop demonstrates line_total(quantity, price). A branch selects an action; a loop repeats it. Negative quantity is rejected, not silently changed.
- PY3 · A test compares observed behavior with an independently calculated expectation. A passing example alone is not proof for every input. Empty, zero and malformed inputs need stated behavior.
- PY4 · Run the downloadable Python workshop locally. The page retains notes, not executed Python. Imported or pasted code is inert text. The separate reproducibility module supplies a second, scientific comparison starter.
Follow one order through the calculation
orders[0][0]orders[0][1]2 * 3The first index selects a row; the second selects a column. A second row [4, 2] contributes 8 credits: the running subtotal goes 0 → 6 → 14. Add a two-credit delivery fee once, after both rows, to get 16.
Compare the faulty addition: 2 + 3 = 5. This input reveals a bug that 2 + 2 = 2 × 2 would hide.
Read the code with the example
Two equals signs compare values; assert fails when the comparison is false. The second input distinguishes the intended multiplication rule from the faulty addition. Keep the expected value from an independent calculation. The downloadable tests use unittest assertions, which remain active independently of Python optimization flags.
def broken_total(quantity, price):
return quantity + price
assert broken_total(2, 2) == 4 # passes by coincidence
assert broken_total(2, 3) == 6 # fails: actual value is 5Fixed worked example, not an in-browser interpreter. Predict a changed input before running your own saved file.
Worked example
A broken total adds quantity + price. Input (2, 2) accidentally returns 4, also the correct product. Input (2, 3) returns 5 instead of 6 and exposes the bug. A passing easy case is therefore weak evidence. Test zero and negative quantities separately.
Check the boundary
A passing test only checks its declared case. Keep an independently calculated expectation and add inputs that distinguish a plausible bug from the intended rule. Changing an expectation to fit a bug hides the failure.