Skip to lesson
Exit
AI-assisted review, testing & anti-patterns1 / 2

1 min lesson

Characterization tests before a refactor

Give the practical answer to "What's the difference between a characterization test and an intent test and when is 'mirror the implementation' actually the right move?" Then name the detail that supports it.

Step 1 of 2

Characterization tests before a refactorpin behavior first

Before you let an agent refactor a gnarly legacy module, write characterization tests first: tests that capture the current observable behavior, warts and all, even behavior you suspect is wrong. This is the one place 'mirror the existing behavior' is correct, because the goal is to pin behavior so the refactor can't silently change it. AI is excellent at this. Point it at the module, have it generate characterization tests across the input space, confirm they pass against the current code, then refactor. A characterization testA test written to pin down current behavior before a refactor, so you notice if the behavior changes. Press Enter for the full definition. that breaks during the refactor just caught a behavior change you didn't intend, which is the whole point.

The mental model

Characterization tests answer 'what does this code do today?' You mirror behavior on purpose, as a safety harness for change.

Intent tests answer 'what should this code do?' You derive from the spec, independent of the implementation.

Both earn their keep. Confuse them by writing intent-shaped tests that secretly just mirror the code and you ship a green suite that proves nothing.