如何将生成的代码从覆盖率统计中排除?

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

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

huangapple
  • 本文由 发表于 2015年6月10日 04:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/30742456.html
匿名

发表评论

匿名网友

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

确定