What the cache keys on#
The go test documentation says that in package list mode (go test ., go test ./...) Go caches successful package results and prints (cached) in place of the elapsed time. A cached result matches when "the run involves the same test binary" and the flags come from a short list of cacheable ones. "Tests that open files within the package's module or that consult environment variables only match future runs in which the files and environment variables are unchanged." The idiomatic way to disable caching is -count=1. go test caching
The same text leaves out everything else. A test that calls an HTTP endpoint, queries a database or reads a file outside the module is replayed even if that answer changed.
Fixture: what the cache noticed and what it missed#
Go 1.26.11 in golang:1.26. The package has three tests: one reads testdata/limit.txt, one reads REGION, and one calls a local HTTP service that answers ok. The service then starts answering broken:
$ go test .
ok example.com/app 0.016s
exit 0
$ go test .
ok example.com/app (cached)
exit 0
$ echo broken > answer file served by the service
$ go test .
ok example.com/app (cached)
exit 0
$ go test -count=1 .
--- FAIL: TestService (0.00s)
app_test.go:25: service said "broken\n"
FAIL
FAIL example.com/app 0.010s
FAIL
exit 1
$ echo 200 > testdata/limit.txt
$ go test .
--- FAIL: TestLimitFile (0.00s)
[…]
$ REGION=us go test .
ok example.com/app 0.009s
exit 0
Changing the test data and the environment variable each made Go run the binary again. Breaking the service did not: the third run replayed a pass and exited 0 while the service it tests was failing.
Why this reaches CI#
actions/setup-go caches GOCACHE and GOMODCACHE by default; the README says caching "is enabled by default", and the action's cache paths come from go env GOCACHE and go env GOMODCACHE. Go keeps test results in that build cache; go clean -testcache "removes all cached test results (but not cached build results)". Build and test caching
So a CI job on a new commit can print (cached) for a package whose code did not change, using a result recorded by an earlier run. For pure code that is a correct reuse. For an integration test, the cached result describes the service and data as they were when that earlier run happened.
Decide which tests may be cached#
| Test reads | Cached result is | Run with |
|---|---|---|
| Only code and files in the module | Valid for this binary | go test ./... |
| Environment variables | Valid if the values match | go test ./... |
| A database, service, cloud API or file outside the module | Stale evidence | -count=1 |
| The clock or random values | A replay of one earlier outcome | -count=1, or fix the test |
-count=1 is the simplest switch for an integration job. Before it, run GODEBUG=gocachetest=1 go test ./..., which the documentation says prints details of each decision to reuse a cached result. When you hand a result on, quote the ok line with its elapsed time. If the line says (cached), write in the handoff that this job did not run those tests.
For counting what actually ran, see a test command that ran no tests. Turborepo has its own form of replay: a cache hit with an undeclared variable.