Reputation: 18760
I have a Maven project with a test case DefaultViewTypeToFragmentMapperTest.java
in the directory /src/test/java/test/com/mycompany/myproduct/android/viewtype2fragmentmapper/
.
I want this test case to be excluded from unit test coverage calculation. In order to achieve this result, I configured the plugin like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<instrumentation>
<excludes>
<exclude>test/co/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
But I still see the aforementioned class in the coverage report.
How can I fix it such that the test case does not appear in the report and its coverage (0 % according to the report) is not taken into account?
Upvotes: 11
Views: 22392
Reputation: 1491
In addition to BertKoor's answer, I'd like to point out that if you're executing mvn cobertura:cobertura
or mvn cobertura:cobertura-integration-test
directly, your report will still include coverage on all instrumented classes found in your target directory, as reported here!
If this is the case, make sure you do mvn **clean** cobertura:cobertura
in order to clean up the target dir from a previous build first, and then instrument and run your tests.
Upvotes: 0
Reputation: 201
I just lost two hours of my life getting an exclusion for Cobertura to be excluded, but finally succeeded.
The solution I found is that the plugin configuration with instrumentation & exclusion for the cobertura-maven-plugin MUST be in the build/plugins or build/pluginManagement chapter of the pom, while there also must be a reference to the cobertura-maven-plugin in the reporting chapter.
The pitfall here is that you initially start with defining the plugin at the reporting chapter, otherwise no report is generated. But the instrumentation itself doesn't pick up any configuration from that part of the pom. You need to define that within the build chapter.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<instrumentation>
<excludes>
<exclude>my/exclusion/package/**</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
Upvotes: 3
Reputation: 301
After a lot try and fail I got it working.
I got it working with:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<instrumentation>
<excludes>
<exclude>aaa/**/*.class</exclude>
<exclude>com/test/bbb/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
Change 'aaa' with the beginning of the package name to be excluded. Change 'bbb' with your package name that you want to exclude from the report.
I hope it helps, Marc Andreu
Upvotes: 11
Reputation: 9069
You should not append the .class as the following example
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<instrumentation>
<excludes>
<exclude>test/co/**/*</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
I hope this may help.
Upvotes: 4
Reputation: 6640
Is it a typo?
<exclude>test/co/**/*.class</exclude>
.
The co should be com.
BTW, <ignores>
instructs Cobertura to ignore any calls to any method that matches the ignore regular expression. It will NOT skip over these classes during instrumention. To exclude classes from being instrumented, <excludes>
should be used.
Upvotes: 5
Reputation: 1296
You should use the <ignore>
tag.
<configuration>
<instrumentation>
<ignores>
<ignore>com.example.boringcode.*</ignore>
</ignores>
</instrumentation>
</configuration>
<exclude>
used within <instrumentation>
simply excludes the package from what your instrumenting. Which in this case, is nothing because you're not doing anything.
Please see the Mojo Maven Cobertura Plugin docs here.
Upvotes: 6