1、下載 jacoco
官網地址:EclEmma - JaCoCo Java Code Coverage Library
2、拷貝 jar 包?
下載好后,找到這兩個文件,然后找到被測項目
3、啟動 jacocoagent,監控被測項目?
java -javaagent:jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=localhost,append=true -jar jacoco-demo-1.0.0.jar
#jacoco-demo-1.0.0.jar為被測項目jar包
$ java -javaagent:jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=localhost,append=true -jar jacoco-demo-1.0.0.jar
16:23:53,451 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.4.5
16:23:53,467 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:23:53,483 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml]
16:23:53,499 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@4808bc9b - URL [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml] is not of type file
16:23:53,658 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [stdout]
16:23:53,658 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:23:53,674 |-INFO in ch.qos.logback.core.model.processor.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:23:53,722 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [ErrorFile]
16:23:53,722 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:53,738 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1893960929 - No compression will be used
16:23:53,738 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1893960929 - Will use the pattern c:/logs/error/error-%d{yyyy-MM-dd}.%i.log for the active file
16:23:53,770 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/error/error-%d{yyyy-MM-dd}.%i.log'.
16:23:53,770 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - Roll-over at midnight.
16:23:53,786 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - Setting initial period to Sun Aug 13 16:11:32 CST 2023
16:23:53,786 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:53,786 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@2ca923bb - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:53,794 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:53,794 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: c:/logs/error/error.log
16:23:53,794 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [c:/logs/error/error.log]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [AllFile]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:53,802 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@333392524 - No compression will be used
16:23:53,802 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@333392524 - Will use the pattern c:/logs/all/all-%d{yyyy-MM-dd}.%i.log for the active file
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/all/all-%d{yyyy-MM-dd}.%i.log'.
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - Roll-over at midnight.
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - Setting initial period to Sun Aug 13 16:22:01 CST 2023
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1ebea008 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:53,802 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: c:/logs/all/all.log
16:23:53,802 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [c:/logs/all/all.log]
16:23:53,802 |-INFO in ch.qos.logback.classic.model.processor.RootLoggerModelHandler - Setting level of ROOT logger to INFO
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [stdout] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [ErrorFile] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [AllFile] to Logger[ROOT]
16:23:53,802 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@72d6b3ba - End of configuration.
16:23:53,802 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1787f2a0 - Registering current configuration as safe fallback point16:23:54,405 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@64030b91 - URL [jar:file:/E:/soft/jacoco/jacoco-0.8.10/lib/jacoco-demo-1.0.0.jar!/BOOT-INF/classes!/logback.xml] is not of type file
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [stdout]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.ImplicitModelHandler - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [ErrorFile]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@540206885 - No compression will be used
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@540206885 - Will use the pattern c:/logs/error/error-%d{yyyy-MM-dd}.%i.log for the active file
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/error/error-%d{yyyy-MM-dd}.%i.log'.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - Roll-over at midnight.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - Setting initial period to Sun Aug 13 16:11:32 CST 2023
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4d23015c - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - Active log file name: c:/logs/error/error.log
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ErrorFile] - File property is set to [c:/logs/error/error.log]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - Processing appender named [AllFile]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderModelHandler - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@943659381 - No compression will be used
16:23:54,422 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@943659381 - Will use the pattern c:/logs/all/all-%d{yyyy-MM-dd}.%i.log for the active file
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - The date pattern is 'yyyy-MM-dd' from file name pattern 'c:/logs/all/all-%d{yyyy-MM-dd}.%i.log'.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - Roll-over at midnight.
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - Setting initial period to Sun Aug 13 16:22:01 CST 2023
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@441cc260 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - This appender no longer admits a layout as a sub-component, set an encoder instead.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
16:23:54,422 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - Active log file name: c:/logs/all/all.log
16:23:54,422 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[AllFile] - File property is set to [c:/logs/all/all.log]
16:23:54,422 |-INFO in ch.qos.logback.classic.model.processor.RootLoggerModelHandler - Setting level of ROOT logger to INFO
16:23:54,422 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@73a00e09 - Propagating INFO level on Logger[ROOT] onto the JUL framework
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [stdout] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [ErrorFile] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.AppenderRefModelHandler - Attaching appender named [AllFile] to Logger[ROOT]
16:23:54,422 |-INFO in ch.qos.logback.core.model.processor.DefaultProcessor@26dcd8c0 - End of configuration.
16:23:54,422 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@66e889df - Registering current configuration as safe fallback point. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.0.2)[INFO ] 2023-08-13 16:23:54.557 [main] com.ybw.JacocoDemoApplication - Starting JacocoDemoApplication using Java 17.0.4.1 with PID 11912 (E:\soft\jacoco\jacoco-0.8.10\lib\jacoco-demo-1.0.0.jar started by 魏先生 in E:\soft\jacoco\jacoco-0.8.10\lib)
[INFO ] 2023-08-13 16:23:54.557 [main] com.ybw.JacocoDemoApplication - No active profile set, falling back to 1 default profile: "default"
[INFO ] 2023-08-13 16:23:55.593 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.catalina.core.StandardService - Starting service [Tomcat]
[INFO ] 2023-08-13 16:23:55.597 [main] o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/10.1.5]
[INFO ] 2023-08-13 16:23:55.673 [main] o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
[INFO ] 2023-08-13 16:23:55.688 [main] o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1042 ms
[INFO ] 2023-08-13 16:23:56.038 [main] o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
[INFO ] 2023-08-13 16:23:56.054 [main] o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
[INFO ] 2023-08-13 16:23:56.054 [main] com.ybw.JacocoDemoApplication - Started JacocoDemoApplication in 2.115 seconds (process running for 2.942)
參數描述
- includes=*:這個代表了,啟動時需要進行字節碼插樁的包過濾,*代表所有的class文件加載都需要進行插樁。
- 你可以寫成: includes=com.test.service.*;這個參數我們可以用來做maven多模塊的覆蓋率,比如我們只想查看service服務層的覆蓋率,我們可以通過設置包路徑的方式進行只統計當前包的覆蓋率。
- output=tcpserver:output主要四個參數
-
file: At VM termination execution data is written to the file specified in the destfile attribute.(當jvm停止掉的時候產出dump文件,即服務掛了產出dump文件)
-
tcpserver: The agent listens for incoming connections on the TCP port specified by the?address and port attribute. Execution data is written to this TCP connection.(常用模式,將jacocoaget作為服務,每次通過cli包進行dump命令去獲取dump包)
-
tcpclient: At startup the agent connects to the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.(將jacocoagent做為客戶端,向指定ip和端口的服務推送dump信息)
-
none: Do not produce any output.(不產出任何dump,dump個寂寞,忽略)
-
-
port=6300:這是jacoco開啟的tcpserver的端口,請注意這個端口不能被占用。
-
address=localhost:這是對外開發的tcpserver的訪問地址。
-
可以配置127.0.0.1,也可以配置為實際訪問ip配置為127.0.0.1的時候,dump數據只能在這臺服務器上進行dump,就不能通過遠程方式dump數據。配置為實際的ip地址的時候,就可以在任意一臺機器上(前提是ip要通,不通都白瞎),通過ant xml或者api方式dump數據。
-
舉個栗子:我如上配置了192.168.110.1:2014作為jacoco的tcpserver啟動服務,
那我可以在任意一臺機器上進行數據的dump,比如在我本機windows上用api或者xml方式調用dump。如果我配置了127.0.0.1:2014作為啟動服務器,那么我只能在這臺測試機上進行dump,其他的機器都無法連接到這個tcpserver進行dump -
如果不知道本機ip地址,可以使用0.0.0.0,這樣ip地址會綁定主機ip。
-
-
append=true:是執行數據文件已經存在,則覆蓋數據將附加到現有文件。
頁面或接口調用
調用接口http://localhost:8080/user/getUser?id=1
4、cli 包 dump 生成 exec 文件(注意一定要測試完畢之后)
$ java -jar jacococli.jar dump --address localhost --port 6300 --destfile jacoco-demo.exec
[INFO] Connecting to localhost/127.0.0.1:6300.
[INFO] Writing execution data to E:\gitcode\mygit\learn\jacoco\jacoco-demo\target\jacoco-demo.exec.
-
--address localhost?--port 6300 指向jacocoagent啟動IP和端口
-
jacoco-demo.exec 為生成exec文件名
5、生成report?
$ java -jar jacococli.jar report jacoco-demo.exec --classfiles E:/gitcode/mygit/learn/jacoco/jacoco-demo/target/classes --sourcefiles E:/gitcode/mygit/learn/jacoco/jacoco-demo/src/main/java --html html-report --xml report.xml --encoding=utf-8
[INFO] Loading execution data file E:\gitcode\mygit\learn\jacoco\jacoco-demo\target\jacoco-demo.exec.
[INFO] Analyzing 4 classes.
- --sourcefiles 和 --classfiles 為本地被測項目源碼和字節碼路徑。
- --html 為生成報告目錄。
6、查看覆蓋率報告
?
表格
- Element:元素;
- Missed Instructions?? ?Cov:未覆蓋字節碼行數;
- Missed Branches?? ?Cov:未覆蓋的分支數;
- Cxty:圈復雜度;
- Lines:行;
- Methods:方法;
- Classes:類;?
覆蓋率標識
- 條件覆蓋:紅鉆:表示未覆蓋;黃鉆:表示部分覆蓋;綠鉆:表示全部覆蓋;
- 行覆蓋:全覆蓋(綠色),未覆蓋(紅色),半覆蓋(黃色),無視(白色)
只調用http://localhost:8080/user/getUser?id=1,效果如下
調用如下接口
http://localhost:8080/user/getUser?id=1、http://localhost:8080/user/getUser?id=2、http://localhost:8080/user/getUser?id=3???????、http://localhost:8080/user/getUser?id=5
效果如下,覆蓋率為100%:
?
?
?
7、單元測試
上面都是功能測試代碼覆蓋率,jacoco單元測試文章可以看:
idea 測試覆蓋率_idea單元測試覆蓋率_xixingzhe2的博客-CSDN博客
8、demo代碼
share: 分享倉庫 - Gitee.com