Question
When running the same go test command twice, the second run may not execute the tests again. Instead, Go can reuse the previous result and display output like this:
ok tester/apitests (cached)
I checked the Go testing flags documentation, but I could not find a direct command-line flag specifically described as “disable test cache.”
Is there a way to force go test to run tests every time instead of using cached test results?
Short Answer
By the end of this page, you will understand why go test sometimes shows (cached), when Go reuses old test results, and how to force tests to run again. You will also learn practical commands such as using -count=1, when to clean the test cache, and how this behavior fits into real Go development workflows.
Concept
Go's test runner can cache successful test results to make repeated test runs faster. If you run the same tests with the same inputs and nothing relevant has changed, Go may skip re-running the tests and reuse the earlier result.
This matters because:
- It speeds up development when you repeatedly run unchanged tests.
- It can be confusing when you expect a test to execute again, especially for debugging.
- It affects tests with external dependencies such as files, environment variables, databases, or services if the test setup is not fully reflected in Go's cache inputs.
To force tests to run again, the usual approach is:
go test -count=1 ./...
The -count=1 flag tells Go not to use cached test results for that run.
You can also clear cached test results manually with:
go clean -testcache
In short:
go testmay cache results for unchanged tests.go test -count=1forces re-execution.go clean -testcacheclears stored cached results.
For most cases, -count=1 is the simplest and most direct solution.
Mental Model
Think of go test caching like a teacher checking whether you already submitted the exact same homework.
- If nothing changed, the teacher says: "I already graded this".
- If you change the homework, the teacher grades it again.
- If you insist on a fresh grade anyway, you say: "Please re-check it now".
In this analogy:
- the homework is your test run
- the previous grade is the cached result
-count=1means grade it again even if it looks the samego clean -testcachemeans throw away the old grading records
This helps explain why (cached) appears: Go believes it already knows the answer for that exact test run.
Syntax and Examples
The most useful commands are:
go test ./...
Runs tests normally. Go may reuse cached results.
go test -count=1 ./...
Forces tests to run once and not use the cached result.
go clean -testcache
Clears the test cache.
Example
Suppose you have this test file:
package mathutil
import "testing"
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
if Add(2, 3) != 5 {
t.Fatal("expected 5")
}
}
If you run:
go test
and then run it again without changing anything, Go may print:
Step by Step Execution
Consider this command:
go test -count=1 ./...
Here is what happens step by step:
go teststarts the Go test tool../...selects packages in the current module and its subdirectories.-count=1tells Go to run each test once without using cached test results.- Go compiles the packages and test files if needed.
- Go executes the tests for each selected package.
- Fresh results are printed.
Small trace example
Command:
go test ./...
Possible first run:
ok example/project/pkg1 0.021s
ok example/project/pkg2 0.015s
Second run with no changes:
ok example/project/pkg1 (cached)
ok example/project/pkg2 (cached)
Now run:
go test -count=1 ./...
Possible output:
Real World Use Cases
There are several practical situations where forcing test re-execution is useful:
- Debugging flaky tests
- You want to see the test actually run every time.
- Working with environment-dependent tests
- A test reads environment variables, files, or external state.
- CI troubleshooting
- You want to confirm that the current pipeline run is not relying on local cached results.
- Benchmarking behavior changes
- You need fresh runs while checking timing or side effects.
- Integration tests
- Tests that touch APIs, databases, or temporary files often need real execution each time.
Example:
DATABASE_URL=postgres://localhost/testdb go test -count=1 ./integration/...
This is useful when your test behavior depends on a live database and you want a fresh run.
Real Codebase Usage
In real Go projects, developers usually do not disable caching globally. Instead, they use it selectively.
Common patterns include:
- Fast local feedback
- Use normal
go test ./...during everyday development.
- Use normal
- Force reruns for sensitive tests
- Use
go test -count=1 ./...when debugging or verifying stateful tests.
- Use
- Separate unit and integration tests
- Keep fast deterministic unit tests cache-friendly.
- Run integration tests with
-count=1.
- Scripts and Makefiles
- Teams often add commands like:
make test
make test-fresh
Example Makefile:
test:
go test ./...
test-fresh:
go test -count=1 ./...
- Guarding against hidden dependencies
- If a test depends on time, files, environment variables, or external services, developers often redesign it to be more deterministic instead of always disabling cache.
A good rule is: if you frequently need -count=1, your tests may depend on state outside normal source changes.
Common Mistakes
Beginners often run into these issues:
1. Thinking (cached) means tests are broken
It usually does not mean anything is wrong. It means Go determined the previous successful result is still valid.
2. Looking for a special --no-cache flag
In Go, the common solution is:
go test -count=1
not something like:
# Not a valid Go flag
go test --no-cache
3. Clearing all caches when only test cache matters
If you only need to remove test results, use:
go clean -testcache
Instead of clearing unrelated caches unnecessarily.
4. Depending on external state in tests
Broken example:
func TestConfigFromEnv(t *testing.T) {
if os.Getenv("APP_MODE") == "prod" {
t.Fatal("should not be prod")
}
}
Why this is risky:
Comparisons
| Approach | What it does | When to use it | Notes |
|---|---|---|---|
go test ./... | Runs tests normally, may use cache | Everyday development | Fastest for repeated runs |
go test -count=1 ./... | Forces tests to run again | Debugging, integration tests, fresh verification | Most direct way to bypass cache |
go clean -testcache | Deletes stored test cache | When you want to clear old cached results | Affects later test runs until cache fills again |
-count=1 vs go clean -testcache
-count=1: affects the current run by forcing execution.go clean -testcache: removes saved cached results globally for your environment.
Cheat Sheet
# Normal test run; may use cache
go test ./...
# Force tests to run again
go test -count=1 ./...
# Clear saved test cache
go clean -testcache
Key points
(cached)means Go reused a previous successful test result.- The usual way to force a fresh run is
-count=1. - Use
go clean -testcacheif you want to remove stored cached results. - Prefer deterministic tests over constantly disabling cache.
Quick rule
- Want a fresh run right now? Use
go test -count=1 - Want to erase old cached test results? Use
go clean -testcache
FAQ
How do I force go test to run every time?
Use:
go test -count=1 ./...
This is the standard way to bypass cached test results for that run.
Why does go test show (cached)?
It means Go determined the package's tests do not need to be re-executed and reused the previous successful result.
Is there a direct --no-cache flag in go test?
The common Go approach is not --no-cache, but -count=1.
How do I clear the Go test cache?
Use:
go clean -testcache
Should I always disable test caching?
Usually no. Caching makes repeated test runs much faster. Disable it only when you need fresh execution.
Why do I keep needing -count=1 for some tests?
That often suggests the tests depend on external state such as environment variables, files, time, databases, or services. Those tests may need better isolation.
Does clearing the test cache fix flaky tests?
Mini Project
Description
Create a small Go project with one package and one test, then practice running the test normally, forcing a fresh run, and clearing the test cache. This helps you see exactly how Go caching behaves in a real workflow.
Goal
Build a tiny Go test setup and use go test, go test -count=1, and go clean -testcache to understand when cached results are reused.
Requirements
- Create a Go package with one simple function.
- Add a unit test for that function.
- Run the test twice and observe the cached output.
- Run the test again with
-count=1. - Clear the test cache and run the test once more.
Keep learning
Related questions
Automatic Build Versioning in Go: Embed Incrementing Build Numbers
Learn how to add automatic build versioning in Go using linker flags, build metadata, CI counters, and Git-based version values.
Blank Identifier Imports in Go: What `_` Means in an Import Statement
Learn what `_` means in a Go import, why blank identifier imports run package init code, and when to use them safely.
Calling Functions Across Files in the Same Go Package
Learn how Go uses packages across multiple files, why functions may appear undefined, and how to organize code correctly.