Cobertura coverage ignore annotation in maven project?

5.3k Views Asked by At

I am trying to exclude certain methods from Unit test coverage. I am using Cobertura because I found out that since version 2.0 they introduced a coverage ignore annotation for excluding methods and classes: https://github.com/cobertura/cobertura/wiki/Coverage-Annotations

I set up my project as it should be, created a @interface called "CoverageIgnore" as it is in the article and annotated some methods. I am using Cobertura 2.0.3 and when generating the report the annotations don't seem to work :(

I found this article also - https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference#ignore-method-annotation which talks about some configuration of the instrument task in cobertura but it seems to be Ant-compliant:

<cobertura-instrument>
  <ignoreMethodAnnotation annotationName="foo.bar.CoverageIgnore"/>
</cobertura-instrument>

Is there something like this for my maven project? Thanks.

3

There are 3 best solutions below

0
On

Try this:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                ...
                <instrumentation>
                    <ignoreMethodAnnotations>
                        <ignoreMethodAnnotation>foo.bar.CoverageIgnore</ignoreMethodAnnotation>
                    </ignoreMethodAnnotations>
                </instrumentation>
                ...
            </configuration>
            ...
        </plugin>
        ...
    </plugins>
    ...
</build>

Notice that this is in the <build> element. I have not tried it in the <reporting> element, but I know that <ignore> and <exclude> only work in <build>, so it wouldn't surprise me if the ignore annotations don't work there either.

0
On

for the moment I've found that there is patch for maven-plugin which needs to be applied. hopefully will be fixed in 2.7 ?!

https://jira.codehaus.org/browse/MCOBERTURA-176

0
On

In order for @CoverageIgnore to work you must use it for instrumentation configuration in the build section of your pom. You might have you full specification for reporting in the parent pom (if multi-module that is) and have only something like this in your child pom:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <configuration>
              <instrumentation>
                <ignoreMethodAnnotations>
                    <ignoreMethodAnnotation>where.it.is.CoverageIgnore</ignoreMethodAnnotation>
                </ignoreMethodAnnotations>
              </instrumentation>
            </configuration>
        </plugin>
    </plugins>
</build>

In this way plugin will be executed in the build section and correctly instrument your annotated classes (reminder: only method annotation is supported by cobertura).