Skip to content

characterization test

A characterization test is an automated test that records what existing code does today, bugs included, so that later changes can be checked against that recorded behavior. Michael Feathers coined the term in Working Effectively with Legacy Code (2004), where it’s the first step toward safely changing code that has no tests.

The test doesn’t claim the behavior is correct. It treats the current output as the expected output and fails when that output changes. That makes it a change detector and a safety net for refactoring, where the goal is to restructure code without altering what it does.

Say a team inherits a shipping module with no tests and needs to clean it up:

Language: Python Filename: shipping.py
def shipping_cost(weight_kg, express=False):
    cost = 5 + round(weight_kg) * 2
    if express:
        cost = cost * 1.5
    return cost

Before touching it, a developer calls the function with a few inputs and observes the results:

Language: Python
>>> from shipping import shipping_cost

>>> shipping_cost(2.5)
9
>>> shipping_cost(3.5)
13
>>> shipping_cost(2, express=True)
13.5

The output holds two surprises. The built-in round() rounds halves to the nearest even number, so a 2.5 kg parcel is billed as 2 kg while a 3.5 kg parcel is billed as 4 kg. Express orders also return a float. Nobody may have intended either one, but customers have been billed that way.

A characterization test pins both quirks as they are:

Language: Python Filename: test_shipping.py
from shipping import shipping_cost

def test_half_kilos_round_to_even():
    assert shipping_cost(2.5) == 9
    assert shipping_cost(3.5) == 13

def test_express_cost_is_a_float():
    assert shipping_cost(2, express=True) == 13.5

If a refactor swaps round() for something that rounds halves up, the first test fails and the team learns about the change before customers do.

Try a few refactors of shipping_cost() below and see which of the pinned tests catch the change in behavior:

Interactive diagram — enable JavaScript to view.

How It Shows Up in Practice

A Python developer usually meets characterization tests when changing code that nobody fully understands. Typical triggers include paying down technical debt, upgrading a major dependency or Python version, and handing a refactor to an AI coding agent. In each case, the tests go in first and the code changes second.

Feathers describes a short loop for writing them. Call the code from a test, assert an expected value that’s almost certainly wrong, run it, and let the failure message report the actual value. Then paste that value into the assertion and move on to the next input.

The same idea scales up under other names. Golden master tests save a large output, such as a generated report, and diff every later run against it. Approval tests and snapshot tests automate that capture and review step. In Python, the ApprovalTests library and the syrupy plugin for pytest both work this way.

Once in place, characterization tests run in the normal test suite and in continuous integration, where they act as regression tests for the legacy code.

Common Pitfalls

Characterization tests are cheap to write, which makes a few failure modes common:

  • Unstable output: Timestamps, random IDs, and machine-specific paths change on every run. These values have to be frozen or masked, or the test becomes a flaky test.
  • Pinned bugs mistaken for requirements: A recorded quirk looks like a deliberate rule to the next reader. Naming or commenting the test as captured behavior keeps the history visible.
  • Blind approval: Large snapshots tempt reviewers to accept every diff without reading it. A test whose expected value changes in every pull request has stopped detecting anything.

Fixing a pinned bug is a separate, deliberate change. The refactor lands with the tests green, and then a follow-up commit corrects the behavior and updates the matching assertion.

Refactoring Python Applications for Simplicity

Tutorial

Refactoring Python Applications for Simplicity

In this step-by-step tutorial, you'll learn how to refactor your Python application to be simpler and more maintainable and have fewer bugs. You'll cover code metrics, refactoring tools, and common anti-patterns.

intermediate best-practices

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Nov. 22, 2026