maven工程
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.jacoco.tutorial</groupId>
<artifactId>jacoco-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<!-- 限定跑哪些測試類 -->
<includes>
<!-- "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test". -->
<!-- "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test". -->
<include>**/*Test.java</include>
<!-- 支持正則 http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html-->
<!-- <include>%regex[.*(Cat|Dog).*Test.*]</include> -->
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<includes>
<!-- 限定只統計部分類的覆蓋率 -->
<include>com/jacoco/tutorial/Calculator*</include>
</includes>
<destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${project.build.directory}/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>
說明:
1.如果不限制跑哪些測試用例,其中的 <artifactId>maven-surefire-plugin</artifactId>的<plugin>可以註釋掉。
2.<!-- 限定跑哪些測試類 -->的<includes>可以刪除。
java代碼
package com.jacoco.tutorial;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
}
package com.jacoco.tutorial;
import org.junit.Assert;
import org.junit.Test;
public class CalculatorTest {
private Calculator instance = new Calculator();
@Test
public void testAdd() {
int a = 10;
int b = 20;
int expected = 30;
Assert.assertEquals(expected, instance.add(a, b));
}
@Test
public void testSub() {
int a = 10;
int b = 20;
int expected = -10;
Assert.assertEquals(expected, instance.sub(a, b));
}
}
生成覆蓋率報告:
run as>mvn test
報告首頁位置:
默認在
\target\site\jacoco\index.html
結果
可以通過outputDirectory(具體位置參考如下)修改下默認位置:
<plugin>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<configuration>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
生成測試用例的測試報告,在pom.xml所在的目錄執行:
mvn test surefire-report:report
位置在:
/target/site/surefire-report.html
結果:
更專業的文章:
http://www.51testing.com/html/11/n-3726211.html
提到 實時插樁模式(on-the-fly)、離線插樁(offline instrumentation) ,jacoco應該可以實現實時監控測試人員人工測試後的覆蓋率。