Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore generated files from Go test coverage

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?

like image 613
Rohanil Avatar asked Apr 27 '18 15:04

Rohanil


People also ask

How do you exclude files from code coverage?

To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class. To include assemblies that aren't part of your solution, obtain the . pdb files for these assemblies and copy them into the same folder as the assembly .

How do you manage test coverage?

Make appropriate test cases that cover the maximum test scenarios required based on the current release. Perform testing before the release so that the focus is provided to cover more scenarios in less time on a scheduled basis. Ensure to cover pending items of release while having a new release.

Should I ignore some files/functions for coverage purposes?

Another class of files/functions you might want to ignore for coverage purposes are test-specific helpers. It doesn’t matter that some of them aren’t run as part of tests, as they’re not the code under test. As with a lot of things in software, it’s about tradeoffs.

How to exclude files from code coverage analysis in unit test?

There are two ways to exclude files from code coverage in Unit Test. If these files are in one assembly and there doesn't have any other files need to be tested code coverage in this assembly, then you can exclude specified assemblies from code coverage analysis in test runsettings file. For example:

How to exclude a file from code coverage?

If the files are not in one assembly and there has any other file need to be include in code coverage, you need to exclude these files with <Sources> node in test runsettings file as below sample code. This has been tested successful in my side, the Class2.cs file is not include in code coverage result.

How do I ignore a file in jest?

We can use istanbul pragmas to ignore files using the following comment at the top of any file: See it in action at Exclude function or statement from Jest coverage on GitHub. Avoid this if you can, if you’re testing some code you should probably test all of that code.


1 Answers

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.

like image 104
Christian Avatar answered Oct 02 '22 21:10

Christian