55

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.

Niks
  • 4,802
  • 4
  • 36
  • 55
ReneS
  • 3,535
  • 2
  • 26
  • 35
  • Hopefully, it's coming soon. Issue: http://sourceforge.net/tracker/?func=detail&atid=720017&aid=3010530&group_id=130558 and conversation about a release to include this http://sourceforge.net/mailarchive/forum.php?thread_name=0F4966F4B6F55240A08C5F9AD979B3BE07E7EFB3%40MERCMBX16D.na.SAS.com&forum_name=cobertura-devel – Kenneth Xu Jun 18 '12 at 23:46
  • See [this post](http://stackoverflow.com/questions/8848585/excluding-java-files-in-sonar-report-through-jenkins/13003357#13003357), 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 Oct 22 '12 at 00:03

5 Answers5

59

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>
Arnost Valicek
  • 2,418
  • 1
  • 18
  • 14
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
  • 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 Jun 05 '09 at 06:42
  • 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ä Jun 05 '09 at 06:52
  • Ok, that confirms, that I have not missed anything. Hope for a hidden feature... well, maybe we get one sooner or later. Thanks! – ReneS Jun 05 '09 at 10:27
  • 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 Mar 07 '11 at 11:58
  • 1
    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 Oct 02 '11 at 20:34
  • There's a bug in the exclusion of classes. See here: http://jira.codehaus.org/browse/MCOBERTURA-52 –  Oct 31 '11 at 14:22
  • 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 http://stackoverflow.com/a/6815777/3294286 – Sebastian D'Agostino Apr 20 '17 at 14:27
21

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.

Iker Jimenez
  • 7,105
  • 9
  • 49
  • 46
17

Remember to exclude inner classes too.

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

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

Sarah Phillips
  • 923
  • 11
  • 30
  • Found this to be the most useful answer! Apparently the star before the dot is essential for this to work. Thanks! – pdp Dec 04 '17 at 13:39
4

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.

Jason
  • 915
  • 6
  • 6
0

Since 2.0 you can write your own @CoverageIgnore annotation.

It will be recognized by Cobertura, which will avoid considering annotated methods (does not work on classes as far as I know).

  1. Create an empty annotation:
public @interface CoverageIgnore {}
  1. Then annotate the methods you want to exclude from the reports:
public class SomeClass {
    @CoverageIgnore
    public void foo(String baz) {
        // useless stuff
    }
}

Source: https://github.com/cobertura/cobertura/wiki/Coverage-Annotations

Salvioner
  • 303
  • 5
  • 16