Approval Testing for Complex Outputs
You'll learn how to use approval (snapshot) testing to characterize legacy systems that produce large, complicated outputs you can't easily verify with traditional assertions.
The Core Problem
When working with legacy code, you often encounter functions that generate massive outputs: multi-page HTML reports, complex JSON structures, formatted text files, or rendering instructions. Writing traditional assertions to verify every field would take hours and become brittle maintenance nightmares. Characterization tests helped you capture what the system does—but how do you verify outputs with hundreds of lines?
What Is Approval Testing?
Approval testing (also called snapshot testing) works differently than assertion-based tests. Instead of coding specific expectations, you:
- Capture the actual output and save it to an "approved" file
- Compare future test runs against that approved baseline
- Review any differences—if intentional, approve the new version; if bugs, fix them
Think of it like a photo memory test: you take a picture of what the system produces today, then check if tomorrow's picture matches. Any differences trigger a review.
How It Works
When you first run an approval test on legacy code you don't fully understand, the test captures the current output (warts and all) as your baseline. This becomes your safety net—the test will fail if anything changes, forcing you to investigate whether the change was intentional or a regression.
You're not claiming the current behavior is correct, just that it's known. As you refactor, you can confidently make structural changes while preserving observable behavior.
When to Use It
Approval testing shines for:
- Complex data structures (nested JSON, XML)
- Generated documents or reports
- Formatted text output
- Any output too large to verify line-by-line with assertions
Key Takeaway: Approval testing lets you characterize complex legacy outputs by comparing against saved snapshots, making behavior changes visible without writing exhaustive assertions.