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
.
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.
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.
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').
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With