Jacoco&cobertura使用笔记
准备工作
安装JDK1.8
安装Maven3.6.1
创建java工程如jacoco
在工程jacoco根目录下运行mvn install
Jacoco
1. 配置pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.jacoco</groupId>
<artifactId>testJacoco</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>JaCoCo</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<configuration>
<!--指定生成.exec文件的存放位置-->
<destFile>target/coverage-reports/jacoco-unit.exec</destFile>
<!--Jacoco是根据.exec文件生成最终的报告,所以需指定.exec的存放路径-->
<dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. 在工程jacoco根目录下运行mvn test (其中test是包名),结束后,测试相关结果保存在target目录下,打开target中的index.html查看测试覆盖情况结果:
3. 结果展示
Cobertura(依旧在上面创建的工程jacoco中实验)
1. 配置pom.xml文件如下:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</build>
2. 在工程jococo根目录下运行 mvn cobertura:cobertura,运行结束后,测试覆盖统计结果保存在target\site中, 通过index.html查看测试覆盖情况结果:
3. 结果展示