First golang test run is slow

Viewed 804

I'm running a very simple test adding two numbers.

package internal

import "testing"

func TestAddingNumbers(t *testing.T) {
    if add(1, 5) != 6 {
        t.Errorf("Failed Adding numbers")
    }
}

First go test -v file.go file_test.go runs in => ok command-line-arguments 0.434s

While second in go test -v file.go file_test.go runs in => ok command-line-arguments 0.099s

Is there a way to make first test faster? My understanding is that there is some caching happening so the second is faster. But in the context of CI step, cache won't be there and it will makes things slow.

1 Answers

You cannot make the first one run faster, because that's the only time the test actually runs. The second run is simply using the test cache, without running the test.

However, you can make the test runs use similar times by disabling the test cache. The idiomatic way to do that is to use the flag:

go test -count=1
Related