通过go test跳过一些测试
是否可以跳过/排除某些测试的运行go test
?
我有大量的集成类型测试,这些调用称为标准服务测试编写的rest服务,并使用运行go
test。开发新功能时,有时可以跳过某些测试,例如,如果该新功能尚未部署在测试服务器上,并且我仍想运行所有现有测试(那些进行测试的新测试除外),则有时会有用新功能)。
我知道-run
,但是我不想指定我要运行的所有测试,那将是一长串。同时,我无法编写排除测试的正则表达式。
另一个选择是不提交不在同一分支中运行的测试,但是如果我仅指定要排除的内容,则会更容易。
回答:
就像VonC所说的,您可以使用+build
标签
┌─ oneofone@Oa [/t/tst-tag] └──➜ ls
a_test.go b_test.go c_test.go
package tagsimport "testing"
func TestA(t *testing.T) {}
// +build !feature1package tags
import "testing"
func TestB(t *testing.T) {}
// +build !feature1// +build !feature2
package tags
import "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)
//更新:不同的逻辑:
// +build allpackage tags
import "testing"
func TestA(t *testing.T) {}
// +build all feature1package tags
import "testing"
func TestB(t *testing.T) {}
// +build all feature2package 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)
或者您通过名称调用特定的测试,例如:
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)
以上是 通过go test跳过一些测试 的全部内容, 来源链接: utcz.com/qa/413655.html