Go testing is built into the standard library: the testing package plus the go test command give you unit tests, table-driven tests, subtests, benchmarks, fuzzing, examples and coverage with no extra framework. Continuous testing means running these same commands on every commit in CI, usually with the race detector and a coverage report.
This guide walks through each feature with current idioms (Go 1.22 and later) and shows how to wire them into a pipeline.
testing packagetesting packageTest files end in _test.go, and test functions are named TestXxx with a *testing.T parameter.
Run all tests in the module with go test ./.... Use t.Errorf to report a failure and keep going, t.Fatalf to stop the current test, and t.Helper() inside helper functions so failures point at the caller's line.
Table-driven tests are the standard Go pattern: one slice of cases, one loop, one subtest per case through t.Run.
Since Go 1.22 each loop iteration has its own tc variable, so t.Parallel() inside the closure is safe without copying it. Run one case with go test -run 'TestAddTable/zero'.
Benchmarks are named BenchmarkXxx. Since Go 1.24 you can write the loop as for b.Loop(), and older versions use for i := 0; i < b.N; i++.
Run them with go test -bench=. -benchmem. Fuzz tests, available since Go 1.18, generate inputs automatically to find crashes:
Run with go test -fuzz=FuzzAdd -fuzztime=30s. An ExampleXxx function with an // Output: comment is both documentation and a test, because go test compares its printed output.
| Command | Purpose |
|---|---|
go test -cover ./... | Print coverage per package |
go test -coverprofile=c.out ./... | Write a profile |
go tool cover -html=c.out | View covered lines in a browser |
go test -race ./... | Detect data races |
go test -count=1 ./... | Bypass the test result cache |
go test -short ./... | Skip tests that check testing.Short() |
Treat coverage as a way to find untested code, not as a goal in itself. Run -race in CI on every change, because data races are invisible in normal runs.
Go favours small interfaces defined by the consumer, so a fake is often a short struct in the test file instead of a generated mock.
For HTTP code, net/http/httptest provides httptest.NewRecorder() to test a handler directly and httptest.NewServer() to start a real local server for client code. For integration tests that need a database, use t.TempDir() for files, t.Setenv() for environment variables and t.Cleanup() for teardown. Mark slow tests with a build tag or skip them under -short. Libraries such as testify or gomock are optional; the standard library is enough for most projects.
A minimal GitHub Actions job runs the same commands you run locally:
Add govulncheck ./... and a linter such as golangci-lint for deeper checks. Locally, a file watcher that reruns go test on save gives you the same feedback loop while you develop.
Use go test -run TestName ./path/to/pkg. The argument is a regular expression, and a slash selects a subtest.
go test show a cached result?Go caches passing results when the code and inputs are unchanged. Use -count=1 to force a fresh run.
Usually not. Define a small interface and write a fake by hand, which is easier to read and debug than generated mocks.
Next to the code they test, in the same directory. Use package foo_test to test only the public API.
The testing package and go test cover unit tests, table-driven subtests, benchmarks, fuzzing, examples and coverage. Use httptest for handlers, small interfaces for fakes, and run go vet and go test -race on every commit in CI to keep quality steady.