Fig. 1 in words
How a property-based test runs. Floats broke the associativity rule; the tool reported a failing case.
one property · many inputs
- 1 · Generate: a, b, c in [0, 10] → 2 · Check: (a+b)+c == a+(b+c)
- Check is joined by a dashed line to a dashed box: Property · for ALL inputs
- pass · next input: a dashed arrow from Check back to Generate
- fail: an arrow from Check down to 3 · Shrink: a simpler failure, with a dashed loop: repeat
- Shrink → 4 · Report: 3.03…, 0.99999, 1.0
01 What a Test Is
- A claim, checked by running the code
- Example test: one input, one answer
Dijkstra (1969): "Program testing can be used to show the presence of bugs, but never to show their absence!"
02 Property Tests
- State a rule for all inputs, not one
- The tool generates cases and shrinks failures
QuickCheck (Claessen & Hughes, 2000) popularised the idea; Hypothesis brings it to Python.
03 Floats Need Tolerance
- 0.1 + 0.2 == 0.3 is False
- Compare with math.isclose, not ==
Floats round at every step, so the order of operations can change the result (Goldberg, 1991).
04 Phase 4 Review
- Sprint 9: gradient vs finite differences
- Sprint 10: every row of the truth table
- Sprint 11: angle excess vs area
Three checks: numerical, exhaustive, and against a known answer.
05 Try It Yourself
sprint12_testing_phase_review.py
import math
import hypothesis as h
x = h.strategies.floats(0, 10)
@h.settings(derandomize=True)
@h.given(x, x, x)
def test_exact(a, b, c):
assert (a+b)+c == a+(b+c)
@h.given(x, x, x)
def test_close(a, b, c):
assert math.isclose((a+b)+c,
a+(b+c), abs_tol=1e-9)
Output, as printed on the image
pytest: 1 failed, 1 passed
a=3.0338…, b=0.99999, c=1.0
Run it. sprint12_testing_phase_review.py · versions, data and steps
06 General Notes
- Test the rule, not one example
- Keep each failure as a test
- Floats: a tolerance, never ==
- A green run is evidence, not proof
07 Mini Project
- Write 3 properties for Sprint 9's code
- Run Hypothesis; keep each failure
- Swap == for isclose where needed
- Run every test in CI (Sprint 3)
R Trusted Sources
- Dijkstra (1969). Notes on Structured Programming (EWD 249)
- Claessen & Hughes (2000). QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs. ICFP
- Goldberg (1991). What Every Computer Scientist Should Know About Floating-Point Arithmetic
- Hypothesis documentation
- Python documentation, math.isclose (PEP 485)
