调用`go test`命令时,使用不同的包会导致输出的差异。

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

Difference in output when calling go test with package

问题

我有一个Go项目,我在其中使用glide进行包管理。

我使用godog设置了一个测试套件,我使用go test运行它,使用TestMain包装器,如godoc文档中所述(使用pretty格式)。

由于我不想测试我正在供应的软件包,我使用以下命令调用测试:

go test $(glide novendor)

在我的情况下,这个项目非常简单,glide novendor只返回一个.

现在我的问题是:为什么当我运行go test时,会得到很多漂亮的输出,列出场景和步骤等等,但是当我运行go test .时,我只得到一行输出:

ok      gitlab.knf.local/ngs/gpp        0.015s [no tests to run]

(即使我添加一个虚拟测试,这个输出也不会改变)。

英文:

I have a Go project in which I'm using glide for package management.

I have a test suite setup using godog, which I run with go test using a TestMain wrapper as described in the docs of godoc (with format pretty).

Since I don't want to test packages I am vendoring, I call the tests with

go test $(glide novendor)

In my case, the project is very simple, and glide novendor just returns a .

Now my question is: Why is it that when running go test, I get a lot of pretty output, listing scenarios and steps and whatnot, but when I run go test . I just get a one-liner

ok      gitlab.knf.local/ngs/gpp        0.015s [no tests to run]

(This doesn't change even if I add a dummy test).

答案1

得分: 2

输出结果不同是因为Go工具是这样编写的。这是有意的,默认行为。

如果只运行go test而不列出包名,它将测试当前目录中的包:

默认情况下,go test不需要参数。它会编译和测试当前目录中的包,包括测试,并运行这些测试。

如果你列出包名给go test(可以是单个包或者只是.),那么go test将测试所有列出的包。

如果你测试单个包,你想要看到每个测试的详细信息和结果。如果你测试多个包,通常你对每个单独测试的详细信息不感兴趣,因为那只会让屏幕杂乱,而且当信息在屏幕上滚动时,你甚至都不会注意到一个测试或一个包是否失败。当测试多个包时,通常你只对每个包的最终结果感兴趣。每个包一行是一个不错的折衷方案,可以给出清晰的结果。

如果你仍然想在列出包时看到更详细的输出,请简单地传递-v标志:

go test -v $(glide novendor)

有关更多详细信息和选项,请运行go help test,或访问Command go文档页面,特别是Test packagesDescription of testing flags部分。

英文:

The output is different because this is how the Go tool is written. This is the intentional, default behavior.

If you just run go test without listing packages, it will test the package in the current directory:

> By default, go test needs no arguments. It compiles and tests the package with source in the current directory, including tests, and runs the tests.

If you list packages to go test (may it be just a single package or just a ., doesn't matter), then go test will test all listed packages.

If you test a single package, you want to see the details and result of each tests. If you test multiple packages, normally you're not interested in the details of each individual tests, because that would just clutter your screen, and as information scrolls through your screen, you would not even notice if a test or a package fails. When testing multiple packages, normally you're just interested in the final result for each package. One line per package is a nice compromise and gives a clear result.

If you still want to see more verbose output when listing packages, simply pass the -v flag:

go test -v $(glide novendor)

For more details and options, run go help test, and/or visit the Command go documentation page, specifically sections Test packages and Description of testing flags.

huangapple
  • 本文由 发表于 2017年3月23日 18:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/42972827.html
匿名

发表评论

匿名网友

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

确定