Jenkins集成jacoco收集单元测试覆盖率

Jenkins集成jacoco收集单元测试覆盖率

2020-02-28

目录

0 整体思路
1 Jenkins创建JacocoIntegrateTestDemo项目
2 配置源码管理
3 配置Build
4 配置构建后操作
5 构建执行,查看代码覆盖率
6 控制台输出

 

0 整体思路

  1. jenkins安装和全局工具配置见 1 安装版本 2 全局工具配置
  2. 单元测试不需要通过添加javaagent选项启动被测系统和执行ant dump来拉取jacoco.exec,只需在pom.xml设置插件,在被测项目中增加单测代码
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="JacocoDmo" xmlns:jacoco="antlib:org.jacoco.ant" default="jacoco">
      <!--Jacoco 的安装路径-->
      <property name="jacocoantPath" value="D:\software\jacoco\lib\jacocoant.jar"/>
      <!--最终生成 .exec 文件的路径,Jacoco 就是根据这个文件生成最终的报告的-->
      <property name="jacocoexecPath" value=".\target\jacoco.exec"/>
      <!--远程 Tomcat 服务的 ip 地址-->
      <property name="server_ip" value="10.8.116.81"/>
      <!--前面配置的远程 Tomcat 服务打开的端口,要跟上面配置的一样-->
      <property name="server_port" value="6300"/>
    
      <!--让 ant 知道去哪儿找 Jacoco-->
      <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
          <classpath path="${jacocoantPath}" />
      </taskdef>
    
      <!--dump 任务:
          根据前面配置的 ip 地址,和端口号,
          访问目标 Tomcat 服务,并生成 .exec 文件。-->
      <target name="dump">
          <jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoexecPath}" port="${server_port}" append="true"/>
      </target>
    </project>
    View Code
  3. 创建maven项目JacocoIntegrateTestDemo

1 Jenkins创建JacocoIntegrateTestDemo项目

图1 创建maven项目JacocoIntegrateTestDemo

2 配置源码管理

图2 配置源码管理

3 配置Build

图3 配置Build

4 配置构建后操作

图4 配置构建后操作

5 构建执行,查看代码覆盖率

图5 代码覆盖率

6 控制台输出

在 master 上构建 在工作空间 D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo 中
using credential 904f882b-46cf-42d6-ae43-68639b1b1482
 > D:\Program Files\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > D:\Program Files\Git\bin\git.exe config remote.origin.url git@e.coding.net:Ming8006/jacocodemo.git # timeout=10
Fetching upstream changes from git@e.coding.net:Ming8006/jacocodemo.git
 > D:\Program Files\Git\bin\git.exe --version # timeout=10
using GIT_SSH to set credentials 
 > D:\Program Files\Git\bin\git.exe fetch --tags --force --progress -- git@e.coding.net:Ming8006/jacocodemo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
 > D:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 06b9139dc9be810a86df0d093e2ab278ad765957 (refs/remotes/origin/master)
 > D:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
 > D:\Program Files\Git\bin\git.exe checkout -f 06b9139dc9be810a86df0d093e2ab278ad765957 # timeout=10
Commit message: "build.xml"
 > D:\Program Files\Git\bin\git.exe rev-list --no-walk 06b9139dc9be810a86df0d093e2ab278ad765957 # timeout=10
Parsing POMs
Established TCP socket on 53538
[JacocoUnitTestDemo] $ "D:\Program Files\Java\jdk1.8.0_231\/bin/java" -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -cp "D:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven35-agent-1.13.jar;D:\software\maven\boot\plexus-classworlds-2.6.0.jar;D:\software\maven/conf/logging" jenkins.maven3.agent.Maven35Main D:\software\maven "D:\Program Files (x86)\Jenkins\war\WEB-INF\lib\remoting-3.27.jar" "D:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven35-interceptor-1.13.jar" "D:\Program Files (x86)\Jenkins\plugins\maven-plugin\WEB-INF\lib\maven3-interceptor-commons-1.13.jar" 53538
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\pom.xml clean test
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.example:jacocodemo >-----------------------
[INFO] Building jacocodemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ jacocodemo ---
[INFO] Deleting D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target
[INFO] 
[INFO] --- jacoco-maven-plugin:0.8.3:prepare-agent (jacoco-initialize) @ jacocodemo ---
[INFO] argLine set to "-javaagent:D:\\software\\maven\\repo\\m2\\org\\jacoco\\org.jacoco.agent\\0.8.3\\org.jacoco.agent-0.8.3-runtime.jar=destfile=D:\\Program Files (x86)\\Jenkins\\workspace\\JacocoUnitTestDemo\\target\\coverage\\jacoco-unit.exec"
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ jacocodemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ jacocodemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target\classes
[INFO] /D:/Program Files (x86)/Jenkins/workspace/JacocoUnitTestDemo/src/main/java/com/example/controller/MathController.java: D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\src\main\java\com\example\controller\MathController.java浣跨敤浜嗘湭缁忔鏌ユ垨涓嶅畨鍏ㄧ殑鎿嶄綔銆�
[INFO] /D:/Program Files (x86)/Jenkins/workspace/JacocoUnitTestDemo/src/main/java/com/example/controller/MathController.java: 鏈夊叧璇︾粏淇℃伅, 璇蜂娇鐢� -Xlint:unchecked 閲嶆柊缂栬瘧銆�
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ jacocodemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ jacocodemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ jacocodemo ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.service.MathServiceTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.08 s - in com.example.service.MathServiceTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[JENKINS] Recording test results
[INFO] 
[INFO] --- jacoco-maven-plugin:0.8.3:report (jacoco-site) @ jacocodemo ---
[INFO] Loading execution data file D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target\coverage\jacoco-unit.exec
[INFO] Analyzed bundle 'jacocodemo' with 6 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.007 s
[INFO] Finished at: 2020-02-28T16:51:22+08:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\pom.xml to com.example/jacocodemo/0.0.1-SNAPSHOT/jacocodemo-0.0.1-SNAPSHOT.pom
channel stopped
[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/target/coverage/**.exec;**/target/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/target/coverage/**.exec: 1
[JaCoCo plugin] Saving matched execfiles:  D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target\coverage\jacoco-unit.exec
[JaCoCo plugin] Saving matched class directories for class-pattern: **/target/classes: 
[JaCoCo plugin]  - D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\target\classes 6 files
[JaCoCo plugin] Saving matched source directories for source-pattern: **/src/main/java: 
[JaCoCo plugin] Source Inclusions: **/*.java,**/*.groovy,**/*.kt,**/*.kts
[JaCoCo plugin] Source Exclusions: 
[JaCoCo plugin] - D:\Program Files (x86)\Jenkins\workspace\JacocoUnitTestDemo\src\main\java 6 files
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: []
[JaCoCo plugin] exclusions: []
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: 17, method: 4, line: 3, branch: 0, instruction: 2, complexity: 4
Finished: SUCCESS
View Code

 

参考

[1] 使用jenkins的jacoco插件快速实现统计单元测试覆盖率

posted @ 2020-02-28 17:38  明-Ming  阅读(1793)  评论(0编辑  收藏  举报