learning-it by example

Back to the video

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

  1. 0:00 A tidy refactor
  2. 0:23 The idea
  3. 0:51 Setup
  4. 1:10 The old code
  5. 1:31 Record the behaviour
  6. 1:59 The test
  7. 2:17 The tidy refactor
  8. 2:55 Are the tests any good?
  9. 3:41 What this does not show
  10. 4:08 Mistakes to avoid
  11. 4:31 My take and next steps
  12. 4:52 Recap

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)

Download the code (zip)

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.