Skip to content
mentoor

Topic 3 of 5 · 5 min read

Characterization tests before changing legacy code

Goal: Have an agent write characterization tests that pin current behaviour, and spot tests that assert the wrong thing.

A characterization test records what the code does today, not what it should do. It is a safety net. Once it is in place, an agent's change can be checked in seconds.

Check yourself · 1 of 3

What does a characterization test assert?

Choose one answer

Why it matters

Legacy code often has no tests, and nobody can say what the correct output is. You still need to change it. If an agent edits the code and nothing pins the old behaviour, a changed result will look like a successful edit. The idea comes from Michael Feathers' work on legacy code: first capture behaviour, then change it.

How to do it

  1. Pick the unit and the inputs. Choose real cases: normal ones, edges, and any odd ones from production data or tickets.
  2. Run the code and record the output. Do not decide the expected value yourself. Call the function, take what it returns, and write that into the assertion.
  3. Let the agent write the tests. Ask it to generate many input cases, run them against the current code, and fix each assertion to the observed result.
  4. Include the ugly behaviour. If the code rounds 2.675 to 2.67, or returns null for an empty list, pin it. Name the test after the behaviour, for example rounds_half_down_on_floats.
  5. Review each assertion. This is the human step. Does the test assert the real output, or what the agent believed it should be?
  6. Run the suite against the unchanged code and see it pass. Only then start the change.

Before and after

# Weak: the agent decided what is "right"
def test_discount():
    assert apply_discount(100, "VIP") == 80

If the real code returns 85, this test fails and the agent may "fix" the code to match its guess. Fixed by observing:

# Pins today's behaviour, including the surprise
def test_vip_discount_is_15_percent_not_20():
    assert apply_discount(100, "VIP") == 85

def test_unknown_code_returns_full_price():
    assert apply_discount(100, "???") == 100

What goes wrong

  • Tests that assert the agent's belief. The model fills in the "obvious" answer. Always compare against the real run.
  • Tests that copy the code. Recomputing the answer with the same formula inside the test proves nothing.
  • Weak assertions. assert result is not None passes for almost any behaviour.
  • Hiding a bug by pinning it. Pinning a bad result is fine as a first step, but mark it, for example # BUG: pins current output, see T-212, so it is fixed on purpose later.

Examples

Observed, not assumed

An agent proposes assert total(order) == 119.00 for a 100.00 order with 19% tax. The actual run returns 119.0 for most orders but 118.99 for one with three line items, because of rounding per line. The test is rewritten to pin both results, with the second named rounds_each_line_before_summing.

A test that proves nothing

assert format_invoice(order) with no comparison, or assert len(output) > 0. Both pass if the function returns almost anything. The reviewer asks for the exact string or a saved approved-output file.

Key points

  • A characterization test records what the code does today, not what you think it should do.
  • Take the expected value from a real run of the unchanged code, not from the agent's guess.
  • Pin the odd behaviour too, and name each test after it.
  • Review every assertion: it should be a specific observed value, not a copy of the code's formula.
  • Run the new tests against the unchanged code first; they must pass before any change begins.

Common mistakes

  • Letting the agent decide the expected values from the function name.
  • Writing only happy-path cases and leaving the odd branches unpinned.
  • Using the same formula in the assertion as in the code.
  • Silently pinning a bug without marking it.

Check yourself · 2 of 3

An agent writes assert discount(100, "VIP") == 80 for legacy code with no spec. The real code returns 85. What is the right fix?

Choose one answer

Check yourself · 3 of 3

Which assertion is the weakest as a characterization test of format_invoice(order)?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic