ChatGPT解决这个技术问题 Extra ChatGPT

Force retesting or disable test caching

Problem:

When I run the same go test twice the second run is not done at all. The results are the cached ones from the first run.

PASS    
ok      tester/apitests    (cached)

Links

I already checked https://golang.org/cmd/go/#hdr-Testing_flags but there is no cli flag for that purpose.

Question:

Is there a possibility to force go test to always run test and not to cache test results.


M
Marc

There are a few options as described in the testing flags docs:

go clean -testcache: expires all test results

use non-cacheable flags on your test run. The idiomatic way is to use -count=1

That said, changes in your code or test code will invalidate the cached test results (there's extended logic when using local files or environment variables as well), so you should not need to invalidate the test cache manually.


It's still useful in case you're testing against a moving dependency e.g. a database where you setup / teardown the dependency but not your code. Looks like OP was testing against a non stubbed API which is what prompted their question.
is it possible to clean the cache for specific tests only?
Note that go clean -testcache ./... works too (at the top of a monorepo)
I've opened an issue to make this better as well: github.com/golang/go/issues/39056?ts=4
great answer, I was needing this because i ran ttwice the test suite with a different set of env vars. This is required to trigger the test of different implementation that must pass the same series of tests. This helped to prevent code duplication.
M
Muhammad Soliman

In Go11, I couldn't disable cache using GOCACHE with modules, I used -count=1 instead:

go test -count=1

Prior to Go11:

GOCACHE=off go test

Or, clean test cache and run test again:

go clean -testcache && go test 

s
soltysh

There's also GOCACHE=off mentioned here.


For go 1.11 and having go modules feature on using GOCACHE=off gives an error go: cannot use modules with build cache disabled. The better is to use suggested -count 1.
You're right, according to github.com/golang/go/issues/26809#issuecomment-410477084 GOCACHE will be slowly phased out in go 1.12 so using go test -count=1 ... is safer choice now.
build cache is disabled by GOCACHE=off, but required as of Go 1.12
d
distortedsignal

The way that I fixed this (I'm using Visual Studio Code on macOS):

Code > Preferences > Settings

Click ... on the right hand side of the settings page

Click Open settings.json

Either:

Add the following snippet to your settings.json file "go.testEnvVars": { "GOCACHE": "off" } Change the value of go.testEnvVars to include the following: "GOCACHE": "off"


Build cache is required as of Go 1.12 thus setting GOCACHE will not work with recent versions of Go. A solution for VS Code is to set "go.testFlags": ["-count=1"] in the settings.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now