英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论