How to ignore generated files from Go test coverage

23,704

Solution 1

Most Go tools operate on packages, because a package itself forms a unit that may be useful in its entirety. Excluding files from a package could easily "break" the package: the excluded file may contain (crucial) package initialization code or may even cause the remaining package files fail to compile.

go test is no exception: it also operates on packages. There is no first-hand support to exclude files from a package.

If your package may be compiled and tested without the generated file, you may opt to generate the file into a different package, and then it won't be included in your package's (coverage) test naturally.

Another way to handle this would be to still generate it in the same package / folder, and use special build tags in the generated files, which you may exclude when running the coverage test. You can read more about build tags here: Build Constraints, and here: What is the right approach to encapsulate platform specific code in Go?

If the generated file is needed to compile / test the package, then you still have an option: use an internal package for the generated file. Internal packages are only available to the package tree rooted at the internal folder, which means you may export any identifiers in an internal package, the compiler ensures they won't be used by "unintended" parties. You can read more about internal packages here: Can I develop a go package in multiple source directories?

Also consider the option to write or generate test for the generated code, which may be good practice anyway, so you wouldn't have to use tricks to exclude them from coverage tests.

Solution 2

You could strip the generated code from the cover profiles:

go test . -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.go" > cover.out
tool cover -func cover.out

Depending on the used tools this can be implemented easily in the pipeline/make.

Solution 3

Following the ways that I could solve this need.

By excluding dirs/pkgs

Since to run the test and generated the cover you will specify the pkgs where the go test command should look for the files out of this pkg will be ignored automatically. Following an example

project
|_______/pkg/web/app 
|_______/pkg/web/mock -> It will be ignored.

# Command:   
$go test -failfast -tags=integration -coverprofile=coverage.out -covermode=count github.com/aerogear/mobile-security-service/pkg/web/apps

However, it cannot be applied in all cases. For example, in the case of the files be mock routines of your interfaces it may not work very well because the imports which are required into the service, test and mock probably will be in a cycle and in this not allowed.

By using "_test" in the name definitions

If you would like to ignore files which are used in the tests then make sense use _test at the end of the name. For example, my_service_mock_test.go. The files with _test at the end of the name will be ignored by default.

By removing from the cover file as follows

go test . -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.go" > cover.out
tool cover -func cover.out

PS.: I could not find a comment or tag which would exclude them.

Solution 4

You can grep those directories out from the test.

So, instead of:

go test ./...

You do this:

go test `go list ./... | grep -v ./excluded/autogen/directory/here`

Also, make sure that you don't add a trailing /. It doesn't work perfectly.

Share:
23,704

Related videos on Youtube

Rohanil
Author by

Rohanil

I am a self-taught Backend developer. My educational background is in Electrical Engineering. I did my specialization in Signal Processing which is equivalent to Data Processing. And from there, I got interest in software development. I have been working as a software developer for the more than 6 years. I worked with Python for the first 2 years. Then I got into Golang for 2 years. I have experience using various tools and frameworks. I have worked professionally using flask, postgresql, mongodb, docker, celery, kafka, influxdb, elasticsearch, git, jenkins, php, javascript, vuejs.

Updated on December 15, 2020

Comments

  • Rohanil
    Rohanil over 3 years

    I have a generated file in my package with DO NOT EDIT on top. I am running tests for my package with go test -coverprofile=cover.out <package>. This creates coverage profile and shows total coverage percentage. But it also includes generated files while calculating the coverage. Is there a way to ignore generated files in coverage calculation?

    • Tim Cooper
      Tim Cooper about 6 years
      Did you examine cover.out? You should notice that it contains the package filenames. You could filter those lines based on if the file contains the text "DO NOT EDIT" in its head.
    • Tim Cooper
      Tim Cooper about 6 years
      After you filter the file, you can use go tool cover to reanalyse the coverage amount.
  • Corey Ogburn
    Corey Ogburn about 4 years
    This could result in an inaccurate total at the bottom of the report.