是否可以跳过/排除某些测试的运行go test?
go test
我有大量的集成类型测试,这些调用称为标准服务测试编写的rest服务,并使用运行go test。开发新功能时,有时可以跳过某些测试,例如,如果该新功能尚未部署在测试服务器上,并且我仍想运行所有现有测试(那些进行测试的新测试除外),则有时会有用新功能)。
我知道-run,但是我不想指定我要运行的所有测试,那将是一长串。同时,我无法编写排除测试的正则表达式。
-run
另一个选择是不提交不在同一分支中运行的测试,但是如果我仅指定要排除的内容,则会更容易。
就像VonC所说的,您可以使用+build标签
+build
┌─ 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) {}
然后使用-tags参数运行测试:
-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)
//更新:不同的逻辑:
// +build all package tags import "testing" func TestA(t *testing.T) {}
// +build all feature1 package tags import "testing" func TestB(t *testing.T) {}
// +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)
或者您通过名称调用特定的测试,例如:
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) {}
输出:
┌─ 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)