Refactor Safely With AI: Characterisation Tests (pytest)
A tidy refactor changed the price of every heavy express parcel. Sixty four recorded cases caught it.
Video #172 ยท AI coding and productivity
Steps in the video
Files
- conftest.py (2 lines)
- legacy.py (21 lines)
- make_golden.py (16 lines)
- mutants.py (39 lines)
- pytest.ini (2 lines)
- refactor_ai.py (14 lines)
- refactor_fixed.py (14 lines)
- test_pricing.py (14 lines)
Tested with
Python 3.12.15 | pytest==9.1.1
What this does not show
No AI model was run: the refactor is hand-written in the style an assistant might use. The prices and the surcharge rule are made up. The 64 recorded cases say same as before, not correct, and are not every input. The seven breakages and the three-case set were picked by hand, not by a mutation testing tool.
Sources
Learning IT by Example.