Sub topic 6
Pinning Down Current Behavior with Characterization Tests
This chapter assumes you can write and run pytest tests and use Git branches and commits. It covers how to record what code does today, so you can tell whether an agent's refactoring changed behavior.
Why you need a safety net
A refactoring changes structure and keeps behavior the same. An agent can restructure code quickly, but it can also change behavior without saying so. If the code has no tests, nothing tells you the difference.
A characterization test records what the code does now. You run the existing code, note its output, and assert that the output stays the same. The expected values come from the running code, not from a specification. The test answers one question: did the behavior change?
Characterization tests versus ordinary tests
| Row | Ordinary test | Characterization test |
|---|---|---|
| Expected value comes from | A specification or requirement | Running the current code |
| Detects | Code that is wrong | Code that changed |
| Treats current bugs as | Failures | Behavior to keep, for now |
A characterization test may lock in a bug. That is acceptable during a refactoring. Fix bugs in a separate change so you can tell which edit caused which difference.
Writing one
Take format_price in pricing.py. Call it with a few inputs and copy the actual results into assertions.
# tests/test_pricing.py
from pricing import format_price
def test_format_price_current_behavior():
# Expected values were recorded by running the existing code,
# not derived from a specification.
assert format_price(1234.5) == "$1,234.50"
assert format_price(0) == "$0.00"Choose inputs that cover each branch of the code: typical values, zero, negative numbers, empty collections, and rounding edges. Add a comment saying the values were recorded, so a later reader does not treat them as a specification.
You can ask the agent to draft these tests. Give it a constraint that stops it from "fixing" the code while it explores:
Write characterization tests for format_price in pricing.py.
Run the existing code to find the actual outputs and assert exactly those.
Do not modify pricing.py. If you think an output is a bug, list it
in your reply and keep the test matching the current behavior.Then check the tests yourself. An agent may derive expected values from what the function "should" return. A test that fails against the unchanged code is not a characterization test.
The workflow
The order matters. The tests must pass on the original code before the agent touches anything. Commit them first, so they sit in the history as a baseline.
Common errors
- Tests that pass for the wrong reason. A test that asserts nothing specific, such as
assert result, passes after almost any change. Assert exact values. - Unstable output. Timestamps, random values, and dictionary or set ordering can make a test fail on unchanged code. Fix the clock, seed the random generator, or sort the output before you assert.
- Too few inputs. Tests that cover only the happy path let edge-case changes through. Include boundary values.
- Side effects. If the code writes files or calls a network service, replace those with test doubles so the test records only the logic.
Exercise
You ask an agent to refactor a function. Its characterization test fails after the change, and the agent proposes updating the expected value in the test so it passes. What should you do, and why?
Show answer
Reject the test edit and inspect the code change. The test recorded the behavior before the refactoring, so a failure means the behavior changed. Either revert the step or fix the code to restore the old output. Update the assertion only if you decide the new behavior is intended, and do that as a separate, deliberate change.