How to exclude generated code from coverage statistics

2.3k Views Asked by At

I have thrift generated code in my project? How do I stop this from affecting my coverage stats? They're dismal.

1

There are 1 best solutions below

4
On BEST ANSWER

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

...