Snapshot Testing
Snapshot testing is:
Run the code.
Record the output.
Create a test that checks for that output.
Snapshot testing is the automated version of “write the stimulus part of the test, copy the actual result, & paste it in as the expected results”.
Test Desiderata
The test desiderata is a list of 12 desirable properties of tests. Different styles of tests satisfy different properties. (There isn’t one “good” kind of test, there are different tradeoffs.) How does snapshot testing “score” on the desiderata?
Isolated—probably? Snapshot tests seem likely to not affect each others’ results. I suppose it depends on the style of code under test.
Composable—likewise probably? You should be able to run a bunch of snapshot tests in any order & get the same results. Kind of the corollary of isolation.
Deterministic—yes, if the code under test is deterministic.
Specific—depends on how much code is covered by the test. At least you will have a specific error, not just “should have been non-null”.
Behavioral—🌟🌟🌟🌟🌟 snapshot tests are definitely sensitive to changes in the behavior of the code under test.
Structure-insensitive—depends on what is returned & checked for. I’ve seen snapshot tests (the manual kind) that checked for a whole huge object structure, much of which was irrelevant to the behavior being tested.
Fast—likely yes, it seems?
Readable—maybe, maybe not, depending on the complexity of the expected result.
Writable—🌟🌟🌟🌟🌟 this is absolutely where snapshot tests shine. If you kinda know you need a test but you’re reluctant to take the time to write it, a snapshot test gives you some kind of a test quickly.
Automated—absolutely.
Predictive—it depends on how many & which ones you write. If you have enough snapshot tests, “green means go” (passing the tests means the code is safe to deploy).
Inspiring—here’s where I wouldn’t choose to rely only on snapshot tests. I like manually calculating the expected value to force myself to think through the problem 2 independent ways. If I come to the same answer, I gain confidence that the code is going to do what I hope it will do.
Conclusion
Snapshot tests get high marks for being:
Behavioral
Automated
Writable
There are also other desirable properties out there, but these are a good start.
Snapshot testing is a good tactic for adding tests to existing (typically legacy) code. Especially if you’re about to refactor some confusing code where you know it works, but how it works is obfuscated.
You can exercise the current behavior, get a snapshot of the results, then refactor the code to make it clean and clear. You know it still does the same thing because of the snapshot.
You’ll learn more about what the code does during this progress so you can now write informed tests.
This sounds a lot like approval testing too me. Are they the same? If not, what is the difference?