Good Go tests are small, deterministic and run on every change: write table-driven unit tests, replace external services with interfaces and fakes, run the race detector, and debug failures with the tools Go gives you (go test -run, -v, Delve and pprof). The rest of this guide shows how, with code you can paste into a module.
The focus here is the day-to-day workflow of testing and debugging a Go package, using the standard toolchain of current Go versions.
Go finds tests by convention, so there is nothing to configure:
_test.go and live next to the code.func TestXxx(t *testing.T), benchmarks are func BenchmarkXxx(b *testing.B), fuzz tests are func FuzzXxx(f *testing.F), and examples are func ExampleXxx().package foo for white-box tests and package foo_test to test only the exported API, which also catches import cycles and awkward APIs.Commands you will use constantly:
Go caches successful test results when the inputs are unchanged. Use -count=1 when a test depends on something outside the module, such as a file or an environment variable that the cache cannot see.
A table keeps cases together, and a helper keeps assertions short. Mark helpers with t.Helper() so failures point at the caller's line.
Map iteration order is random, which is a feature here: it exposes tests that accidentally depend on order. Since Go 1.22 the loop variable is per iteration, so the closure is safe.
When code talks to a database, a payment API or the clock, depend on a small interface that you define yourself, and give the test a fake.
Hand-written fakes are usually clearer than generated mocks. Generated mocks (for example from mockgen) are useful when you must verify exact call sequences. For HTTP dependencies, run httptest.NewServer and point the client at its URL. For databases, prefer integration tests against a real instance in a container, since a mock cannot tell you your SQL is wrong.
Concurrency bugs are the hardest to reproduce, so make the tools find them:
A test that sleeps to wait for a goroutine is flaky by design. Wait on a channel, a sync.WaitGroup or a context instead. With Go 1.25 and later, testing/synctest runs code in an isolated bubble with a fake clock, so timeouts and tickers can be tested instantly and deterministically.
Start with the cheapest tool and escalate:
go test -v -run TestName ./pkg.t.Logf or, in a program, a slog.Debug call with the relevant values. Prefer %+v and %#v when printing structs.At the (dlv) prompt, use break math_test.go:20, continue, next, step, print c, locals, goroutines and bt. Delve understands goroutines and optimization settings. If variables show as optimized out, build with -gcflags="all=-N -l" to turn off optimizations and inlining.
For performance bugs and leaks, take profiles (go test -cpuprofile cpu.out -memprofile mem.out, then go tool pprof) and look at the goroutine profile for stuck goroutines.
Collect coverage and inspect the untested branches rather than chasing a number:
In CI, run gofmt -l ., go vet ./..., go test -race ./... and govulncheck ./.... Treat a failing run as a stop signal, and keep the suite fast enough that developers run it before pushing.
t.Error marks the test failed and continues. t.Fatal marks it failed and stops that test immediately, which is right when later steps would crash or mislead.
_test package?Use the same package for internal details and package foo_test for black-box tests of the public API. Many projects use both.
Common causes are shared global state, order dependence, timing assumptions, missing environment variables and data races. Try -shuffle=on, -race and -count=1 locally.
Rarely. Small interfaces with hand-written fakes cover most cases. Reach for a generator only when the interface is large or call order matters.
t.Helper().httptest.-race, -shuffle and go vet, and avoid sleeps in tests.pprof, and keep all checks in CI.