Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go test: only run tests that contain a build tag?

Tags:

testing

go

I have a set of long running tests, defined with the build tag. For example,

// file some_test.go
//+build func_test

(rest of file with test cases)

And I have many other shorter running tests, without this build flag. Is there a way I can easily run only the tests containing the build tag "func_test"?

Note that if I just run go test -tags func_test, it runs ALL tests including the ones in some_test.go.

like image 300
Sush Avatar asked Jan 13 '19 03:01

Sush


People also ask

Does go test run go build?

It will have two functions: a constructor NewPerson and a function older , which will take a *Person and return if one *Person is more senior than another *Person . This way, the go test locates the package, builds it, and runs its tests.

How do I run a specific Golang test?

Go provides the go test command for running the unit tests in a project. Running this command will ordinarily run the whole test suite, but you can limit it to only a specific file or test. If you run go test , it will run both the TestFactorial and TestSquare functions.

Does go test run tests in parallel?

The 'go test' command may run tests for different packages in parallel as well, according to the setting of the -p flag (see 'go help build').

Does go test run go vet?

As part of building a test binary, go test runs go vet on the package and its test source files to identify significant problems. If go vet finds any problems, go test reports those and does not run the test binary.


2 Answers

As per the golang Doc https://golang.org/pkg/go/build/

The build tag lists the conditions under which a file should be included in the package. So if you want to run the test only for the build tags func_test then you need to provide a different tag for the other tests.

Here is an example: I have following 2 test files in my test directory.

func_test.go

//+build test_all func_test

package go_build_test

import (
    "fmt"
    "testing"
)

func TestNormal(t *testing.T) {
    fmt.Println("testing:", t.Name())

}

other_test.go

//+build test_all,!func_test

package go_build_test

import "testing"
import "fmt"

func TestOtherCase(t *testing.T) {
    fmt.Println("testing:", t.Name())
}

Now If you want to run all the tests.

$ go test -tags=test_all
testing: TestNormal
testing: TestOtherCase
PASS
ok      _/D_/Project/ARC/source/prototype/go/src/go-build-test  0.186s

Only running the func_test

$ go test -tags=func_test
testing: TestNormal
PASS
ok      _/D_/Project/ARC/source/prototype/go/src/go-build-test  1.395s

The trick is to work with the //+build comment with AND/OR conditions.

like image 67
Deepak Kumar Avatar answered Nov 10 '22 00:11

Deepak Kumar


You could name your test by convention. I.e let your long running test function start with TestLong_ Then run them with go test ./... -run TestLong -tags func_test

like image 38
Kjetil Lilleby Avatar answered Nov 09 '22 23:11

Kjetil Lilleby