jonbonazza
jonbonazza

Reputation: 944

Ignore code blocks in Golang test coverage calculation

I am writing unit tests for my golang code, and there are a couple methods that I would like to be ignored when coverage is calculated. Is this possible? If so, how?

Upvotes: 33

Views: 16184

Answers (1)

Jason Coco
Jason Coco

Reputation: 78393

One way to do it would be to put the functions you don't want tested in a separate go file, and use a build tag to keep it from being included during tests. For example, I do this sometimes with applications where I have a main.go file with the main function, maybe a usage function, etc., that don't get tested. Then you can add a test tag or something, like go test -v -cover -tags test and the main might look something like:

//+build !test

package main

func main() {
    // do stuff
}

func usage() {
    // show some usage info
}

Upvotes: 19

Related Questions