就像VonC所说的,您可以使用
+build标签
┌─ oneofone@Oa [/t/tst-tag] └──➜ lsa_test.go b_test.go c_test.go
a_test.go:
package tagsimport "testing"func TestA(t *testing.T) {}b_test.go:
// +build !feature1package tagsimport "testing"func TestB(t *testing.T) {}c_test.go:
// +build !feature1// +build !feature2package tagsimport "testing"func TestC(t *testing.T) {}然后使用
-tags参数运行测试:
┌─ 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)
//更新:不同的逻辑:
a_test.go:
// +build allpackage tagsimport "testing"func TestA(t *testing.T) {}b_test.go:
// +build all feature1package tagsimport "testing"func TestB(t *testing.T) {}c_test.go:
// +build all feature2package tagsimport "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)或者您通过名称调用特定的测试,例如:
d_test.go:
package tagsimport "testing"func TestA1(t *testing.T) {}func TestB1(t *testing.T) {}func TestC1(t *testing.T) {}func TestD1(t *testing.T) {}输出:
┌─ 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)



