Authoring and Fixtures
The structural pieces of a Go test file that outlive the assertion: t.Parallel ordering, fixtures under testdata/ and golden files a flag regenerates. This is where table tests break.
part ofGo (Golang)overview, primer and where to startread it →on this pageshowhide
explore
- Cleanup and Temp Dirs4 questions
- Reporting Test Failures4 questions
- External Test Packages4 questions
- Parallel Cases and Isolation4 questions
- Runnable Example Functions4 questions
- Table-Driven Tests and Subtests4 questions
- Comparing Expected and Actual5 questions
- Golden Files and testdata4 questions
questions
page 2 of 2A pull request adds a green test asserting reflect.DeepEqual(got, want) on a result struct - why might it prove nothing?
basics
~20 sA green whole-struct comparison proves nothing when want came from the same code path, or when both sides are zero values and every field trivially matches. It can also over-constrain, breaking on fields the case never meant to pin.
Reviewing a PR that regenerates a 400-line golden file under testdata, what do you check before approving?
basics
~20 sRead the diff, because a run with -update writes the file it then compares and cannot fail. Check that every hunk, especially deletions, is explained by the change, and that a plain go test passes on the committed bytes.
When has a Go test table's case struct grown too many fields, and what do you do about it?
basics
~20 sWhen the loop body branches on flags most rows leave unset, the table describes two behaviours at once. Split it into a second test function, and pick any new field's zero value so existing rows keep their meaning.
showing 31–33 of 33