How to force go test to rerun tests without caching?

When I run go test twice, the second run does not execute the tests again. Instead, it uses the cached results from the first run.

PASS ok tester/apitests (cached)

I have reviewed the documentation at go command - cmd/go - Go Packages, but there doesn’t appear to be a CLI flag specifically for this purpose.

Is there a way to force go test to always run the tests and not use cached results? I need to ensure that go test executes the tests each time and does not rely on cached results.

To make sure your tests always run fresh and skip the cache, I like to use the -count flag. It’s super simple but effective. Just set the count to 1, and it guarantees your tests will run every time:

go test -count=1

This little trick has saved me more than once when I needed fresh test results, and it’s a handy way to bypass the caching mechanism entirely.

Give it a try—it’s straightforward and does the job!

One trick I like to use to ensure my tests always run fresh is the -test.cache flag. If you want to disable test caching entirely, just set this flag to false, and you’re good to go. This guarantees that every time you run your tests, they’ll execute from scratch:

go test -test.cache=false

It’s a super handy option when you’re debugging or making frequent changes and need to be sure the test results aren’t cached. It’s saved me from some frustrating moments when I thought I was getting fresh results, but wasn’t!

Clear the Cache Manually

If you ever need to force Go to re-run all tests, one trick I use is clearing the test cache manually. You can do this by running:

go clean - testcache

Then, just follow it up with:

go test

This approach guarantees that no cached results are used, so you’ll get fresh test executions on the next run. It’s a nice way to ensure you’re starting from scratch!