Disable test result caching from within Go code?

565 views
Skip to first unread message

twp...@gmail.com

unread,
Mar 28, 2019, 9:20:53 PM3/28/19
to golang-nuts
I have a bunch of integration tests which depend on external services. I've added the build tag "integration" for each source file containing such tests so they are only run when I do:
  $ go test -tags=integration ./...

Go 1.12, by default, caches the tests results, which is exactly what I don't want because these "external services" are usually different backends running on my local machine. When I'm developing code, I want to test my code against the current backend running on my local machine, and not have "go test" return the result from running the tests against the previous backend.

I can disable caching by passing the "-count=1" argument to "go test" but this is a pretty blunt instrument: I need to remember when I'm running an integration test and when I'm not, and it applies to the whole set of tests that I'm running.

Ideally I'd like to specify within each test whether the result can be cached or not. I looked at the documentation for testing, but the word "cache" is not present,

Is there any way for Go test code to control whether its results should be cached?

Cheers,
Tom

twp...@gmail.com

unread,
Apr 11, 2019, 7:44:17 PM4/11/19
to golang-nuts
OK, I guess that Go has no solution for this.

Robert Engels

unread,
Apr 11, 2019, 9:41:17 PM4/11/19
to twp...@gmail.com, golang-nuts
Where are you getting the idea that results are cached? I can't even figure out how that would be useful in a testing framework - do you mean it auto-mocks services based on past requests? I've seen nothing like this - are you sure you just don't have the wrong HTTP header fields, so the HTTP request is getting cached by a CDN, etc.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Tom Payne

unread,
Apr 12, 2019, 3:41:47 AM4/12/19
to Robert Engels, golang-nuts
Here's an example of a test that is cached but shouldn't be:

This particular test uses github.com/ory/dockertest to run an integration test against a PostgreSQL database. It uses Docker which is an external service, and the result should not be cached.

Once the test has run one, running go test again prints:
which is giving me the idea that the results are cached. go test also terminates very quickly, whereas the test normally takes ~8s to run on my machine.

Manlio Perillo

unread,
Apr 12, 2019, 4:06:59 AM4/12/19
to golang-nuts
On Friday, March 29, 2019 at 2:20:53 AM UTC+1, twp...@gmail.com wrote:
No, and I doubt such an API will be added.

From `go help cache` and `go help test` it seems that there is no control variable to disable test caching.
There is only a control variable for debugging: `GODEBUG=gocachetest=1`.

However, since all your integration tests have an `integration` build tag and you need to run them with `-tags=integration`, I'm not sure to understand why you say that "I need to remember when I'm running an integration test and when I'm not".
You just need to specify `-count=1` every time you specify `-tags=integration`.


Regards
Manlio
Reply all
Reply to author
Forward
0 new messages