跳过一些测试去测试

是否可以跳过/排除使用 go test运行的某些测试?

我有大量的集成类型测试,这些测试调用作为标准 Go 测试编写的 rest 服务,并使用 go test运行。当一个新特性被开发出来的时候,它有时候可以跳过一些测试,例如,如果这个新特性还没有被部署到测试服务器上,我仍然想要运行所有现有的测试(除了那些测试新特性的新特性)。

我知道 -run,但是我不想指定所有要运行的测试,那将是一个很长的列表。同时,我还不能编写一个排除测试的正则表达式。

另一个选择是不提交不在同一个分支中运行的测试,但是如果我能够指定排除哪些内容将会更容易。

69453 次浏览

Like VonC said, you can use +build tags

┌─ oneofone@Oa [/t/tst-tag]
└──➜ ls
a_test.go  b_test.go  c_test.go

a_test.go :

package tags


import "testing"


func TestA(t *testing.T) {}

b_test.go :

// +build !feature1


package tags


import "testing"


func TestB(t *testing.T) {}

c_test.go :

// +build !feature1
// +build !feature2


package tags


import "testing"


func TestC(t *testing.T) {}

Then run the test with the -tags parameter :

┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature1 . | grep PASS:
--- PASS: TestA (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature2 . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)

// Update : different logic :

a_test.go:

// +build all


package tags


import "testing"


func TestA(t *testing.T) {}

b_test.go:

// +build all feature1


package tags


import "testing"


func TestB(t *testing.T) {}

c_test.go:

// +build all feature2


package tags


import "testing"


func TestC(t *testing.T) {}




┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags all | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags feature1 | grep PASS:
--- PASS: TestB (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -v -tags="feature1 feature2" | grep PASS:
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)

Or you call specific tests by name like :

d_test.go:

package tags


import "testing"


func TestA1(t *testing.T) {}
func TestB1(t *testing.T) {}
func TestC1(t *testing.T) {}
func TestD1(t *testing.T) {}

Output:

┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -run="(A|B)1" -v | grep PASS:
--- PASS: TestA1 (0.00 seconds)
--- PASS: TestB1 (0.00 seconds)
┌─ oneofone@Oa [/t/tst-tag]
└──➜ go test -run="D1" -v | grep PASS:
--- PASS: TestD1 (0.00 seconds)

Testing package has SkipNow() and Skip() methods. So, an individual test could be prepended with something like this:

func skipCI(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
}


func TestNewFeature(t *testing.T) {
skipCI(t)
}

You could then set the environment variable or run CI=true go test to set CI as a command-local variable.

Another approach would be to use short mode. Add the following guard to a test

if testing.Short() {
t.Skip("skipping testing in short mode")
}

and then execute your tests with go test -short

Supplement to the Vadym Tyemirov's answer.

You can use TestMain skip test in whole pkg when env CI not empty.

func TestMain(m *testing.M) {
if os.Getenv("CI") == "" {
m.Run()
}
}