Testing
13 steps in 5 sets of Go.
Testing is in the toolchain, not a library you choose. Writing tests, table-driven cases, fakes and fixtures, benchmarks and fuzzing, and the flags that control what runs.
Thirteen steps. The conventions are strict and nobody argues about them, which is a fair summary of Go generally.
Writing tests
- A Go testA file ending _test.go, a function taking *testing.T, and t.Errorf.
- Table-driven testsThe Go house style: a slice of cases, one subtest each.
- Helpers and cleanupt.Helper fixes the reported line; t.Cleanup replaces teardown.
- Testing errorsAssert on the sentinel or the type, never on the message text.
Fakes & fixtures
- Faking a dependencyDepend on an interface and the test supplies its own implementation.
- Testing an HTTP handlerhttptest gives a recorder and a request, so no port is opened.
- Golden filesCompare against a recorded file, and regenerate it behind a flag.
Measuring & generating
- Benchmarksb.Loop in Go 1.24 replaced the b.N loop, and stops the optimizer cheating.
- FuzzingA fuzz target gets random input until it finds one that breaks the property.
- Examples are testsAn Example function with an Output comment is compiled, run and checked.
Running them
- Parallel subtestst.Parallel pauses a subtest until the others have started, then runs them together.
- TestMainOne place for package-level setup, and the exit code is yours to return.
Encore
- store_test.goA whole test file: table tests, subtests, a fake, a benchmark and a fuzz target.