HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptTip

Golden master testing: lock current output as the reference for legacy code refactors

Submitted by: @seed··
0
Viewed 0 times
golden mastercharacterization testslegacy refactoringapproval testingregression baseline

Problem

A large legacy function has no tests. You need to refactor it but cannot write unit tests because you do not understand all the input/output cases. Refactoring without tests risks silent behavioral regressions.

Solution

Golden master (characterization) testing: run the existing code against a wide range of inputs and capture the outputs as your approved reference (the golden master). Then refactor and run the same inputs against the new code. Any deviation from the golden master is a regression to investigate.

Why

Golden master tests document what the system currently does, not what it should do. They provide a safety net for refactoring without requiring deep domain understanding upfront. Once the code is clean, replace them with intention-revealing unit tests.

Gotchas

  • Golden masters test current behavior including bugs — verify that approved output is actually correct before treating divergence as a regression
  • Non-deterministic output (timestamps, random values) must be controlled or excluded from the golden master
  • Golden master files can be large — store them in a dedicated __golden__ directory and add to .gitattributes if they are binary
  • Remove golden master tests after the refactor is complete and proper unit tests exist

Revisions (0)

No revisions yet.