Go ships with the tools you need to test, benchmark, profile and debug a program: the testing package and go test for unit tests, table-driven tests, subtests, benchmarks, fuzzing and coverage; the race detector for concurrency bugs; pprof and go tool trace for performance; and Delve for interactive debugging. Used together, they cover correctness, quality and performance without third-party frameworks.
This guide shows each technique with a small example and tells you when to reach for it.
Tests live in files ending in _test.go. A test is a function named TestXxx that takes *testing.T. The idiomatic style is a table of cases run as subtests.
Run it with go test ./.... Add -v for names, -run 'TestDivide/zero' to select one subtest, and -count=1 to bypass the test cache. Since Go 1.22, each loop iteration has its own tc variable, so t.Parallel() inside the loop is safe without copying it.
Useful helpers: t.Helper() marks a function as a helper so failures point at the caller, t.Cleanup() registers teardown, t.TempDir() gives a temporary directory, and t.Setenv() sets an environment variable for the test.
Use net/http/httptest to test handlers without opening a port, and interfaces to replace databases or APIs with fakes.
For code that calls another service, httptest.NewServer starts a real local server you can point the client at. For time-dependent code, inject a clock function instead of calling time.Now() directly. In Go 1.25 and later, the testing/synctest package also lets you test concurrent, time-based code with a fake clock.
Benchmarks are named BenchmarkXxx and take *testing.B. Since Go 1.24 you can loop with b.Loop(), which also stops the compiler from optimising the work away.
Run go test -bench=. -benchmem to see time per operation and allocations. On older Go versions, use for i := 0; i < b.N; i++. Compare two runs with the benchstat tool from golang.org/x/perf rather than eyeballing single numbers.
Fuzzing, built in since Go 1.18, generates inputs to find panics and logic errors:
Run go test -fuzz=FuzzReverse -fuzztime=30s. Failing inputs are saved under testdata/fuzz and run as regular tests afterwards.
Other flags worth knowing:
| Command | Purpose |
|---|---|
go test -cover | Print statement coverage |
go test -coverprofile=c.out then go tool cover -html=c.out | Browse covered lines |
go test -race ./... | Detect data races at run time |
go test -shuffle=on | Randomise test order to expose hidden dependencies |
go vet ./... | Static checks for common mistakes |
Run -race in continuous integration. It slows execution and raises memory use, but a reported race is always a real bug.
When a benchmark or production service is slow, collect a profile first. Do not guess.
For a running service, import net/http/pprof on an internal-only port and fetch /debug/pprof/profile. Flame graphs in the pprof web UI show where CPU time goes; the heap profile shows allocations. Use go tool trace to inspect goroutine scheduling, blocking and garbage collection pauses, and runtime/trace to record a trace from your program.
Delve is the standard Go debugger. Install it with go install github.com/go-delve/delve/cmd/dlv@latest.
It understands goroutines, so you can list them and switch between them, and dlv test debugs a failing test. Most editors, including VS Code and GoLand, wrap Delve with a graphical interface.
For production debugging, combine structured logging with log/slog, wrap errors with fmt.Errorf("load config: %w", err) so errors.Is and errors.As still work, and use GOTRACEBACK=all to see every goroutine when a program crashes. Build with -gcflags="all=-N -l" when you need a debugger to see variables that the optimiser would remove.
No. The standard testing package is enough for most projects. Libraries such as testify add assertions and mocks, but plain if checks with t.Errorf are idiomatic.
Use go test -run '^TestDivide$'. For a subtest, add a slash: -run 'TestDivide/zero_divisor'. Spaces in subtest names become underscores.
It instruments memory accesses and reports when two goroutines access the same variable without synchronisation and at least one access is a write.
Check testing.Short() and skip with t.Skip, or guard them with a build tag such as //go:build integration, then run go test -tags=integration ./....
Write table-driven tests with subtests, use httptest and interfaces to isolate dependencies, and add benchmarks and fuzz tests where performance or input handling matters. Run go vet and go test -race in CI, profile with pprof before optimising, and debug with Delve and structured logs when behaviour is unclear.