Overview
go test compiles each package's _test.go files together with the package into a test
binary, then runs it — there is no external runner, no framework to pick. This note covers
the command; testing strategy (what to test, how to structure it) lives under
/go/testing/.
Key points
- Test binary model: a generated
TestMain/testmain harness calls everyTestXxx,BenchmarkXxx,FuzzXxx;go test -cemits the binary itself, runnable on another machine — handy for embedded targets and containers. - Selectors:
-run 'TestName/subtest'(regexp over test and subtest names),-bench .for benchmarks (off by default),-fuzz FuzzXfor native fuzzing (1.18+). - Test cache: passing results are cached per package+inputs; unchanged packages report
(cached)instantly.-count=1forces re-run (the idiomatic "no cache" flag);-count=Nalso repeats tests to shake out flakes. -raceis the flag that earns its keep: run it in CI for any concurrent code.-timeout(default 10m) panics with full goroutine dumps on hang — often the fastest deadlock diagnosis you'll get.- Coverage:
-coverfor the percentage,-coverprofile=c.outfor a profile handed to go tool cover;-covermode=atomicwhen combined with-race. -v,-short,-shuffle on: verbose output, skip slow tests guarded bytesting.Short(), randomize execution order to catch inter-test coupling.- Table tests fit the tool: one
TestXxxiterating cases witht.Run(name, …)gives each case its own selector path and parallelism (t.Parallel()) — the runner's model and the community idiom reinforce each other.
Examples
go test ./... # everything, cached
go test -race -count=1 -timeout 2m ./... # CI gate
go test -run 'TestParse/empty_input' -v ./pkg # one subtest
go test -bench BenchmarkDecode -benchmem ./codec
Related
- The Go toolchain — parent catalog.
- Testing in Go — the strategy map this command executes.
- go tool cover — where the coverage profile goes next.
- go build, go run, go install — the shared build machinery and cache.