当测试文件位于包外时,Golang的测试覆盖率。

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

Golang test coverage when test files outside package

问题

我正在尝试使用位于测试目录中的测试文件,获取处理器包中代码的适当覆盖量。我尝试了许多不同的-cover和-coverpkg组合,但无法正确运行。在将测试文件放在单独的包/文件夹中的情况下,是否可以实现这一点?

.
├── internal
│   └── processor
└── test
英文:

I am trying to get the proper coverage amount of my code in the processor package using my test files that are in the test directory. I've tried numerous combinations of -cover and -coverpkg and cannot get it to work correctly. Can this be done while having the test files in separate package/folder?

.
├── internal
│   └── processor
└── test

答案1

得分: 4

我找到了一个答案,这是我完成目标的方法:

go test -cover -coverpkg "./internal/processor" "./test"

根据 cmd/go, Testing Flags 的说明,该标志的用法是:

-coverpkg pattern1,pattern2,pattern3

应用于每个测试的覆盖率分析,匹配模式的包。**默认情况下,每个测试只分析被测试的包。**有关包模式的描述,请参阅 'go help packages'。设置-cover。

英文:

I tracked down an answer and this is how I was able to accomplish my goal:

go test -cover -coverpkg "./internal/processor" "./test"

Based on cmd/go, Testing Flags, the flag usage is:

-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.

huangapple
  • 本文由 发表于 2022年4月21日 22:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/71956571.html
匿名

发表评论

匿名网友

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

确定