brandur.org

An unfortunate discovery in Go today: when using go test and running a testable example, the -count flag is silently ignored – a confusing, underdocumented sharp edge for users. That is unless you specified the magic number -count=1, in which case you get the normal test cache busting behavior.

You can find a caveat in help once you suspect the existence of this problem, but the only way to notice it’s happening is that your tests are running suspiciously fast at high -count iterations. No error or warning is produced.

$ go help testflag
    ...

        -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.

This makes it hard to reproduce intermittent problems in an example test. As a workaround, I’m using a shell loop that runs the test until it returns a non-zero exit code:

while go test . -run Example_customInsertOpts -test.v -count=1; do :; done