Characterization Tests for Untested Code

Characterization Tests for Untested Code

Feathers' definition of legacy code, characterization tests that pin current behavior bugs included, seams for stubborn code, and why pinned is not the same as correct.

Michael Feathers defines legacy code simply as code without tests — because without tests, there is no way to know whether a restructuring changed behavior. Characterization tests break the circular trap by capturing what the code does now, bugs included, rather than what it should do. Seams are the injection points that let stubborn code be observed at all, and the harness only needs to cover the surface the specific refactor threatens. Pinned behavior is not verified correctness — discovered bugs get recorded, not silently fixed, because fixing mid-refactor destroys the exact guarantee the exercise exists to provide.

The legacy dilemma

Michael Feathers' definition, stated in Working Effectively with Legacy Code and worth taking literally: legacy code is code without tests. Not code that is old, not code that is ugly — code without tests, full stop. The reasoning behind that narrow definition is what makes it useful: without tests, there is no mechanical way to tell whether a restructuring changed the code's behavior. Everything downstream of "is this refactor safe" depends on having that answer, and untested code cannot supply it.

This creates a circular trap that anyone who has touched an old, uncovered module will recognize. Refactoring safely requires tests as a safety net. But getting genuinely untested code under test is itself often difficult without some restructuring first — the code was not written with testability in mind, so testing it requires exactly the kind of structural change that testing is supposed to make safe. The discipline in this article exists to break that circle with small, careful, low-risk moves rather than either giving up or bulldozing through.

It is worth being explicit about what refactoring untested code without any net actually is: not bravery, but risk transfer. The risk of a regression does not disappear because nobody wrote a test for it — it moves downstream, to whoever happens to hit the broken behavior in production, at a time and place no longer connected to the change that caused it. The engineer who made the change looks fast in the moment; the cost simply reappears later, attached to someone else's incident, with none of the context that would make it quick to diagnose.

This is why "just be careful" is not an adequate answer to untested legacy code. Careful reading catches the changes you thought to check. It does not catch the caller three modules away that depended on a return value's exact shape, or the edge case that only shows up for one customer's unusual data. A characterization test does not require the engineer to anticipate every consequence — it only requires capturing what already happens, which is a mechanical task rather than an act of prediction.

Characterization tests

The defining move is an inversion of how tests are normally written. A characterization test captures what the code does right now, not what it should do — current behavior, including any bugs, becomes the baseline the test asserts against. This is deliberately different from a specification test, which encodes intended behavior and fails when the code deviates from a design.

Writing one is mechanically simple: call the code under test, assert a placeholder value you know is wrong, and let the failing assertion tell you what the real value is. Once you know it, put it in as the assertion. The test now documents reality exactly as it stood before any structural change, and any future change that alters that reality will make the test fail, which is precisely the signal a refactor needs.

For code that produces complex output — a large data structure, a rendered document, an aggregate report — writing individual assertions for every field is impractical. Approval-style testing handles this case: capture the entire output once, review it by hand to confirm it looks right, save it as the reference, and diff every future run against that saved reference. One approved reference can stand in for the many field-by-field assertions that intricate output would otherwise need.

Seams: getting a grip on stubborn code

Feathers' other key concept, and the one that makes characterization tests possible for code that actively resists testing, is the concept of a seam: a place where you can alter behavior in your program without editing in that place. A seam is what lets a test instantiate a piece of code in isolation and observe it, even though the code was never designed with that in mind.

The minimal moves for introducing seams are deliberately small. Extracting a hard-wired dependency into a constructor or function parameter turns an untestable internal detail into something a test can substitute. Wrapping a global variable or singleton access behind a thin interface gives a test a point to intercept. Introducing a constructor argument where a class previously reached out and grabbed what it needed is the same idea applied at the class level. Each of these is a tiny, mechanical, low-risk edit made for exactly one purpose: to enable observation, nothing more.

There is an honest tension worth naming directly. These enabling edits are themselves unverified changes — the very thing this whole exercise exists to guard against — because they happen before any characterization test exists to check them. The discipline's answer is not to pretend this risk away but to keep these specific edits as small and mechanical as humanly possible: extract a parameter, do not touch logic; wrap a global, do not rename anything else nearby. This is the step that calls for particular care, precisely because it is the one step with no test watching it yet.

Scope the harness to the refactor

A tempting but wasteful instinct is to characterize an entire module before touching any of it. Pin only the surface the specific refactor actually threatens — renaming one function does not require characterizing the whole file it lives in, and characterizing the whole file inverts the cost-benefit of what should be a quick, targeted safety net.

Some characterization tests are disposable, meant to retire once the refactor lands and proper specification tests exist in their place. Others earn a permanent spot and graduate into the regular suite, because they turned out to document behavior worth protecting going forward. Which category a given test belongs to is a judgment call made per test, not a blanket policy.

text
Sufficiency check before starting the refactor:
- List every code path the restructuring will touch
- Confirm each path has at least one characterization test
- Confirm each test fails loudly (not silently) if that path's behavior shifts
- Stop adding tests once this list is satisfied — no further coverage needed

The harness is sufficient — no more, no less — when every path the restructuring will touch fails loudly if its behavior shifts during the work.

What pinning does not give you

Pinned behavior is not the same thing as verified correctness, and confusing the two is a real risk. A characterization suite asserts fidelity to the past — including whatever bugs the past contained — and it makes no claim at all about whether that past behavior was actually right.

Bugs discovered while writing characterization tests should be recorded, not silently fixed in the moment. Fixing a bug mid-refactor destroys the exact guarantee the whole exercise exists to provide: that behavior did not change during the restructuring. A bug fix is itself a behavior change, and mixing it into a behavior-preserving refactor makes both harder to verify. The honest move is to note the bug, keep the characterization test asserting the current (buggy) behavior, finish the refactor, and address the bug afterward as its own separate, clearly labeled change.

There is a coverage-theater risk worth naming plainly: a fully green characterization suite built over the wrong surface delivers confidence it has not actually earned. The suite's real value is exactly the paths it pins — no more, no further, regardless of how reassuring an all-green run looks from the outside.

None of this makes characterization testing a substitute for real specification tests over the long run. It is a bridge — a way to make a specific, scoped refactor verifiable today, on code that has none of the safety net a newer module might have. Once the restructuring lands and the code is shaped for testability, some of the characterization tests graduate into real assertions of intended behavior, and the rest can be retired once they have served their purpose. The discipline exists to get from untested and unsafe to refactored and verifiable, one narrow, honest step at a time — not to make every legacy module fully specified in one pass.

Project Inquiry