Search code examples
gogo-testing

Why does -count=1 ignores caching in Go tests?


I understand that in order to avoid cached results in Go tests you can use the -count=1 flag in the go test command, but why?

This is from the docs:

The idiomatic way to disable test caching explicitly is to use -count=1

The explanation for the count flag is:

-count n
    Run each test, benchmark, and fuzz seed n times (default 1).
    If -cpu is set, run n times for each GOMAXPROCS value.
    Examples are always run once. -count does not apply to
    fuzz tests matched by -fuzz.

It doesn't say anything about caching and the default value is 1, but skipping cached tests aren't ignored by default.


Solution

  • The simple answer is because this is how the go tool is written.

    The reasoning is: Test outputs are cached to speed up tests. If the code doesn't change, the test output shouldn't change either. Of course this is not necessarily true, tests may read info from external sources or may use time and random related data which could change from run to run.

    When you request multiple test runs using the -count flag, obviously the intention is to run the tests multiple times, there's no logic running them just once and show n-1 times the same result. So -count triggers omitting the cached results. -count=1 will simply cause running the tests once, omitting previous cached outputs.