英文:
Skip some tests with go test
问题
在使用go test
命令运行测试时,是否可以跳过/排除某些测试?
我有一些相当多的集成类型测试,它们调用一个作为标准go测试编写的REST服务,并使用go test
运行。当开发新功能时,有时候可以跳过一些测试是很有用的,例如,如果新功能尚未部署在测试服务器上,但我仍然希望运行所有现有的测试(除了那些测试新功能的测试)。
我知道可以使用-run
选项来指定要运行的测试,但我不想指定我想要运行的所有测试,那将是一个很长的列表。同时,我也无法编写一个正则表达式来排除测试。
另一个选项是在同一分支中不提交不运行的测试,但如果我可以直接指定要排除的内容会更容易。
英文:
Is it possible to skip/exclude some tests from being run with go test
?
I have a fairly large amount of integration type tests which call a rest service written as standard go tests, and run with go test
. When a new feature is developed its sometimes useful to be able to skip some of the tests, for example if the new feature is not yet deployed on the testing server and I still want to run all the existing tests (except those new ones which tests the new feature).
I know about -run
, but I dont want to specify all tests I want to run, that would be a long list. At the same time I have not been able to write a regex for excluding tests.
Another option would be to not commit the tests which dont run in the same branch, but it would be easier if I could just specify what to exclude.
答案1
得分: 158
测试包中有SkipNow()
和Skip()
方法。因此,可以在单个测试之前添加类似以下的内容:
func skipCI(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("在 CI 环境中跳过测试")
}
}
func TestNewFeature(t *testing.T) {
skipCI(t)
}
然后,你可以设置环境变量或运行CI=true go test
来将CI
设置为命令本地变量。
另一种方法是使用短模式。在测试中添加以下保护:
if testing.Short() {
t.Skip("在短模式下跳过测试")
}
然后使用go test -short
来执行你的测试。
英文:
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
答案2
得分: 73
根据VonC的说法,你可以使用+build
标签。
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
参数运行测试:
go test -v . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
go test -v -tags feature1 . | grep PASS:
--- PASS: TestA (0.00 seconds)
go test -v -tags feature2 . | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
更新:不同的逻辑:
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) {}
go test -v -tags all | grep PASS:
--- PASS: TestA (0.00 seconds)
--- PASS: TestB (0.00 seconds)
--- PASS: TestC (0.00 seconds)
go test -v -tags feature1 | grep PASS:
--- PASS: TestB (0.00 seconds)
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) {}
输出:
go test -run="(A|B)1" -v | grep PASS:
--- PASS: TestA1 (0.00 seconds)
--- PASS: TestB1 (0.00 seconds)
go test -run="D1" -v | grep PASS:
--- PASS: TestD1 (0.00 seconds)
英文:
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)
答案3
得分: 5
当环境变量CI不为空时,你可以使用TestMain
来跳过整个包中的测试。
func TestMain(m *testing.M) {
if os.Getenv("CI") == "" {
m.Run()
}
}
英文:
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()
}
}
答案4
得分: 1
go 1.20测试命令现在接受-skip
https://tip.golang.org/doc/go1.20
英文:
The go 1.20 test command now accepts -skip <pattern> to skip tests, subtests, or examples matching <pattern>.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论