Testing Go code

The Go course already teaches the testing package: the lesson on go test and test files writes the first test, and table tests and subtests turn it into a table. This group is the layer above the lessons: the command a developer and a CI job type, the libraries a team argues about, the doubles behind an interface, and the tools that find what a passing test does not.

Every output on these pages came from a run in this container: the same Go version and the same machine, named at the end of each page. Two tools, staticcheck and golangci-lint, are not installed here, and the page that needs them says so and shows their commands as text instead of inventing their output.

Test The go test command What go test builds, the flags a CI job uses, the exit status of a failure, the -json stream and the test cache. Open Test Assertions: the standard library and the libraries Plain if checks against testify, go-cmp and is, the failure each one prints, and the dependency each one adds. Open Test httptest, fakes and mocks A handler tested with httptest, a fake store behind an interface, and a gomock mock that fails when the call is missing. Open Test Golden files and testdata A test that compares against a file in testdata, the -update flag that rewrites it, and the diff a mismatch prints. Open Test Fuzzing A fuzz target in the standard library, a two-second run with its coverage line, the crasher it writes, and the seed corpus that keeps it. Open Test Benchmarks, the race detector and coverage A benchmark with -benchmem, the profile it writes and the top of it, a data race the detector reports, and what coverage counts. Open Test go vet, staticcheck and CI A printf mistake go vet reports, the tools this image does not have, and the CI steps that run what is here. Open

Which tool when

Job Tool Page
Run the tests before a commit go test ./..., -count=1, -race The go test command
Compare two values and print both testing, testify, go-cmp Assertions
Call an HTTP handler without a network httptest httptest, fakes and mocks
Replace a database or a service in a test a hand-written fake; gomock when the calls matter httptest, fakes and mocks
Check a long, stable output a golden file in testdata Golden files
Find an input nobody thought of go test -fuzz Fuzzing
Measure speed or allocations go test -bench -benchmem, pprof Benchmarks
Prove there is no data race go test -race Benchmarks, the race detector and coverage
Find code the tests never ran -coverprofile, go tool cover Benchmarks, the race detector and coverage
Find a mistake before a test does go vet, staticcheck, golangci-lint go vet, staticcheck and CI

The pages do not repeat the lessons. A reader who has never written a test in Go should start with the first testing lesson and come back here when the question is which flag or which library to use. The capstone projects use the same tools on a real package: the JSON parser is tested in table tests and fuzzing, and the notes service in httptest and a fake store.

The Go programs of this group were run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container. staticcheck and golangci-lint are absent from that image, and no output of theirs appears in the group.