1.單元測試覆蓋率:
maven命令的方式:
1.生成jacoco.exec:
mvn org.jacoco:jacoco-maven-plugin:prepare-agent clean test -Dmaven.test.failure.ignore=true
2.生成index.html
cd 模塊 java -jar jacoco-0.8.4/lib/jacococli.jar report target/jacoco.exec --classfiles target/classes --html target/site/jacoco/
添加maven依賴的方式:
在項目root目錄 pom.xml中添加:
<plugin>
? ? ? ? ? ? ? ? <groupId>org.jacoco</groupId>
? ? ? ? ? ? ? ? <artifactId>jacoco-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <version>0.8.5</version>
? ? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <id>default-prepare-agent</id>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>prepare-agent</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <id>default-report</id>
? ? ? ? ? ? ? ? ? ? ? ? <phase>test</phase>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>report</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <id>default-check</id>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>check</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? </executions>
? ? ? ? ? ? </plugin>
運行 mvn test 即可在target/site/jacoco文件夾下生成報告。
如果想要跳過失敗的測試用例,請使用 mvn test -Dmaven.test.failure.ignore=true.
2.增量覆蓋率:
原理:
①先得到單元測試覆蓋率;
②diff代碼,指定工程根目錄和需要處理的子模塊及對比的版本.計算增量代碼覆蓋率,并標記修改覆蓋率報告.
python main.py -d . -m xmpush-common-spring-boot-starter -o 80e8d5220f906f1242736dc77ac8ef10947ff0fc
3.黑盒測試覆蓋率:
原理:on the fly模式.通過javaagent代理實現(xiàn).
tomcat項目:
1.配置Tomcat
1.1 把解壓獲取到的jacoco下的lib\jacocoagent.jar 文件放到tomcat安裝路徑\lib 下
1.2 配置Tomcat? \bin\catalina.bat文件,添加如下
# -javaagent: 的后面跟jacoco的安裝路徑
# includes= 選項,選擇你要覆蓋率的服務(wù),例如com.baidu.*
# port= 選項,選擇你要打開的端口
# address= 選項,tomcat服務(wù)所在機器的ip地址(如果想在跟tomcat服務(wù)同一臺機器上執(zhí)行ant任務(wù)的話,需要改為127.0.0.1)
set JAVA_OPTS="-javaagent:%CATALINA_BASE%\lib\jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=127.0.0.1"
2.部署項目
打包你的java工程項目到Tomcat 下的 \webapps 目錄下,并啟動Tomcat.
3.啟動項目
如果是springboot項目:
配置Vm
options:-javaagent:jacocoagent.jar=includes=*,output=tcpserver,port=8089,address=127.0.0.1
訪問項目提供的API進行測試.
Ant 構(gòu)建獲取代碼覆蓋率:
在服務(wù)器上,新建 build.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<project name="nelus" xmlns:jacoco="antlib:org.jacoco.ant">
? ? <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
? ? ? ? <classpath path="jacocoant.jar"/>
? ? </taskdef>
? ? <target name="dump">
? ? ? ? <!-- address和port和catalina.bat設(shè)置保持一致,destfile設(shè)置jacoco.exec保存地址 -->
? ? ? ? <jacoco:dump address="127.0.0.1" reset="true" port="8089" destfile="CodeCoverage\jacoco\jacoco.exec"/>
? ? </target>
? ? <target name="report">
? ? ? ? <jacoco:report>
? ? ? ? ? ? <executiondata>
? ? ? ? ? ? ? ? <file file="CodeCoverage\jacoco\jacoco.exec"/>
? ? ? ? ? ? </executiondata>
? ? ? ? ? ? <structure name="Example Project">
? ? ? ? ? ? ? ? <!-- 可以按軟件功能模塊設(shè)置多個group,方便在report中查看 -->
? ? ? ? ? ? ? ? <group name="SpringMVC">
? ? ? ? ? ? ? ? ? ? <classfiles>
? ? ? ? ? ? ? ? ? ? ? ? <!--.class文件路徑,可以放在客戶端-->
? ? ? ? ? ? ? ? ? ? ? ? <fileset dir="/home/xumeng/workspace/nelus/target/classes"/>
? ? ? ? ? ? ? ? ? ? </classfiles>
? ? ? ? ? ? ? ? ? ? <sourcefiles encoding="utf-8">
? ? ? ? ? ? ? ? ? ? ? ? <!--源代碼路徑,可以放在客戶端-->
? ? ? ? ? ? ? ? ? ? ? ? <fileset dir="/home/xumeng/workspace/nelus/src/main/java" />
? ? ? ? ? ? ? ? ? ? </sourcefiles>
? ? ? ? ? ? ? ? </group>
? ? ? ? ? ? </structure>
? ? ? ? ? ? <!-- report文件保存地址 -->
? ? ? ? ? ? <html destdir="CodeCoverage\jacoco\report"/>
? ? ? ? </jacoco:report>
? ? </target>
</project>
ant dump生成jacoco.exec文件
ant report 生成report文件(如果通過Jenkins插件這步驟可免)
生成報告地址是build.xml 中配置的。
查看報告>http://127.0.0.1:8089
