美文网首页
gradle项目集成jacoco跑测试类覆盖率

gradle项目集成jacoco跑测试类覆盖率

作者: 洪兴掌管一代 | 来源:发表于2021-12-12 12:09 被阅读0次

    本文只讲述主要步骤以及踩过的坑。

    本地生成覆盖率文件:

    项目的build.gradle文件添加如下内容,如果一个项目下有多个子项目,则每个项目的build.gradle文件都要添加。

    这里有两个坑:

    1.jacoco test report任务需要依赖test任务生成的jacoco.exec文件,另外,当前项目必须要有测试类,才会跑出覆盖率文件,否则找不到覆盖率文件夹。

    2.excludedSourcesPattern 中配置的不扫描覆盖率的包,每一个pattern要放在一对单引号中,pattern之间要用逗号分隔。这个不同于jenkins中的pattern配置。

    test{

        useJUnitPlatform()

    ignoreFailures =true

        jacoco{

            destinationFile = file("$buildDir/jacoco/jacoco.exec")

    classDumpDir = file("$buildDir/jacoco/classpathdumps")

    }

    }

    jacoco{

        toolVersion ="0.8.7"

    }

    def excludedSourcesPattern = ['**/seed/**','**/index/**','**/openapi*/**','**/model/**']

    jacocoTestReport{

        dependsOn test

    afterEvaluate{

            classDirectories.setFrom(files(classDirectories.files.collect{

                fileTree(dir: it,excludes:excludedSourcesPattern)

    }))

    }

        sourceSets sourceSets.main

    executionData.setFrom fileTree(buildDir).include("/jacoco/*.exec")

    reports{

            html.enabled =true

            csv.enabled =false

        }

    }

    jenkins上跑jacoco task生成覆盖率:

    项目根目录下创建jenkins脚本文件,我的起名Jenkinsfile_junit.build, exclusion pattern在一个单引号对中,这个与本地不一样。提交到git上后 jenkins上建一个

    #!groovy

    // Define Jenkins Job configurations and required parameters

    properties([buildDiscarder(logRotator(artifactNumToKeepStr:'10',numToKeepStr:'10')),

    parameters([

    string(name:'EXEC_PATTERN',defaultValue:'**/jacoco.exec',description:'Path to the exec files generated by JaCoCo. Ant path wildcards are understood. (e.g.: **/target/**.exec, **/jacoco.exec)',trim:true),

    string(name:'CLASS_PATTERN',defaultValue:'**/classes/java',description:'Path to the Java class directories that should be included in coverage reporting. Ant path wildcards are understood. (e.g.: **/target/classDir, **/classes)',trim:true),

    string(name:'INCLUSION_PATTERN',defaultValue:'',description:'Path to the Java class files that should be included in coverage reporting. Ant path wildcards are understood. (e.g.: **/*.class)',trim:true),

    string(name:'EXCLUSION_PATTERN',defaultValue:'**/seed/**,**/index/**,**/openapi*/**,**/model/**',description:'Path to the Java class files that should be excluded from coverage reporting. Ant path wildcards are understood. (e.g.: **/*Test*.class, **/test/**.class)',trim:true),

    ])

    ])

    string execPattern = params.EXEC_PATTERN

    string classPattern = params.CLASS_PATTERN

    string inclusionPattern = params.INCLUSION_PATTERN

    string exclusionPattern = params.EXCLUSION_PATTERN

    pipeline{

        agent{

            node{

                label'terraform-0.11.13'

            }

    }

        stages{

            stage('Build Gradle'){

                steps{

                    sh'./gradlew clean build'

                }

    }

            stage('Coverage Report'){

                steps{

                    jacoco(

    execPattern:"${execPattern}",

    classPattern:"${classPattern}",

    inclusionPattern:"${inclusionPattern}",

    exclusionPattern:"${exclusionPattern}",

    changeBuildStatus:true,

    runAlways:true,

    )}}}}

    提交到git上后 jenkins上建一个,multi-branch pipeline. 配置相应的git服务器地址以及相应的branch filter

    注意script path中的文件名字与工程目录下创建的文件名一致。

    选择想跑的分支名字,点击后,初次进入时没有build with parameters,需要点击build now后再刷新页面,就能看见,然后点击后就能看到设置的default value.

    Feel free to contact me.

    相关文章

      网友评论

          本文标题:gradle项目集成jacoco跑测试类覆盖率

          本文链接:https://www.haomeiwen.com/subject/vxrrfrtx.html