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.
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.