前言
jacoco是一個開源的覆蓋率工具,它針對的開發語言是java,其使用方法很靈活,可以插樁到Ant、Maven中,可以使用其JavaAgent技術監控Java程序等。
那么本次主要使用對java代碼進行覆蓋率統計,比如A測試攻城獅在功能測試時/接口測試時,實際覆蓋測試了多少java代碼,可以作為一個評判。
jacoco官網地址:http://www.eclemma.org(有牆)
一、jacoco基本介紹
1、jacoco是根據控制流Type來采用不同的探針插樁策略的。(植入探針)
2、探針原理是字節碼指令集插入到java方法中,程序執行后可以被記錄,它不會改變原有代碼的行為。
3、jacoco支持on-the-fly和offline的兩種插樁模式。本次主要使用在java -jar xx.jar啟動的時候加入參數(on-the-fly)
行覆蓋率:度量被測程序的每行代碼是否被執行,判斷標准行中是否至少有一個指令被執行。
類覆蓋率:度量計算class類文件是否被執行。
分支覆蓋率:度量if和switch語句的分支覆蓋情況,計算一個方法里面的總分支數,確定執行和不執行的 分支數量。
方法覆蓋率:度量被測程序的方法執行情況,是否執行取決於方法中是否有至少一個指令被執行。
指令覆蓋:計數單元是單個java二進制代碼指令,指令覆蓋率提供了代碼是否被執行的信息,度量完全 獨立源碼格式。
圈復雜度:在(線性)組合中,計算在一個方法里面所有可能路徑的最小數目,缺失的復雜度同樣表示測 試案例沒有完全覆蓋到這個模塊。
二、必備環境
本篇基於Windows下使用:
1、ant 環境
所有版本:https://downloads.apache.org/ant/binaries/
本篇直接點擊此鏈接下載:https://downloads.apache.org/ant/binaries/apache-ant-1.9.15-bin.zip 下載后直接解壓:
注意Windows后綴為.zip,如:apache-ant-1.9.15-bin.zip
設置環境變量:
①將bin、lib目錄添加至環境變量中的系統變量path即可,如下圖我的解壓路徑:
D:\apache-ant-1.9.15\bin D:\apache-ant-1.9.15\lib
②cmd驗證環境:
cmd輸入:ant -v
如下圖即可顯示ant的版本為:1.9.15
2、jacoco
全部版本下載地址:https://repo1.maven.org/maven2/org/jacoco/jacoco/
可在我網盤中下載鏈接:https://pan.baidu.com/s/19evt3K1eo1y7GeeyggD5Hg 提取碼:gsxl
強烈推薦clone git:https://github.com/git-gsxl/jacoco-demo.git
隨你自己解壓到哪個目錄,
3、java.jar 與 build.xml 文件
文件在網盤中下載鏈接:https://pan.baidu.com/s/19evt3K1eo1y7GeeyggD5Hg 提取碼:gsxl
強烈推薦clone git:https://github.com/git-gsxl/jacoco-demo.git
4、配置build.xml文件
①配置 jacocoant.jar 文件路徑;
②配置源碼路徑
③配置server_ip
④其它自行根據所需配置
三、啟動 jar 服務
1、在 jar 包cmd下輸入啟動命令:java -javaagent:F:/jacoco-0.8.6/lib/jacocoagent.jar=includes=*,output=tcpserver,port=9999,address=127.0.0.1,append=true -jar demo-V1024.jar
通過javaagentJavaAgent會監聽服務器端設置的TCP port,然后把收集到的信息(jacoco.exec)寫入TCP connection,所以服務不需要關閉。
C:\Users\Administrator\Desktop\jacoco-demo>java -javaagent:F:/jacoco-0.8.6/lib/jacocoagent.jar=includes=*,output=tcpserver,port=9999,address=127.0.0.1,append=true -jar demo-V1024.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE) 2020-10-24 18:08:50.125 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : Starting JacocodemoApplication vV1024 on WIN-Q3LSDAOKUJ1 with PID 13264 (C:\Users\Administrator\Desktop\jacoco-demo\demo-V1024.jar started by Administrator in C:\Users\Administrator\Desktop\jacoco-demo) 2020-10-24 18:08:50.133 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : No active profile set, falling back to default profiles: default 2020-10-24 18:08:53.933 INFO 13264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-10-24 18:08:53.989 INFO 13264 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-10-24 18:08:53.989 INFO 13264 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38] 2020-10-24 18:08:54.195 INFO 13264 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-10-24 18:08:54.222 INFO 13264 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3949 ms 2020-10-24 18:08:54.748 INFO 13264 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-10-24 18:08:55.272 INFO 13264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-10-24 18:08:55.301 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : Started JacocodemoApplication in 6.268 seconds (JVM running for 7.3)
2、運行 ant 命令
在此之前我們在瀏覽器上訪問我們的服務,ip+prot (既我已經訪問了hello的方法,業務測試覆蓋了hello方法)
①ant dump
C:\Users\Administrator\Desktop\jacoco-demo>ant dump Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.8.0_151\lib\tools.jar Buildfile: C:\Users\Administrator\Desktop\jacoco-demo\build.xml dump: [jacoco:dump] Connecting to /127.0.0.1:9999 [jacoco:dump] Dumping execution data to C:\Users\Administrator\Desktop\jacoco-report\jacoco-integation.exec BUILD SUCCESSFUL Total time: 0 seconds C:\Users\Administrator\Desktop\jacoco-demo>
②ant report
C:\Users\Administrator\Desktop\jacoco-demo>ant report Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.8.0_151\lib\tools.jar Buildfile: C:\Users\Administrator\Desktop\jacoco-demo\build.xml report: [mkdir] Created dir: C:\Users\Administrator\Desktop\jacoco-demo\jacoco-report [jacoco:report] Loading execution data file G:\5-ppl-study-java\jacoco-demo\target\coverage\jacoco-integation.exec [jacoco:report] Writing bundle 'Check Order related' with 2 classes BUILD SUCCESSFUL Total time: 0 seconds
四、查看覆蓋率報告
1、在jacoco-report目錄下打開,index.html即可
2、點擊進入 controller 查看詳情
可以發現與我們上述所示的一致,只覆蓋hello方法。剩下看你們了,去試試運行其它方法,再執行ant,再查看報告。
歡迎來大家QQ交流群一起學習:482713805