Go ships its testing and debugging tools in the standard library and the go command. Use the testing package for unit tests, table-driven subtests, benchmarks, fuzzing and examples, net/http/httptest for HTTP handlers, go test -race for data races, -cover for coverage, and runtime/pprof or net/http/pprof plus log/slog for diagnosing running programs.
This guide covers each tool with current idioms, including testing.B.Loop and t.Context from Go 1.24.
Test files end in _test.go, and test functions are named TestXxx(t *testing.T). The idiomatic pattern is a table of cases run as subtests, so each case reports separately and can be selected with -run.
Run it with go test ./..., or a single case with go test -run 'TestAdd/negative' -v. Since Go 1.22 each loop iteration has its own tt, so t.Parallel() inside a subtest is safe without copying the variable.
Mark helper functions with t.Helper() so failures point at the caller. Use t.TempDir() for files that are deleted automatically, t.Cleanup to register teardown, and t.Setenv to change environment variables for one test.
Since Go 1.24, t.Context() returns a context that is cancelled just before cleanup functions run, which is useful for stopping goroutines a test starts.
Benchmarks live in the same files and are named BenchmarkXxx(b *testing.B). Go 1.24 added b.Loop(), which is preferred over a manual b.N loop because it prevents the compiler from optimising the work away and runs setup once.
Run with go test -bench=. -benchmem, which also reports allocations per operation. To compare two runs statistically, use the benchstat tool from golang.org/x/perf.
Fuzzing (built in since Go 1.18) generates inputs to find crashes and invariant violations.
Run go test -fuzz=FuzzReverse -fuzztime=30s. Failing inputs are saved under testdata/fuzz and rerun as regular tests. The old testing/quick package still works for simple property checks but is frozen, so fuzzing is the recommended approach.
A function named ExampleXxx with an // Output: comment is compiled, run and compared by go test, and it appears in the generated documentation.
If the printed output differs, the example fails like any test, so documentation cannot drift from behaviour.
net/http/httptest runs handlers without opening a real port.
Use httptest.NewServer when you need a real listener, for example to test an HTTP client against a fake upstream. For time-dependent concurrent code, testing/synctest (stable in Go 1.25) runs goroutines against a virtual clock so tests are fast and deterministic.
| Goal | Command |
|---|---|
| Detect data races | go test -race ./... |
| Coverage percentage | go test -cover ./... |
| Coverage report | go test -coverprofile=c.out ./... && go tool cover -html=c.out |
| CPU profile of a benchmark | go test -bench=. -cpuprofile=cpu.out then go tool pprof cpu.out |
| Memory profile | go test -bench=. -memprofile=mem.out |
| Vet for suspicious code | go vet ./... |
The race detector reports concurrent unsynchronised access at runtime, so run it in CI. For a running service, import net/http/pprof on an internal-only port to expose /debug/pprof/ endpoints, and capture goroutine, heap and CPU profiles with go tool pprof.
Start with structured logs from log/slog, which output key-value pairs that are easy to search:
When a program panics, the stack trace lists the failing goroutine. Send SIGQUIT (Ctrl+backslash) to dump all goroutine stacks, which exposes deadlocks. Use runtime/trace for scheduling and latency analysis, and the dlv debugger from the Go debugger project (dlv test, dlv debug) for breakpoints and stepping, since the standard distribution does not include an interactive debugger.
Use go test -run 'TestName'. For a subtest, separate levels with a slash: go test -run 'TestAdd/negative'.
Go caches passing results when the package and inputs are unchanged. Use go test -count=1 to force a fresh run.
No. The standard testing package is enough, and many teams add only github.com/google/go-cmp/cmp for readable diffs of structs.
Dump goroutines with SIGQUIT or the /debug/pprof/goroutine endpoint, then look for stacks that should have exited.
Write table-driven tests with subtests, use t.TempDir, t.Cleanup and t.Helper, benchmark with b.Loop, and fuzz inputs that parse data. Test handlers with httptest, run -race in CI, and use pprof, log/slog and dlv to debug running programs.