Exclude methods from code coverage with Cobertura
Asked Answered
A

5

56

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.

Accomplishment answered 4/6, 2009 at 16:15 Comment(2)
Hopefully, it's coming soon. Issue: sourceforge.net/tracker/… and conversation about a release to include this sourceforge.net/mailarchive/…Frager
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 projectsSaltworks
L
61

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>
Lynseylynus answered 4/6, 2009 at 16:31 Comment(7)
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?Accomplishment
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 ;)Rancor
Ok, that confirms, that I have not missed anything. Hope for a hidden feature... well, maybe we get one sooner or later. Thanks!Accomplishment
Anywhere, is there a reference , to how the regulare expressions need to be deviced??Basically , any reference on how different regular expressions will behaveWelbie
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.Dedradedric
There's a bug in the exclusion of classes. See here: jira.codehaus.org/browse/MCOBERTURA-52Myron
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 https://mcmap.net/q/206354/-exclude-methods-from-code-coverage-with-coberturaHarrisonharrod
E
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.

Envelope answered 25/7, 2011 at 12:1 Comment(2)
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.Poaceous
can you provide an example?Verulamium
B
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!

Bestrew answered 26/11, 2012 at 14:54 Comment(1)
Found this to be the most useful answer! Apparently the star before the dot is essential for this to work. Thanks!Clipfed
B
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.

Belden answered 30/12, 2010 at 11:23 Comment(4)
-1 because it is incorrect to write "cobertura doesn't currently provide such a feature". It does, according to the accepted answer.Comfy
@Jason, see my answer, maybe you are doing the same mistake I was.Envelope
actually the question is about excluding methods, not classes, and this answer is correct that its impossible right now.Vibraphone
Edited the question title to avoid the confusion. This answer is correct btw,as @MateuszChromiński saidWalls
D
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

Dunk answered 22/1, 2019 at 8:54 Comment(1)
Missing one critical piece: adding the annotation to the cobertura-instrument task: <cobertura-instrument> <ignoreMethodAnnotation annotationName="foo.bar.CoverageIgnore"/> </cobertura-instrument>Ganesa

© 2022 - 2024 — McMap. All rights reserved.