只运行单一测试而不是整个套件?

时间:2014-09-29 03:35:04

标签: go

我有一个Go包的测试套件,它实现了十几个测试。有时,套件中的一个测试失败,我想单独重新运行该测试以节省调试过程的时间。这是可能的,还是我每次都必须为此写一个单独的文件?

5 个答案:

答案 0 :(得分:31)

使用go test -run标志运行特定测试。该标志记录在 testing flags section文档的go tool

-run regexp
    Run only those tests and examples matching the regular
    expression.

答案 1 :(得分:6)

如果有人使用Go的Ginkgo BDD框架会遇到同样的问题,可以在该框架中通过将测试规范标记为聚焦(see docs),在它之前预先设置F,上下文或描述来实现功能。

所以,如果您的规格如下:

    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

答案 2 :(得分:5)

说您的测试套件的结构如下:

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

答案 3 :(得分:3)

给出测试:

func Test_myTest() {
    //...
}

仅运行以下测试:

go test -run Test_myTest path/to/pkg/mypackage

答案 4 :(得分:0)

简单可靠:

go test -run TestMyFunction ./...

有关./...的更多信息: https://stackoverflow.com/a/28031651/5726621

相关问题