英文:
How to detect code-coverage of separated folders in GO?
问题
我的项目结构如下:
stuff/stuff.go -> 包: stuff
test/stuff/stuff_test.go -> 包: test
尽管stuff_test执行了stuff.go中的代码,但显示的覆盖率为0.0%。
我使用了go test -cover
命令。
如果我将*_test.go文件移动到程序的stuff文件夹中,它就能正常工作。
或者,也许我的项目结构方法不够好或不符合Go的规范?
英文:
My project-structure<br>
stuff/stuff.go -> package: stuff
<br>
test/stuff/stuff_test.go -> package: test
<br>
Although stuff_test executes code from stuff.go it shows<br>
coverage: 0.0% of statements
<br>
<br>
I used
go test -cover
<br>
If I move my *_test.go to the stuff-folder of the program it is working fine.<br>
Or perhaps my approach of the project-structure is not well designed/go-conform?
答案1
得分: 5
传统的Go程序结构将测试与包一起保存。像这样:
项目
|-stuff
|--stuff.go
|--stuff_test.go
在你的测试文件的顶部,你仍然声明package stuff
,如果你希望go test
自动运行它们,那么你的测试方法必须采用TestMethodX
的形式。
详细信息请参阅Go文档:https://golang.org/pkg/testing/
英文:
Conventional Go program structure keeps the tests with the package. Like this:
project
|-stuff
|--stuff.go
|--stuff_test.go
At the top of your testing files you still declare package stuff
, and it is required that your test methods take the form TestMethodX
if you want go test
to automatically run them.
See Go docs for details: https://golang.org/pkg/testing/
答案2
得分: 5
你可以使用-coverpkg
选项来选择要记录覆盖率信息的包。
从go help testflag
的输出中可以看到:
-coverpkg pattern1,pattern2,pattern3
对与模式匹配的包在每个测试中应用覆盖率分析。
默认情况下,每个测试只分析被测试的包。
有关包模式的描述,请参阅'go help packages'。
设置-cover。
例如:
go test ./test/... -coverprofile=cover.out -coverpkg ./...
然后使用以下命令查看报告:
go tool cover -html=cover.out
英文:
You can use the -coverpkg
option to select the packages for which to record coverage information.
From the output of go help testflag
:
> -coverpkg pattern1,pattern2,pattern3
> Apply coverage analysis in each test to packages matching the patterns.
The default is for each test to analyze only the package being tested.
See 'go help packages' for a description of package patterns.
Sets -cover.
For example:
go test ./test/... -coverprofile=cover.out -coverpkg ./...
Then view the report with:
go tool cover -html=cover.out
答案3
得分: 4
跨包测试覆盖率不被直接支持,但是有几个人构建了包装器来合并各个测试覆盖率文件。
请参考Issue #6909了解详细历史。还可以参考gotestcover作为一个合并工具的示例。还有gocovmerge。我构建了自己的版本,所以我没有尝试过这些工具,但我相信它们都像我的一样有效。
我觉得这只是一个没有人为之编写一个真正引人注目的变更列表的问题,并且对于核心维护者来说并不是那么重要,所以它没有得到解决。它确实引发了一些可能会破坏现有测试的小问题,所以对于大多数人有效的快速解决方案并没有被原样接受。但我没有看到任何讨论表明核心维护者对这个功能有积极的反对意见。
英文:
Cross-package test coverage is not directly supported, but several people have built wrappers to merge individual coverage profiles.
See Issue #6909 for the long history on this. And see gotestcover for an example tool to do the merging. There's also gocovmerge. I built my own version, so I haven't tried any of these, but I'm sure they all work like mine, and mine works fine.
My sense is that this is just an issue that no one has written a really compelling changelist for, and hasn't been that important to the core maintainers, so it hasn't been addressed. It does raise little corner cases that might break existing tests, so the quick hacks that work for most of us haven't been accepted as-is. But I haven't seen any discussion suggesting the core maintainers actively object to this feature.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论