Simon Frey
Simon Frey

Reputation: 2939

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.

Upvotes: 275

Views: 244220

Answers (5)

julian soro
julian soro

Reputation: 5228

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.

Upvotes: 19

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

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 

Upvotes: 124

distortedsignal
distortedsignal

Reputation: 369

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"

Upvotes: 9

soltysh
soltysh

Reputation: 1504

There's also GOCACHE=off mentioned here.

Upvotes: 21

Marc
Marc

Reputation: 21035

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.

Upvotes: 425

Related Questions