问题
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?
回答1:
After a lot try and fail I got it working.
- Edit the pom.
- mvn clean test-compile
- mvn cobertura:cobertura
- Reopen the page from Firefox. (make sure the page is not cached)
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
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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>
回答6:
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.
来源:https://stackoverflow.com/questions/15234914/cobertura-maven-plugin-excludes-configuration