1

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.

nono
  • 2,262
  • 3
  • 23
  • 32

1 Answers1

4

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
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • very disappointing – nono Sep 18 '21 at 20:20
  • 2
    `go test` will build and vet the program first, and then run the tests. The duration of a test run is rarely an issue. What are you trying to achieve? – Burak Serdar Sep 18 '21 at 20:28
  • 2
    @nono, the only way to make things faster is to do less work; so would you like to skip the tests or skip building and testing entirely? – JimB Sep 18 '21 at 20:37