【发布时间】:2020-11-06 20:01:01
【问题描述】:
我有一个用于实现十几个测试的 Go 包的测试套件。有时,套件中的一项测试失败,我想单独重新运行该测试以节省调试过程的时间。这是可能的还是我每次都必须为此编写一个单独的文件?
【问题讨论】:
-
go run -run regexp只会运行名称与正则表达式匹配的测试。 docs -
谢谢。但应该是
go test -run
标签: go
我有一个用于实现十几个测试的 Go 包的测试套件。有时,套件中的一项测试失败,我想单独重新运行该测试以节省调试过程的时间。这是可能的还是我每次都必须为此编写一个单独的文件?
【问题讨论】:
go run -run regexp 只会运行名称与正则表达式匹配的测试。 docs
go test -run
标签: go
使用go test -run 标志运行特定测试。该标志记录在
go tool 文档的 testing flags section:
-run regexp
Run only those tests and examples matching the regular
expression.
【讨论】:
regexp,如果您想运行精确测试(并且没有其他可能共享该名称作为前缀的测试) - 使用正则表达式的第一个和最后一个锚点,例如go test '-run=^TestMyTest$'
go test -r "TestFoo/^bar$/baz 之类的内容是有效的。
如果有人在 Go 中使用 Ginkgo BDD 框架也会遇到同样的问题,这可以在该框架中通过将测试规范标记为重点 (see docs)、在 It 之前添加 F、@ 来实现987654324@ 或 Describefunctions。
所以,如果你有这样的规范:
It("should be idempotent", func() {
你把它改写成:
FIt("should be idempotent", func() {
它将完全按照一个规范运行:
[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped
【讨论】:
给定一个测试:
func Test_myTest() {
//...
}
只运行那个测试:
go test -run Test_myTest path/to/pkg/mypackage
【讨论】:
假设您的测试套件的结构如下:
type MyTestSuite struct {
suite.Suite
}
func TestMyTestSuite(t *testing.T) {
suite.Run(t, new(MyTestSuite))
}
func (s *MyTestSuite) TestMethodA() {
}
要在 go 中运行测试套件的特定测试,您需要使用:-testify.m。
go test -v <package> -run ^TestMyTestSuite$ -testify.m TestMethodA
更简单地说,如果方法名对一个包来说是唯一的,你总是可以运行这个
go test -v <package> -testify.m TestMethodA
【讨论】:
【讨论】:
go test -v <package> -run <TestFunction>
【讨论】: