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

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

2020-02-28

目录 

0 整体思路
1 安装版本
2 全局工具配置
3 Jenkins创建JacocoIntegrateTestDemo项目
  3.1 配置源码管理
  3.2 配置构建
  3.3 配置构建后操作
4 执行JacocoIntegrateTestDemo项目,查看代码覆盖率

 

0 整体思路


  返回

  1. 启动被测系统  
    java -javaagent:D:/software/jacoco/lib/jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=10.8.116.81,append=true -jar D:/Code/jacocodemo/target/jacocodemo.jar --server.port=9999
  2. 执行测试脚本(可通过postman执行,或自动化测试脚本)
  3. 通过jenkins Job收集展示覆盖率报告
    1. 拉取codenet上的JacocoDemo的代码(ant的build.xml也放在源代码根目录下)
    2. 编译源代码生成class文件(jacoco生成report需要)
    3. ant dump拉取覆盖率信息jacoco.exec
    4. 配置jacoco coverage report展示覆盖率信息

1 安装版本

jenkins-2.150.3

jenkins插件

jenkins -> 系统管理 -> 插件管理

  • git
  • jacoco
  • maven

图1 插件git、jacoco、maven

 2 全局工具配置


 返回

jenkins -> 系统管理 -> 全局工具配置

以便在window批处理中使用

图2 jdk git maven全局配置

3 Jenkins创建JacocoIntegrateTestDemo项目


 返回

图3 新建自由风格的软件项目

3.1 配置源码管理

  • Repository Url: git@e.coding.net:Ming8006/jacocodemo.git
  • Credentials: ming

图4 配置源码管理

上图4源码管理配置中,点击 立即构建 JacocoIntegrateTestDemo项目 时,会拉取 git@e.coding.net:Ming8006/jacocodemo.git 到jenkins部署的机器目录下: D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo 

 在上图4中,源码管理的凭据是 ming ,凭据设置如下

Jenkins->凭据->系统->添加域

图5 添加域e.coding.net

图6 在域中添加ssh凭据

在上图6中,添加ssh的private key,jekins通过ssh 拉取代码 git@e.coding.net:Ming8006/jacocodemo.git ,需在coding.net上添加公钥,如下图

图7 电脑生成的公钥私钥

图8 添加公钥到codenet个人账户

3.2 配置构建

 

图9 构建配置

在上图9中,做2步构建操作:

  • 添加window批处理命令,在jenkins workspace目录项目JacocoIntegrateTestDemo下执行mun clean compile生成class文件
  • 安装build.xml的配置获取覆盖率信息jacoco.exec

注意:在获取覆盖率信息前,先postman调用JacocoDemo程序的接口

build.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

图 jenkins workspace下JacocoItegrateTestDemo目录结构

 3.3 配置构建后操作

图10 构建后操作

如图10中,配置Jacoco Coverage Report

4 执行JacocoIntegrateTestDemo项目,查看代码覆盖率


 返回

 图10 查看代码覆盖率

 Jenkins控制台输出

在 master 上构建 在工作空间 D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo 中
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
[JacocoIntegrateTestDemo] $ cmd /c call C:\Windows\TEMP\jenkins2606191743471144903.bat

D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo>d:

D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo>cd D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo 

D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo>mvn clean compile 
[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\JacocoIntegrateTestDemo\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\\JacocoIntegrateTestDemo\\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\JacocoIntegrateTestDemo\target\classes
[INFO] /D:/Program Files (x86)/Jenkins/workspace/JacocoIntegrateTestDemo/src/main/java/com/example/controller/MathController.java: D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo\src\main\java\com\example\controller\MathController.java使用了未经检查或不安全的操作。
[INFO] /D:/Program Files (x86)/Jenkins/workspace/JacocoIntegrateTestDemo/src/main/java/com/example/controller/MathController.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.773 s
[INFO] Finished at: 2020-02-28T14:13:11+08:00
[INFO] ------------------------------------------------------------------------
[JacocoIntegrateTestDemo] $ cmd.exe /C "ant.bat -file build.xml dump && exit %%ERRORLEVEL%%"
Buildfile: D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo\build.xml

dump:
[jacoco:dump] Connecting to /10.8.116.81:6300
[jacoco:dump] Dumping execution data to D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo\target\jacoco.exec

BUILD SUCCESSFUL
Total time: 0 seconds
[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/target/**.exec;**/target/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/target/**.exec: 1
[JaCoCo plugin] Saving matched execfiles:  D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo\target\jacoco.exec
[JaCoCo plugin] Saving matched class directories for class-pattern: **/target/classes: 
[JaCoCo plugin]  - D:\Program Files (x86)\Jenkins\workspace\JacocoIntegrateTestDemo\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\JacocoIntegrateTestDemo\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: 100, method: 79, line: 72, branch: 63, instruction: 76, complexity: 71
Finished: SUCCESS
View Code

 

 参考

[1] jenkins+jacoco+ant自动化代码和应用服务代码分离场景获取远程服务的覆盖率

[2] Jenkins + Jacoco 持续集成代码覆盖率 

[3] Selenium Web 自动化 - 项目持续集成

posted @ 2020-02-28 16:11  明-Ming  阅读(7419)  评论(0编辑  收藏  举报