Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude methods from code coverage with Cobertura

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not drive down the coverage numbers.

I know that Clover has such a functionality, but I have not found anything similar for Cobertura.

like image 215
ReneS Avatar asked Jun 04 '09 16:06

ReneS


People also ask

How do you exclude code coverage methods?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

How do you exclude methods in JaCoCo code coverage?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

How does cobertura check code coverage?

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

How do you exclude test classes in sonar code coverage?

To do so, go to Project Settings > General Settings > Analysis Scope > Code Coverage and set the Coverage Exclusions property.


2 Answers

You can exclude classes from instrumentation. Then they should not appear on reports. See exclude statements below.

You can also ignore calls to some methods. See ignore statement below.

If you are using maven, see maven plugin manual.

    <configuration>       <instrumentation>         <ignores>           <ignore>com.example.boringcode.*</ignore>         </ignores>         <excludes>           <exclude>com/example/dullcode/**/*.class</exclude>           <exclude>com/example/**/*Test.class</exclude>         </excludes>       </instrumentation>     </configuration> 

And for ant see this.

<cobertura-instrument todir="${instrumented.dir}">   <ignore regex="org.apache.log4j.*" />   <fileset dir="${classes.dir}">     <include name="**/*.class" />     <exclude name="**/*Test.class" />   </fileset>   <fileset dir="${jars.dir}">     <include name="my-simple-plugin.jar" />   </fileset> </cobertura-instrument> 
like image 145
Juha Syrjälä Avatar answered Sep 30 '22 01:09

Juha Syrjälä


This has been breaking my head for some time now.

My problem was that I had the cobertura maven plugin setup in the reporting section instead of the build section.

The instrumentation settings, and hence the excluding of classes or packages, won't be applied if you don't set it up on build section, so watch out for this.

like image 37
Iker Jimenez Avatar answered Sep 30 '22 02:09

Iker Jimenez