【问题标题】:How do I run specific golang test using go test -run如何使用 go test -run 运行特定的 golang 测试
【发布时间】:2018-01-27 00:25:10
【问题描述】:

我在这里做错了什么?我只想运行这个 ConnectionPoolTest.TestNew 并且无论我尝试什么,我都会返回“没有要运行的测试”

:go test  --check.list *.go |grep Connectio
ConnectionPoolTest.TestNew
:go test  --run ConnectionPoolTest *.go
ok      command-line-arguments  0.005s [no tests to run]
:go test  --run ConnectionPoolTest.TestNew *.go
ok      command-line-arguments  0.005s [no tests to run]

【问题讨论】:

  • go test 需要包,而不是 go 文件,要过滤要运行的测试,请使用 -run X,其中 X 是一个正则表达式,它将与测试名称匹配。运行go help test了解更多详情;和Go tool: Test packages;和Go tool: Description of testing flags
  • 我不明白。是否有一种简单的方法可以自行运行该测试。我的正则表达式应该与 -check.list 的测试输出相匹配
  • 要单独运行该测试,请转到其文件夹,然后键入:go test -run TestNew。如果您有其他名称包含 TestNew 子字符串的测试,请运行 go test -run ^TestNew$
  • 这就是我认为它会工作的方式,但它不是 go test --run ^TestNew$ testing: warning: no tests to run PASS go test -check.vv --run TestNew testing: warning:没有测试可以运行 PASS
  • 只需一个破折号:-run,并确保您在包的文件夹中,否则您还必须传递包名称。

标签: go


【解决方案1】:

如果你想运行一个特定的测试,你可以运行如下

package main

import (
    "testing"
    "github.com/stretchr/testify/assert"
)

func TestA(t *testing.T) {
    assert.True(t, true)
}

func TestB(t *testing.T) {
    assert.True(t, false)
}

运行测试:

$ go test -run B
--- FAIL: TestB (0.00s)
        Error Trace:    a_test.go:13
        Error:          Should be true
FAIL
exit status 1
FAIL    test    0.004s
$ go test -run A
PASS
ok      test    0.003s
shahriar@Kite ~/g/s/test> 

-run标志

-run regexp
        Run only those tests and examples matching the regular expression.
        For tests the regular expression is split into smaller ones by
        top-level '/', where each must match the corresponding part of a
        test's identifier.

【讨论】:

  • 代码库使用“gopkg.in/check.v1”并运行特定测试需要使用 -check.f REGEX 其中 REGEX 是 suite.test 名称的正则表达式
  • 如果您有更合适的答案,您可以回答自己的问题。 @AC。然后它将帮助每个人
猜你喜欢
  • 2016-10-11
  • 2017-09-10
  • 2019-05-03
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
相关资源
最近更新 更多