Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude generated code from coverage statistics

Tags:

testing

go

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

like image 889
Darth Egregious Avatar asked Oct 25 '25 03:10

Darth Egregious


1 Answers

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

...
like image 140
Not_a_Golfer Avatar answered Oct 26 '25 18:10

Not_a_Golfer