如何使用go test -run运行特定的golang测试

25

我在这里做错了什么?我只想运行这个 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]

4
go test 命令需要的是包而不是 Go 文件,如果想要筛选需要运行的测试,可以使用 -run X,其中 X 是一个正则表达式,会与测试名称进行匹配。运行 go help test 以获取更多详细信息;还可以参考 Go 工具: 测试包Go 工具: 测试标志说明 - icza
我不明白。有没有一种简单的方法来单独运行那个测试?我的正则表达式应该匹配由-check.list输出的测试结果。 - AC.
4
要独立运行这个测试,请前往它所在的文件夹并键入:go test -run TestNew。如果您有其他测试名称中包含“TestNew”子字符串,则运行go test -run ^TestNew$ - icza
这就是我认为它应该工作的方式,但事实并非如此。 go test --run ^TestNew$ 测试:警告:没有要运行的测试 通过go test -check.vv --run TestNew 测试:警告:没有要运行的测试 通过 - AC.
抱歉,这是别人扔给我的代码。我看到他们导入了“gopkg.in/check.v1”,我需要查看是否会对我造成问题。 - AC.
显示剩余4条评论
1个回答

28
如果您想运行特定的测试,可以按照以下方式运行
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是套件名称.测试名称的正则表达式。 - AC.
2
如果您有更合适的答案,可以回答自己的问题。@AC。这将有助于每个人。 - Shahriar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接