keyfil/教程/工具/20250303-Jenkins整合Jacoco实现覆盖率报告.md
liuxiaohua 3bfea7cda3
Some checks failed
Publish to Confluence / confluence (push) Failing after 1m15s
[2025-05-07] 添加端口转发文档
2025-05-07 11:33:02 +08:00

5.8 KiB
Raw Blame History

Jenkins整合Jacoco实现覆盖率报告

前置说明

Maven添加Jacoco插件

方法一、更新parent版本

  • 项目中父依赖为:qifu-saas-parent
  • 更新 qifu-saas-parent 版本 <version>[1.0.14,)</version>。开发测试 <version>[1.0.14-SNAPSHOT,)</version>

方法二、直接添加插件

  • pom.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安装插件

Jenkins流水线配置

自由风格项目Freestyle Project

  • 添加构建后操作Post Step
  • 增量对比:
  • 报告采集展示

流水线Pipeline

  • 编写Jenkinsfile
  • 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依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

编写单元测试

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插件

参考