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.

upvote
  flag
Hopefully, it's coming soon. Issue: sourceforge.net/tracker/… and conversation about a release to include this sourceforge.net/mailarchive/… – Kenneth Xu
upvote
  flag
See this post, This is a option for exclude code of cobertura with sonar and jenkins but I hope that its answer can apply for non jenkins projects – nd.valencia

4 Answers 11

up vote 52 down vote accepted

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>
upvote
  flag
Thanks. Is there anything that can be added to the code to exclude a method? Would be easier than a long long list in ANT. Any annotation support? – ReneS
upvote
  flag
I made a very quick search to Cobertura docs, but I couldn't find anyting about Cobertura's annotations. So it seems you need to work on your unit-tests or package structure to make your exclude lists shorter ;) – Juha Syrjälä
upvote
  flag
Ok, that confirms, that I have not missed anything. Hope for a hidden feature... well, maybe we get one sooner or later. Thanks! – ReneS
upvote
  flag
Anywhere, is there a reference , to how the regulare expressions need to be deviced??Basically , any reference on how different regular expressions will behave – Neeraj
1 upvote
  flag
Has anyone confirmed whether the instructions on the plugin actually work? I tried a few different settings and I couldn't get the exclusions to work correctly. – Archimedes Trajano
upvote
  flag
There's a bug in the exclusion of classes. See here: jira.codehaus.org/browse/MCOBERTURA-52 – user321068
upvote
  flag
The answer is really helpful, but you should edit the post in order to say that the maven part goes in the plugin section and no reporting. I did not check the link until I read this answer //allinonescript.com/a/6815777/3294286 – sebadagostino

Cobertura doesn't currently provide such a feature, and neither does Emma (which we use) although it is listed as a forthcoming enhancement - although in the form of an extension to the exclusion rules I believe rather than as an annotation.

Would be handy to cover off those few inaccessible corners cleanly so that you can strive for 100% without being ridiculous.

I think annotations would probably be a friendlier way to do it, but they ought to be fairly explicitly named and based on a list of acceptable scenarios as I fear otherwise something like '@ExcludeFromCoverage' would get added over generously.

upvote
  flag
-1 because it is incorrect to write "cobertura doesn't currently provide such a feature". It does, according to the accepted answer. – Jesper Rønn-Jensen
upvote
  flag
-1 because it's wrong – Marcel Stör
upvote
  flag
@Jason, see my answer, maybe you are doing the same mistake I was. – Iker Jimenez
9 upvote
  flag
actually the question is about excluding methods, not classes, and this answer is correct that its impossible right now. – Mateusz Chromiński
upvote
  flag
Edited the question title to avoid the confusion. This answer is correct btw,as @MateuszChromiński said – Nikhil

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.

upvote
  flag
This is the right answer! I was banging my head against this too putting it in the <report> section of a Maven 3.0.4 pom. – Melloware
2 upvote
  flag
can you provide an example? – add9
upvote
  flag

Remember to exclude inner classes too.

<exclude>path/to/class/MyClass*.class</exclude>

It took me ages to notice I was missing an asterisk!

upvote
  flag
Found this to be the most useful answer! Apparently the star before the dot is essential for this to work. Thanks! – pdp

Not the answer you're looking for? Browse other questions tagged or ask your own question.