Golden File Testing

4 months ago 21

May 28, 2025

Zürich, Schweiz

Today, driven by annoyance, I refactor-extracted a small set of test helper code found in various private personal projects around the topic of golden file management into a dedicated Go module: github.com/matttproud/goldentest (API documentation). I’m happy to finally have a canonical solution that I am happy with.

I’ll freely admit it: I use change detector tests1 for some things, as a slew of these personal projects use reference data defined in the testdata directory as canonical want values that got is compared against. It’s practical: for code that generates HTML files, I can inspect the files in a browser or local web server and verify behavior conformance. Same goes for end-to-end tests that generate output. In select cases, it is easier than redefining onerous data literals in my test themselves — not that I like indirection all that much.

So what compelled me to have written my own testing helpers around golden files? It’s rather simple: I found the existing solutions not particularly idiomatic; and I don’t say that to chide any of them.

Long story short: I wanted a small, minimalist, and opinionated framework for checking whether data values generated by a system under test (SUT) match reference values.

By “minimalist” and “opinionated,” I want something that is:

  1. Effectively the spiritual companion to package cmp but for management of files in the testdata directory.

    It does relatively little but is extensible (see the package’s example to see what I mean by extensibility); I have this library even driving an image diffing protocol that works with external viewer tools

  2. Conformant with prevailing Go style guidelines whereby the diffing mechanisms do not act as assertions. They merely present a diff and update the reference values in the testdata directory.

Anyway, perhaps this library is useful to you. I’ve converted seven projects over to it, so I’ll consider it a win.

Navigation:

Read Entire Article