192 lines
5.8 KiB
Markdown
192 lines
5.8 KiB
Markdown
|
<!-- Space: qifu -->
|
|||
|
<!-- Parent: 后端技术&知识&规范 -->
|
|||
|
<!-- Parent: 技术方案 -->
|
|||
|
<!-- Parent: 基建 -->
|
|||
|
<!-- Parent: 04-使用教程 -->
|
|||
|
<!-- Title: 20250303-Jenkins整合Jacoco实现覆盖率报告 -->
|
|||
|
|
|||
|
<!-- Macro: :anchor\((.*)\):
|
|||
|
Template: ac:anchor
|
|||
|
Anchor: ${1} -->
|
|||
|
<!-- Macro: \!\[.*\]\((.+)\)\<\!\-\- width=(.*) \-\-\>
|
|||
|
Template: ac:image
|
|||
|
Url: ${1}
|
|||
|
Width: ${2} -->
|
|||
|
<!-- Macro: \<\!\-\- :toc: \-\-\>
|
|||
|
Template: ac:toc
|
|||
|
Printable: 'false'
|
|||
|
MinLevel: 2
|
|||
|
MaxLevel: 4 -->
|
|||
|
<!-- Include: 杂项/声明文件.md -->
|
|||
|
|
|||
|
<!-- :toc: -->
|
|||
|
|
|||
|
# Jenkins整合Jacoco实现覆盖率报告
|
|||
|
|
|||
|
## 前置说明
|
|||
|
- [Jenkins 安装](https://plugins.jenkins.io/maven-plugin)
|
|||
|
- Jenkins 插件安装
|
|||
|
- [Git plugin][Git plugin]
|
|||
|
- [Git Forensics Plugin][Git Forensics Plugin]
|
|||
|
- [Git Parameter Plug-In][Git Parameter Plug-In]
|
|||
|
- [Coverage Plugin][Coverage Plugin]
|
|||
|
- [Maven Integration plugin][Maven Integration plugin]
|
|||
|
- [Maven](https://plugins.jenkins.io/maven-plugin)
|
|||
|
- [Jacoco](https://www.jacoco.org/)
|
|||
|
|
|||
|
## Maven添加Jacoco插件
|
|||
|
### 方法一、更新parent版本
|
|||
|
- 项目中父依赖为:`qifu-saas-parent`
|
|||
|
- 更新 `qifu-saas-parent` 版本 `<version>[1.0.14,)</version>`。开发测试 `<version>[1.0.14-SNAPSHOT,)</version>`
|
|||
|
|
|||
|
### 方法二、直接添加插件
|
|||
|
- pom.xml 中添加插件
|
|||
|
- ```xml
|
|||
|
<!-- 以下是需要复制的内容,以上是方便查找复制的位置-->
|
|||
|
<plugin>
|
|||
|
<groupId>org.apache.maven.plugins</groupId>
|
|||
|
<artifactId>maven-surefire-plugin</artifactId>
|
|||
|
<version>3.5.2</version>
|
|||
|
<configuration>
|
|||
|
<argLine>${argLine} </argLine>
|
|||
|
<forkCount>4</forkCount>
|
|||
|
<reuseForks>true</reuseForks>
|
|||
|
</configuration>
|
|||
|
</plugin>
|
|||
|
<plugin>
|
|||
|
<groupId>org.jacoco</groupId>
|
|||
|
<artifactId>jacoco-maven-plugin</artifactId>
|
|||
|
<version>0.8.12</version>
|
|||
|
<executions>
|
|||
|
<execution>
|
|||
|
<id>prepare-agent</id>
|
|||
|
<goals>
|
|||
|
<goal>prepare-agent</goal>
|
|||
|
</goals>
|
|||
|
</execution>
|
|||
|
<execution>
|
|||
|
<id>report</id>
|
|||
|
<phase>test</phase>
|
|||
|
<goals>
|
|||
|
<goal>report</goal>
|
|||
|
</goals>
|
|||
|
</execution>
|
|||
|
</executions>
|
|||
|
</plugin>
|
|||
|
```
|
|||
|
|
|||
|
## 集成覆盖率报告
|
|||
|
### Jenkins安装插件
|
|||
|
- [Git plugin][Git plugin]
|
|||
|
- [Git Forensics Plugin][Git Forensics Plugin]
|
|||
|
- [Git Parameter Plug-In][Git Parameter Plug-In]
|
|||
|
- [Coverage Plugin][Coverage Plugin]
|
|||
|
- [Maven Integration plugin][Maven Integration plugin]
|
|||
|
|
|||
|
### Jenkins流水线配置
|
|||
|
- 可以参考:https://github.com/jenkinsci/coverage-plugin?tab=readme-ov-file#usage
|
|||
|
#### 自由风格项目(Freestyle Project)
|
|||
|
- 添加构建后操作(Post Step)
|
|||
|
- 增量对比:
|
|||
|
- 
|
|||
|
- 报告采集展示
|
|||
|
- 
|
|||
|
|
|||
|
#### 流水线(Pipeline)
|
|||
|
- 编写Jenkinsfile
|
|||
|
- ```groovy
|
|||
|
pipeline {
|
|||
|
agent any
|
|||
|
tools {
|
|||
|
maven "MavenInner"
|
|||
|
}
|
|||
|
parameters {
|
|||
|
gitParameter(branch: '', branchFilter: '.*', defaultValue: 'master', name: 'buildBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition')
|
|||
|
}
|
|||
|
environment {
|
|||
|
GIT_URL='ssh://git@git.keyfil.com:9922/qifu-gateway/keycloak-justauth.git'
|
|||
|
}
|
|||
|
stages {
|
|||
|
stage('Checkout') {
|
|||
|
steps {
|
|||
|
echo "正在从 GitLab 拉取分支的代码..."
|
|||
|
git branch: "main", credentialsId: "1", url: "${GIT_URL}"
|
|||
|
}
|
|||
|
}
|
|||
|
stage('Maven Build') {
|
|||
|
steps {
|
|||
|
echo "开始执行 Maven 构建..."
|
|||
|
sh 'mvn clean test jacoco:report -Daliyun=*'
|
|||
|
}
|
|||
|
}
|
|||
|
stage('Generate Coverage Report') {
|
|||
|
steps {
|
|||
|
echo "使用 Git Forensics 统计代码变更..."
|
|||
|
discoverGitReferenceBuild referenceJob: 'Jacoco测试', targetBranch: '$referenceJob'
|
|||
|
recordCoverage qualityGates: [[criticality: 'NOTE', metric: 'MODULE'], [baseline: 'MODIFIED_FILES', criticality: 'NOTE', metric: 'FILE'], [baseline: 'MODIFIED_LINES', criticality: 'NOTE', metric: 'LINE']], tools: [[parser: 'JACOCO']]
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
post {
|
|||
|
success {
|
|||
|
echo "构建成功!"
|
|||
|
}
|
|||
|
failure {
|
|||
|
echo "构建失败,请检查日志。"
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
### 构建测试
|
|||
|
|
|||
|
## 编写单元测试
|
|||
|
### 添加pom依赖
|
|||
|
```xml
|
|||
|
<dependency>
|
|||
|
<groupId>org.springframework.boot</groupId>
|
|||
|
<artifactId>spring-boot-starter-test</artifactId>
|
|||
|
<scope>test</scope>
|
|||
|
</dependency>
|
|||
|
```
|
|||
|
### 编写单元测试
|
|||
|
```java
|
|||
|
package io.github.yanfeiwuji.justauth.social;
|
|||
|
|
|||
|
import org.junit.jupiter.api.BeforeEach;
|
|||
|
import org.junit.jupiter.api.Test;
|
|||
|
|
|||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
|
|
|||
|
class WeworkIdentityProviderFactoryTest {
|
|||
|
|
|||
|
private WeworkIdentityProviderFactory weworkIdentityProviderFactoryUnderTest;
|
|||
|
|
|||
|
@BeforeEach
|
|||
|
void setUp() {
|
|||
|
weworkIdentityProviderFactoryUnderTest = new WeworkIdentityProviderFactory();
|
|||
|
}
|
|||
|
|
|||
|
@Test
|
|||
|
void testGetName() throws Exception {
|
|||
|
assertEquals("企业微信", weworkIdentityProviderFactoryUnderTest.getName());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
## 高级使用
|
|||
|
### Squaretest插件
|
|||
|
- 安装 Squaretest 插件并破解
|
|||
|
- [直接下载](http://confluence.qifu.com/download/attachments/38510668/Squaretest.zip?api=v2)
|
|||
|
- [破解教程参考](https://blog.csdn.net/binbushi/article/details/135942290)
|
|||
|
- 右键 > Generate > Generate test
|
|||
|
|
|||
|
## 参考
|
|||
|
|
|||
|
|
|||
|
[Git plugin]: https://plugins.jenkins.io/git
|
|||
|
[Git Forensics Plugin]: https://plugins.jenkins.io/git-forensics
|
|||
|
[Git Parameter Plug-In]: https://plugins.jenkins.io/git-parameter
|
|||
|
[Coverage Plugin]: https://plugins.jenkins.io/coverage
|
|||
|
[Maven Integration plugin]: https://plugins.jenkins.io/maven-plugin
|