liveBook · Manning

Test fixture

As explained in Learn Go with Pocket-Sized Projects

A test fixture is prepared input or reference data used to evaluate software under test.

A test fixture is prepared input or reference data used to evaluate software under test. For generated HTML, store the expected output as a file in testdata; this reference is commonly called a golden file. section 11.1.1

A practical fixture-based HTTP test follows these steps:

  • Create an httptest response recorder and invoke the handler. section 11.1.1
  • Compare the generated body with the fixture, while recognizing that strict equality makes whitespace changes significant. section 11.1.1

Golden HTML fixture

Keep the expected page in testdata/index.html rather than embedding HTML in a Go test. The test then compares the handler response with that reference and detects output changes automatically. section 11.1.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains fixture placement and strict comparison, but stops short of whitespace-insensitive HTML testing.
Examples
Uses testdata/index.html as expected output for a recorded HTTP response.
What it assumes
Assumes familiarity with Go handlers, tests, and httptest response recorders.
What you can do after
Evaluate generated HTTP output against a maintained reference file and detect unintended changes.
How it connects
Connects test fixtures with unit testing, golden files, HTTP testing, response recorders, and HTML output.

Also explained in

Compare how 10 books explain test fixture →

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.