Characterization tests record what existing software actually does—including bugs and undocumented rules—so its structure can change without changing its behavior. section 7.4
To establish a useful baseline:
- Run the code with realistic inputs and note surprising outputs, branches, and thresholds. section 7.5
- Test typical paths, discovered edge cases, and boundary conditions. Assert what happens, not what ought to happen. section 7.5
- After each structural change, rerun the tests; passing tests indicate that the captured behavior remains intact. section 7.4
Listing 7.10 Testing approach to capture known behavior as you see it
def test_discount_calculation_characterization():
# Capture actual behavior, even if it seems wrong
assert calculate_discount(100, 'SUMMER') == 15.0
assert calculate_discount(0, 'SUMMER') == 0.0
assert calculate_discount(-50, 'SUMMER') == 0.0 # Bug? Maybe. Current behavior.
assert calculate_discount(100, 'INVALID') == 0.0
assert calculate_discount(1000, 'VIP') == 250.0
assert calculate_discount(999, 'VIP') == 99.9 # Why does threshold matter? Document it.The discount examples capture ordinary results, invalid inputs, negative values, and a surprising threshold difference. section 7.4

