-
引入插件
<!-- surefire plugin with spock and junit --> <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.9.0</version> <executions> <execution> <goals> <goal>compileTests</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <!-- 配置单测失败几次后停止执行 --> <skipAfterFailureCount>0</skipAfterFailureCount> <!-- 不允许跳过单测 --> <skipTests>false</skipTests> </configuration> </plugin>
引入 JaCoCo Plugin 的配置
<!-- JaCoCo plugin --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <configuration> <includes> <include>tech/**/*</include> </includes> </configuration> <executions> <execution> <id>pre-test</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>post-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
-
生成报告
做好上述报告后,直接执行
mvn test
就可以生成单测覆盖率报告了。如果没有什么异常的话,程序会生成单测覆盖率报告文件,地址为:target/site/jacoco/index.html
Surefire Plugin 官网文档
JaCoCo Plugin 官网文档