Go ships testing in the toolchain. Put tests in files ending in _test.go, write functions named TestXxx(t *testing.T) using the standard testing package, and run them with go test ./.... The same tool also handles benchmarks, fuzzing, coverage and the race detector, so most projects need no external test framework.
Quality assurance in Go is a combination of table-driven unit tests, integration tests, go vet and a linter, race detection and coverage checks in CI.
A test file lives next to the code it tests. This example tests an Add function and uses t.Errorf so the test continues after a failure and reports every problem.
Run it with the commands below.
Use t.Fatalf when the rest of the test cannot continue, for example after a failed setup step.
Table-driven tests run many cases through one test function. Combine them with t.Run so each case is reported by name and can be run on its own with -run.
Since Go 1.22 every loop iteration has its own tc variable, so calling t.Parallel() inside the subtest is safe. Before Go 1.22 you had to copy it with tc := tc.
Mark shared setup functions with t.Helper() so failures point at the calling test, and register teardown with t.Cleanup. For temporary files use t.TempDir(), and for environment variables use t.Setenv().
Go favours small interfaces for mocking. Define the interface where it is used, and pass a hand-written fake in tests. Generated mocks are optional.
For HTTP code, net/http/httptest gives you a recorder for handlers and a test server for clients.
Keep slow tests that need a database or network separate. A common approach is to skip them in short mode, or to use a build tag.
Run only fast tests with go test -short ./....
Benchmarks measure performance. Since Go 1.24 you can write them with b.Loop(), which keeps the compiler from optimising the work away.
Fuzzing, built in since Go 1.18, generates inputs to find crashes and edge cases.
Run it with go test -fuzz=FuzzAdd -fuzztime=30s ./calc.
| Tool | Command | Purpose |
|---|---|---|
| Race detector | go test -race ./... | Finds data races in concurrent code |
| Coverage | go test -cover -coverprofile=c.out ./... | Shows which lines tests execute |
| Coverage report | go tool cover -html=c.out | Browsable per-line coverage |
| Vet | go vet ./... | Catches suspicious constructs |
| Linters | golangci-lint run | Runs many static analysers at once |
| Vulnerabilities | govulncheck ./... | Reports known vulnerable dependencies |
| Formatting | gofmt -l . | Lists files that are not formatted |
Run these in your continuous integration pipeline on every pull request. Treat race detector failures as real bugs.
package calc_test) when you want to enforce that.time.Now() directly in logic you test.reflect.DeepEqual, or slices.Equal and maps.Equal for simple cases, and go-cmp when you need readable diffs.testdata/ directories for fixtures; the Go tool ignores them when building.No. The testing package is enough, and many teams use it alone. Libraries such as testify add assertions and mocks if you prefer that style.
Use go test -run '^TestAdd$' ./calc. The argument is a regular expression, and you can target subtests with a slash, for example -run 'TestAddTable/zero'.
Put the test in the same package (package calc), and it can call unexported identifiers. Prefer testing through exported behaviour where possible.
Go caches passing results when inputs are unchanged. Run go test -count=1 ./... to force a fresh run.
Go's built-in tooling covers unit tests, benchmarks, fuzzing, coverage and race detection with one command. Write table-driven tests with subtests, keep integration tests separable, use httptest for HTTP, and run go vet, golangci-lint, govulncheck and go test -race in CI.