【问题标题】:Just run single test instead of the whole suite? [duplicate]只运行单个测试而不是整个套件? [复制]
【发布时间】:2020-11-06 20:01:01
【问题描述】:

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

【问题讨论】:

  • go run -run regexp 只会运行名称与正则表达式匹配的测试。 docs
  • 谢谢。但应该是go test -run

标签: go


【解决方案1】:

使用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 之类的内容是有效的。
【解决方案2】:

如果有人在 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

【讨论】:

  • 我正在尝试这样做,但在 'var _ = g.Describe(...) 内部,但它不起作用,并且仍在运行其他测试。你知道为什么吗?我对 Go 有点陌生。
  • @JoshuaOliphant 对我来说,当有多个包含测试的包时会发生这种情况,并且它会在所有包中运行测试。然后,即使在一个包中,测试也很集中——它运行其他包。但要弄清楚你的问题,我需要更多细节。
  • 就对了!超级好记,好一棵银杏
【解决方案3】:

给定一个测试:

func Test_myTest() {
    //...
}

只运行那个测试:

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

【讨论】:

  • 这很有帮助,我不知道应该用 RegExp 表达什么,但这个示例让我在 1s 内理解了
【解决方案4】:

假设您的测试套件的结构如下:

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

【讨论】:

    【解决方案5】:

    简单可靠:

    go test -run TestMyFunction ./...
    

    更多关于./...https://stackoverflow.com/a/28031651/5726621

    【讨论】:

    • 这正是我想要的。谢谢 - 简单明了
    【解决方案6】:
    go test -v <package> -run <TestFunction>
    

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2020-12-06
      • 1970-01-01
      • 2017-04-20
      • 2016-11-24
      相关资源
      最近更新 更多