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
.
You need the =
sign after -tags
go test -tags=func_test
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.