可以将Golang单元测试结果打印到文件中吗?

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

Is it possible to print golang unit test results out to a file?

问题

我在我的pkg目录中运行go test,并且在运行过程中将测试结果打印到控制台,但如果能将它们打印到txt文件或者html文件中就更理想了,这种做法可行吗?我知道你可以从中获取覆盖率报告,并为其生成html文件,这非常好,但我认为应该也可以做同样的事情,只是针对测试的实际结果,即哪些测试运行了,哪些通过了,哪些失败了。我在网上搜索了一下,但即使是go test help也没有提供将结果打印到文件的建议。

英文:

I run go test in my pkg directory and I get the test results printed to the console as they run, but it would be ideal if I could get them printed to a txt file or even a html file, is it possible to do this? I know you can get coverage reports out from it and generate html files for those which is excellent, but I would have thought it possible to do the same just for the actual results of the tests i.e which tests ran, which passed and which failed. I've been searching the net but even go test help doesn't offer any suggestions for printing results out to a file.

答案1

得分: 4

由于我只想看到失败的测试结果,所以我运行了一个名为"gt"的脚本来代替"go test"命令:

go test -coverprofile=coverage.out %*|grep -v -e "^\.\.*$"|grep -v "^$"|grep -v "thus far"

这样,它会过滤掉除了失败的测试用例之外的所有内容。
你可以将其内容重定向到一个文件中,如下所示:gt > test.out

这个脚本还会生成代码覆盖率报告,这就是我有另一个名为"gc"的脚本的原因:

grep -v -e " 1$" coverage.out

这样,我甚至不需要等待浏览器打开,我直接就能看到尚未覆盖的行的列表(即在coverage.out文件中不以" 1"结尾的行)。

英文:

Since I only want to see failed test, I have this script "gt" that I run instead of go test:

go test -coverprofile=coverage.out %*|grep -v -e "^\.\.*$"|grep -v "^$"|grep -v "thus far"

That way, it filters everything but the failed cases.
And you can redirect its content to a file, as mentioned: gt > test.out

It also generates code coverage, which is why I have another script "gc":

grep -v -e " 1$" coverage.out

That way, I don't even wait for a brower to open, I directly see the list of lines which are not yet covered (ie, which don't end with ' 1' in the coverage.out file)

答案2

得分: 4

这将把测试结果追加到test.out文件中。

go test >> test.out

这将覆盖每次测试运行的测试结果。

go test > test.out
英文:

This will append test results to the test.out file.

go test >> test.out

This will overwrite the test results for each test run.

go test > test.out

huangapple
  • 本文由 发表于 2015年3月20日 01:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/29150756.html
匿名

发表评论

匿名网友

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

确定