Golden File Testing
10 points by napkindrawing
10 points by napkindrawing
Golden File Testing is also known as Snapshot Testing. There’s no Wikipedia article under either name, so here’s a summary for anyone who might not know this neat testing strategy.
An ordinary test case takes the form test_assert(actual, expected)
, and if actual != expected
then it fails with a message giving the value for actual
and for expected
and saying that they’re not equal.
A snapshot test takes the form test_snapshot(actual)
. The first time you run it, it succeeds and records the result (typically in a file). Every subsequent time you run it, it compares actual
to the recorded result and fails if they’re different, showing the diff. You can then either (i) realize this diff is bad and go fix your code, or (ii) explicitly tell the test framework that the new output is good and should replace the recorded result.
There are a lot of details that can vary. Like, if the snapshot is of text then there’s a question of how the text diff is shown. Or if it’s of an image, how do you show that? Side by side comparison? But that’s the basic idea.
I’m sad it doesn’t have a Wikipedia article. It’s a little niche (you should almost always use an ordinary test if it’s feasible) but snapshot testing is really nice when you need it.
I know it as “golden master testing” and Wikipedia has Characterization Test. That article focuses on testing against a preexisting system, but the key definition in my mind is lack of true specification: we decided at some point that this output is correct by some unknown means (judgement, consensus, heritage, etc.) and test against that going forward.