Golang测试在详细模式下运行时给出的结果不一致。

huangapple go评论70阅读模式
英文:

Golang test gives inconsistent results when ran in verbose mode

问题

我在我的_test.go文件中有一个单独的测试函数,其中包含一堆子测试。它看起来像这样:

func MyTest(t *testing.T) { 
    t.Run("Subtest1", func(t *testing.T) {
       ...
    })
    t.Run("Subtest2", func(t *testing.T) {
       ...
    })
}

我使用go test运行测试,并得到以下结果:

PASS
ok  	package_path	9.137s

然而,我希望在结果中列出所有的子测试。查看$GOROOT/src/testing/testing.go中的Run函数,看起来我需要测试是chatty的。

所以我尝试通过go test -v运行测试,但我仍然没有得到期望的输出。相反,我的测试现在失败了:

=== RUN   MyTest
api.test: error: expected argument for flag '-t', try --help
exit status 1
FAIL	package_path	0.004s

--help没有显示关于-t的任何内容。

英文:

I have a single test function in my _test.go file with a bunch of sub tests.
It looks like this:

func MyTest(t *testing.T) { 
    t.Run("Subtest1", func(t *testing.T) {
       ...
    })
    t.Run("Subtest2", func(t *testing.T) {
       ...
    })
}

I run the test with go test and get

PASS
ok  	package_path	9.137s

However, I would like to see listed all my subtests in the result. Looking at the Run function in $GOROOT/src/testing/testing.go it looks like I need the test to be chatty.

So I tried to run the test via go test -v but I still do not get the desired output. Instead my test is now failing:

=== RUN   MyTest
api.test: error: expected argument for flag '-t', try --help
exit status 1
FAIL	package_path	0.004s

--help does not show anything about -t

答案1

得分: 1

这是一个关于我正在测试的代码的问题,它期望使用自己的参数,并包含以下代码行:

kingpin.MustParse(cli.Parse(os.Args[1:]))

我知道在测试中禁止解析参数。

英文:

This turned out to be a problem with the code I was testing which expects its own arguments and contained this line:

kingpin.MustParse(cli.Parse(os.Args[1:]))

I know disallow parsing of arguments in the test.

huangapple
  • 本文由 发表于 2016年9月28日 05:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/39734832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定