I need to change this code, but it has no test!
This is the title of a talk I gave at ConFoo 2020. Can you relate?
I present a technique known under different names:
- Approval Tests
- Characterization Tests
- Golden Master
- Snapshot Tests
The purpose of the technique is to capture the existing behavior of system to create regression tests that will tell you if it changes.
It's used to quickly put Legacy Code under tests so you can refactor it safely.
Have you ever felt helpless, facing some cryptic code you had to change? You know, the kind of code that has no test to tell you if something breaks…
This is a scenario I frequently faced. And I learned better ways to work with such Legacy Code.
In this session, I'll show you how to write tests on existing code, even when you don't understand what it does. You'll learn how to modify this code and know that nothing broke when you’re done.
- Slides of my talk
- jest-extended-snapshot which provides Jest matchers to write Approval Tests
- Emily Bache's Gilded Rose kata starter in many, many languages
🎓 Related articles
- Refactoring, Unit Tests, and Symmathesy
5 great talks on Legacy Code that I had the pleasure to host. Learn how to progressively migrate existing codebase towards a better state.
- Approval Tests, TCR, and Menders
5 great talks on Legacy Code that I had the pleasure to host. Learn how to test existing code and approach unfamiliar codebases.
- 5 coding exercises to practice refactoring Legacy Code
Feeling overwhelmed by your legacy codebase? These katas will help you learn how to tackle it.
- 3 steps to add tests on existing code when you have short deadlines
Here's a recipe you can follow when you want to add tests on Legacy Code, but you don't have much time to do so.