236

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Simon Frey
  • 2,539
  • 2
  • 11
  • 20

5 Answers5

372

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.

Marc
  • 19,394
  • 6
  • 47
  • 51
  • 12
    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. – joakim Apr 24 '18 at 16:26
  • 1
    is it possible to clean the cache for specific tests only? – nickcamillo Jan 17 '19 at 21:14
  • 7
    Note that `go clean -testcache ./...` works too (at the top of a monorepo) – voutasaurus Apr 02 '19 at 17:45
  • I've opened an issue to make this better as well: https://github.com/golang/go/issues/39056?ts=4 – wesm May 13 '20 at 19:20
  • 1
    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. –  Feb 12 '21 at 12:08
  • 1
    It can be useful when tests are flaky and results can vary from run to run: relying on tests being invalidated by code changes assumes reproducible tests, which is is only guaranteed for unit tests: for integration tests, where the source of cache invalidations my rest outside the code, it's better to use -count=1. – FGM Oct 14 '21 at 12:42
  • It's worth noting that this is useful when running benchmarks as well with 'go test -bench=...' - I have in fact a suite which was misbehaving (some tests where lagging behind for no real reason) right up until I actual run 'go clean -testcache'. Weird but there you have it. – XDS Mar 02 '22 at 10:13
  • The second point "use non-cacheable flags on your test run" is actually quite good. The [Golang docs for test package](https://pkg.go.dev/cmd/go/internal/test#:~:text=The%20idiomatic%20way%20to%20disable%0Atest%20caching%20explicitly%20is%20to%20use%20%2Dcount%3D1.) states "The idiomatic way to disable test caching explicitly is to use `-count=1`". This is because setting `-count` to anything above zero has the side effect of disabling the test cache. Therefore you can even set `-count=5` to both disable the test cache and to run each test five times. – Tom Anderson Aug 30 '23 at 02:19
103

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 
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
21

There's also GOCACHE=off mentioned here.

soltysh
  • 1,464
  • 12
  • 23
  • 8
    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`. – zdebra Jan 06 '19 at 17:03
  • 1
    You're right, according to https://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. – soltysh Jan 07 '19 at 12:24
  • 2
    `build cache is disabled by GOCACHE=off, but required as of Go 1.12` – Francesco Casula Apr 29 '20 at 08:00
10

For VS Code (in 2022)

  1. Open VSCode's settings.json. To open settings.json, press Ctrl + , (or Cmd+, on Mac), then click the Open JSON button shown below. Optionally, if you don't want to set this globally, you can create a .vscode/settings.json file at the project root.

    Button for settings.json file

  2. Set the go.testFlags value in settings.json:

     {    
         "go.testFlags": ["-count=1"]
     }
    
  3. Save and enjoy.

Note: these steps ensure test cache will be skipped every time. If you want a one-time fix, then run go clean -testcache in the terminal, as Marc's most-voted answer says.

julian soro
  • 4,868
  • 5
  • 28
  • 36
9

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:

  1. Add the following snippet to your settings.json file

    "go.testEnvVars": {
        "GOCACHE": "off"
    }
    
  2. Change the value of go.testEnvVars to include the following: "GOCACHE": "off"
distortedsignal
  • 370
  • 5
  • 17
  • 17
    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. – Armand Grillet Nov 07 '19 at 08:40