go test flags
go test compiles test files and runs them. Flags select packages, names, race detection, fuzzing, benchmarks, cache, and verbosity. CI should pin the flags that define "green."
Detailed Explanation
go test ./... tests every package under the current module. go test . is the current directory. -run TestParse is a regexp on the test name, including subtest paths. -count=1 disables the test cache, which otherwise skips a package it thinks has not changed.
The flags you will actually use.
| Flag | Role |
|---|---|
-race | data-race detector |
-count=n | repeat; 1 skips cache |
-run / -bench / -fuzz | select tests |
-timeout | default 10m; hung tests fail |
-v | log every test |
-cover / -coverprofile | coverage |
-short | skip long tests that check testing.Short() |
-tags | build tags (integration) |
Cache. go test caches successful package results. A test that reads the clock or a live service can pass from cache after the service died. -count=1 in CI is the usual hammer. go clean -testcache is the other.
Packages vs binaries. go test on a package that is package main still works. -c compiles a test binary without running it.
What it costs. -race and -cover slow the suite. -v drowns CI logs. Default go test ./... with -count=1 -timeout 2m is a solid CI line; add -race on a subset if the full suite is too slow.
Code Examples
go test ./...
go test -race -count=1 ./internal/httpapi
go test -run TestParse/empty -v
go test -coverprofile=cover.out ./...
go tool cover -html=cover.outfunc TestSlow(t *testing.T) {
if testing.Short() {
t.Skip("skipping in -short")
}
}go test -tags integration ./...Common Gotchas
Cached passing tests hide a broken integration
use `-count=1` in CI.
`-run Parse` matches any test whose name contains `Parse`
anchor with `^TestParse$` when you mean one function.
`-timeout 0` means no timeout
a deadlock then hangs CI.
`go test` of a package with no tests still compiles the package
`go test` of a package with no tests still compiles the package