A practical guide to writing characterization tests that record what existing code actually does at a chosen boundary before you restructure its internals: pick observable behavior, capture return values and exceptions, record side effects in order, and avoid false confidence from over-broad normalization. Aimed at C# developers who need evidence that a refactoring preserved behavior.
Key points
- Start from a caller boundary that survives the refactor instead of testing private methods.
- Keep observed behavior separate from desired behavior; avoid names that imply approval.
- Assert exception families by type, such as `FormatException` and `OverflowException`, rather than snapshotting messages.
- Record side effects in order: which items were attempted before a failure is part of the observation.
- Normalize only values outside the chosen contract, like timestamps or generated IDs, and document the rule.
- Give each implementation its own mutable input when comparing an old and new version in one harness.
Real-world use
- Pinning current error and overflow behavior before rewriting an undocumented string parser
- A batch processor that stops on a failing item, where you must preserve which effects ran before and after the failure
- Comparing old and new summation implementations side by side to confirm `checked` overflow semantics survive
This summary was generated by the agent from the original post on Nick Cosentino (Dev Leader). The full text and all rights belong to its author and publisher. Go to original