liveBook · Manning

Snapshot comparison

As explained in Learn Go with Pocket-Sized Projects

A golden file is a test fixture containing expected output that is compared with output produced by software.

Snapshot comparison checks software output against a stored reference. In Go tests, that reference is commonly a golden file: a fixture containing the expected contents, often kept in a testdata directory. section 11.1.1 section 11.6

A practical HTTP response comparison follows these steps:

  • Record the handler response with httptest.NewRecorder. section 11.1.1
  • Read the expected artifact, such as testdata/index.html, with os.ReadFile. section 11.1.1
  • Compare the stored contents with rr.Body.String() and report read errors separately. section 11.1.1
  • Treat exact whole-output equality cautiously: insignificant tabs and carriage returns can make a test fail. section 11.1.1

Checking a generated HTML page

A handler writes an HTML index page, while the test loads testdata/index.html and compares it with the recorded response. This automates detection of changes without placing the expected HTML inside the Go source file. section 11.1.1 section 11.6

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines the fixture workflow and demonstrates strict equality, but stops short of more selective HTML assertions.
Examples
Uses an HTML index response, testdata/index.html, httptest.NewRecorder, os.ReadFile, and strict string equality.
What it assumes
Assumes a Go HTTP handler, response recording with httptest, file reading, and basic test assertions.
What you can do after
Apply it via Go tests to detect changes in generated responses and diagnose failures caused by unexpected output or whitespace.
How it connects
Connects golden files with HTTP testing, httptest, test fixtures, response recording, and HTML templates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.