工作要搞覆蓋率測試,看到公司平台上用的jacoco,就找了網上的demo自己跑了一下。
一.覆蓋率測試是干什么的
http://www.open-open.com/lib/view/open1472174544246.html
1.用來看看類/方法/判定 中的語句的使用情況,防止廢棄無用的代碼和錯誤代碼出現。
2.用來和需求覆蓋做一個對照,做用例和代碼的一種映射檢查,補齊一些用例的邏輯上的缺陷。
二.jacoco的原理
jacoco提供兩種插樁方式,on-the-fly 和offline兩種,這兩種的實現原理不同。
參考:http://www.open-open.com/lib/view/open1472174544246.html
三.項目代碼
參考網址:https://www.eclemma.org/jacoco/trunk/doc/maven.html
pom部分
<dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.3</version> <type>maven-plugin</type> </dependency>
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.3</version> <!-- <configuration> <includes>zhizhi</includes> </configuration> --> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-prepare-agent-integration</id> <goals> <goal>prepare-agent-integration</goal> </goals> </execution> <execution> <id>default-report</id> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-report-integration</id> <goals> <goal>report-integration</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>BUNDLE</element> <limits> <limit> <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.60</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin>
執行mvn語句
mvn install
在 target/site/jacoco 中可以看到report
最后,自有平台的jacoco的實際實現過程