Java 如何让jacoco将测试文件本身添加到覆盖率报告中
在我的示例maven项目中,我有以下jacoco配置:Java 如何让jacoco将测试文件本身添加到覆盖率报告中,java,maven,code-coverage,jacoco,jacoco-maven-plugin,Java,Maven,Code Coverage,Jacoco,Jacoco Maven Plugin,在我的示例maven项目中,我有以下jacoco配置: <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.4</version> <executions> <execution> <id>jacoco
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
我从那里得到的,然后换成了最新的版本
它对于实现src/main的覆盖非常有效,但是没有为测试本身src/test提供任何覆盖信息
虽然我同意这是一个合理的默认值,但我想将其更改为我的一个项目,以便告诉我测试的覆盖率信息。有人知道怎么做吗
我这里有一个完整的例子 此外,我们还可以在jacoco插件中添加我们希望在项目中使用limit和counter的最小覆盖率,或者允许跳过的最大类数 下面是插件的示例:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-check</id>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>50</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
然而,根据目前的情况,jacocomaven插件并没有提供此功能
可以通过maven antrun插件使用Ant任务为测试源生成报告
比如说
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.ant</artifactId>
<classifier>nodeps</classifier>
<version>0.8.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<typedef resource="org/jacoco/ant/antlib.xml"/>
<report>
<executiondata>
<fileset dir="target" includes="jacoco.exec"/>
</executiondata>
<structure name="Coverage Report">
<classfiles>
<fileset dir="${basedir}/target/test-classes"/>
</classfiles>
<sourcefiles>
<fileset dir="${basedir}/src/test/java"/>
</sourcefiles>
</structure>
<html destdir="${basedir}/target/coverage-report/html"/>
</report>
</target>
</configuration>
</execution>
</executions>
</plugin>
为您的测试生成以下报告