Jenkins集成jacoco做单元测试报告

1.在jenkins里下载jaCoCo plugin插件,重启生效

2.修改配置文件

2.1pom.xml配置

<1>dependency依赖部分

复制代码
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.1</version>
    <scope>test</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
</dependency>
复制代码

<2>plugin插件部分

复制代码
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <skipTests>false</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.8</version>
                <configuration>
                    <skip>false</skip>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <configuration>
                            <outputDirectory>${basedir}/target/coverage-reports</outputDirectory>
                        </configuration>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
复制代码

2.2Jenkinsfile配置

复制代码
//单元测试阶段
        stage("unit-test") {
            when {
                expression { ENV =~ 'CI.*' }
            }
            steps {
                echo "自动化测试,单元测试"
                sh 'shell/unit_test.sh'
            }
                //3.收集html测试报告                   
            post {  
                always {
                    script { 
                        //allowEmptyResults: true允许空结果即没有测试报告输出,skipPublishingChecks: true跳过github发布检查
                        if (env.moudle_name == '.') {
                            junit allowEmptyResults: true, skipPublishingChecks: true, testResults: "**/target/surefire-reports/*.xml"
                            jacoco()
                            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'target', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: ''])
                         }
                        else {
                            junit allowEmptyResults: true, skipPublishingChecks: true, testResults: "**/${params.moudle_name}/target/surefire-reports/*.xml"
                            jacoco(classPattern: '**/${params.moudle_name}/target/classes', execPattern: '${params.moudle_name}/target/**.exec', sourcePattern: '**/${params.moudle_name}/src/main/java')
                            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '${params.moudle_name}/target', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: ''])
                        }

                    }
                }
            }
        }


jacoco的参数
jacoco(

 //代码覆盖率统计文件位置,Ant风格路径表达式


        execPattern: ' target/**/* . exec', 


        // classes 文件位置,Ant风格路径表达式


        classPattern: ' target/classes',


        //源码文件位置,Ant风格路径表达式


        sourcePattern: ' src/main/java',


        //排除分析的位置,Ant风格路径表达式


        exclusionPattern: 'src/test*',


        //是否禁用每行覆盖率的源文件显示


        skipCopyOfSrcFiles: false,


        //如果为true,则对各维度的覆盖率进行比较。如果任何一个维度的当前覆盖率小于最小覆盖率阈值,则构建状态为失败


        //如果当前覆盖率在最大阈值和最小阈值之间,则当前构建状态为不稳定;如果当前覆盖率大于最大阈值,则构建成功


        changeBuildStatus: true,


        //字节码指令覆盖率


        minimumInstructionCoverage: ' 30' , maximumInstructionCoverage: '70',


        //行覆盖率


        minimumLineCoverage: ' 30' , maximumLineCoverage: '70',


        //圈复杂度覆盖率


        minimumComplexityCoverage: ' 30' ,maximumComplexityCoverage:'70', 


        //方法覆盖率


        minimumMethodCoverage: ' 30' , maximumMethodCoverage:'70',


        //类覆盖率


        minimumClassCoverage: ' 30', maximumClassCoverage: ' 70',


        //分支覆盖率


        minimumBranchCoverage: ' 30' ,maximumBranchCoverage:'70',


        //如果为true,则只有所有维度的覆盖率变化量的绝对值小于相应的变化量阈值时,构建结果才为成功


        build0verBuild: true,


        //以下是各个维度覆盖率的变化量阈值


        deltaInstructionCoverage: ' 80', deltaLineCoverage: '80',


        deltaMethodCoverage: ' 80 ' ,deltaClassCoverage: '80',


        deltaComplexityCoverage: ' 80', deltaBranchCoverage: '80 '


)
复制代码

 

posted @   潇潇暮鱼鱼  阅读(620)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示