Skip to content
Technology, explained from the insideAI · Software · Digital payments
TayoTechTalk.

AI Software Development

Characterization Tests Before an AI Refactor

By Temitayo Solomon Gbadebo · November 24, 2026
Side-by-side mazes show a complex route and a simplified route between the same endpoints.

Ask an AI coding agent to simplify a legacy export, and it may replace a tangle of branches with a clean function. It may also decide that empty exports should contain no lines. If a spreadsheet importer relies on receiving headers even when the export has no rows, the new function is clearer and the product is broken.

Before changing code, capture a few of the behaviors that matter. This is where characterization tests help: they document what the software does now, without treating every observation as a design decision.

Start with behavior, not implementation

Martin Fowler defines refactoring as changing the structure of code without changing its observable behavior. The word observable is the useful part. A client can see a file’s header and the order of its rows. It normally cannot see whether the export code used a loop or a separate formatter class. A test that locks down the loop does not protect the client.

In an interview about working with legacy code, Michael Feathers distinguishes tests of what a system actually does from tests that define correctness. The distinction matters when an AI agent infers an intent from unclear code. Ask it to identify examples worth checking, then have a person decide which results represent a contract.

Pick the smallest entry point that exposes the effect you care about. For a CSV export, call the command or export function and read the resulting file. If consumers depend on an HTTP status, content type, or file name, exercise the route too. Do not assert on private method calls just because those calls are easy to inspect.

Classify surprises before turning them into tests

Suppose you are refactoring a fictional library reservation export. Its consumers include a spreadsheet importer and a librarian who scans the rows in alphabetical order. A before-run with synthetic reservations might reveal the following:

Example decisions for a legacy library export
Observed behavior Decision
An empty export still includes column headers. Preserve. The importer uses the headers to recognise the format.
Rows with the same surname retain their input order. Investigate. Ask whether staff or an integration depends on the tie order before making it permanent.
A comma inside a book title produces an extra column. Known bug. Document it separately, and get agreement on the corrected output before changing it.

For the tie-order case, record what happened and ask the owner whether to preserve it. Until there is a decision, avoid changing it in a supposedly behavior-preserving refactor. For the known bug, don’t make broken output a permanent acceptance rule. If you need to keep track of it while restructuring, record it as a temporary comparison result with a linked issue. Make the fix a separate change with an explicit test of the intended behavior. If the bug is urgent, fix it first and then baseline the corrected version.

Capture details that customers can actually see

Prefer focused assertions that name the promise: empty export includes the header is more useful than matches export output. For an established format that consumers compare byte-for-byte, a short reviewed snapshot can be appropriate. Otherwise, check selected fields and avoid freezing generated identifiers or timestamps that are irrelevant to the contract.

If you use snapshots, review the expected output just as you would a code change. Jest’s snapshot guidance recommends keeping snapshots short and focused; it also describes tools for inspecting changes rather than accepting them in bulk. Never update baselines just to make an unexpected diff disappear.

Make the before-and-after review explicit

  1. Run the new checks on the unmodified revision and keep the output. A test that has only run on the refactor cannot show what the legacy version did.
  2. Review the assertions and identify any gaps worth testing before editing the implementation. Keep the known-bug notes and undecided behaviors visible.
  3. Have the agent restructure the code without rewriting the approved expectations. Run the same cases against both revisions under equivalent conditions.
  4. Explain every observable difference. If one was intended, give it its own rationale and revised acceptance test. If not, fix the refactor or pause for a decision.

A green run proves agreement on the cases you captured, not equivalence for every possible input. That’s enough to make the next change less speculative. The win is not asking AI to preserve everything. It is choosing what matters and making it visible before the agent changes the code.