英文:
How to exclude generated code from coverage statistics
问题
我在项目中有生成的 thrift 代码。我该如何阻止它影响我的覆盖率统计?它们很糟糕。
英文:
I have thrift generated code in my project? How do I stop this from affecting my coverage stats? They're dismal.
答案1
得分: 2
这是go test
的帮助信息,它似乎建议你可以过滤你正在测试的包:
-coverpkg pkg1,pkg2,pkg3
对给定的包列表应用覆盖率分析。
默认情况下,每个测试只分析正在测试的包。
包是通过导入路径指定的。
设置-cover。
另一个更简单的选项是将生成的代码作为一个库包导入,该库包位于代码树之外,因此覆盖工具在统计数据中忽略它。
例如,如果你的应用程序是github.com/Fuser97381/myproj
,将生成的代码放在github.com/Fuser97381/protocols
中。然后你的代码看起来像这样:
package main
import (
"github.com/Fuser97381/protocols/myproto"
"git.apache.org/thrift.git/lib/go/thrift"
)
...
英文:
This help message from go test
seems to suggest you can filter the packages you're testing:
-coverpkg pkg1,pkg2,pkg3
Apply coverage analysis in each test to the given list of packages.
The default is for each test to analyze only the package being tested.
Packages are specified as import paths.
Sets -cover.
Another simpler option, and this is what I do, is to import the generated code as a library package that sits outside your code tree, and thus the cover tool ignores it in its stats.
e.g. if your app is github.com/Fuser97381/myproj
, put the generated code in github.com/Fuser97381/protocols
. Then your code looks like this:
package main
import (
"github.com/Fuser97381/protocols/myproto"
"git.apache.org/thrift.git/lib/go/thrift"
)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论