developer tip

Gradle로 JaCoCo 커버리지 보고서 필터링

optionbox 2020. 9. 22. 08:04
반응형

Gradle로 JaCoCo 커버리지 보고서 필터링


문제:

하는 프로젝트가 특정 클래스 및 / 또는 패키지를 필터링 할 수 있기를 원합니다.

관련 문서 :

다음 문서를 읽었습니다.

공식 사이트 : http://www.eclemma.org/jacoco/index.html

공식 대한 문서 : https://gradle.org/docs/current/userguide/jacoco_plugin.html

공식 Github 문제, 적용 범위 작업 : https://github.com/jacoco/jacoco/wiki/FilteringOptions https://github.com/jacoco/jacoco/issues/14

관련 StackOverflow 링크 :

JaCoCo 및 Gradle-필터링 옵션 (답변 없음)

Sonarrunner 및 Gradle을 사용하여 Jacoco 보고서에서 패키지 제외 ( 사용하지 않음 )

JaCoCo-보고서에서 JSP 제외 ( 에서 작동하는 것 같습니다. 사용 )

메이븐 Jacoco 구성 - 클래스 / 보고서에서 패키지가 작동하지 제외 (그것은을 위해 작동하는 것 같다 , 내가 사용하고 )

JaCoCo gradle 플러그인 제외 (이 작업을 수행 할 수 없음)

Gradle Jacoco-커버리지 보고서에는 구성에서 제외 된 클래스가 포함되어 있습니다 (매우 가깝게 보였고 사용 doFirst했으며 나를 위해 작동하지 않았습니다)

내가 시도한 것의 예 :

apply plugin: 'java'
apply plugin: 'jacoco'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
}

repositories {
    jcenter()
}

jacocoTestReport {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    test {
        jacoco {
            destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
            classDumpFile = file("$buildDir/jacoco/classpathdumps")
            excludes = ["projecteuler/**"] // <-- does not work
            // excludes = ["projecteuler"]
        }
    }
}

질문:

How can I exclude certain packages and classes when generating the coverage reports?


Thanks to, Yannick Welsch:

After searching Google, reading the Gradle docs and going through older StackOverflow posts, I found this answer on the Official forums!

jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: 'com/blah/**')
        })
    }
}

Source: https://issues.gradle.org/browse/GRADLE-2955

Solution to my build.gradle for Java/Groovy projects:

apply plugin: 'java'
apply plugin: 'jacoco'

jacocoTestReport {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['codeeval/**',
                              'crackingthecode/part3knowledgebased/**',
                              '**/Chapter7ObjectOrientedDesign**',
                              '**/Chapter11Testing**',
                              '**/Chapter12SystemDesignAndMemoryLimits**',
                              'projecteuler/**'])
        })
    }
}

As you can see, I was successfully able to add more to exclude: in order to filter a few packages.

Source: https://github.com/jaredsburrows/CS-Interview-Questions/blob/master/build.gradle

Custom tasks for other projects such as Android:

apply plugin: 'jacoco'

task jacocoReport(type: JacocoReport) {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['codeeval/**',
                              'crackingthecode/part3knowledgebased/**',
                              '**/Chapter7ObjectOrientedDesign**',
                              '**/Chapter11Testing**',
                              '**/Chapter12SystemDesignAndMemoryLimits**',
                              'projecteuler/**'])
        })
    }
}

Source: https://github.com/jaredsburrows/android-gradle-java-app-template/blob/master/gradle/quality.gradle#L59


For Gradle version 5.0 and later, the classDirectories = files(...) gives a deprecation warning. This is the non deprecated way of excluding classes:

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: 'com/exclude/**')
        }))
    }
}

for me, it's fine working with

test {
  jacoco {
    excludes += ['codeeval/**',
                 'crackingthecode/part3knowledgebased/**',
                 '**/Chapter7ObjectOrientedDesign**',
                 '**/Chapter11Testing**',
                 '**/Chapter12SystemDesignAndMemoryLimits**',
                 'projecteuler/**']
  }
}

as stated out in documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html#N16E62 and initally asked so the answer is:

so if you ask me: it's not a question of

excludes = ["projecteuler/**"]

or

excludes += ["projecteuler/**"]

but

excludes = ["**/projecteuler/**"]

to exclude a package *.projecteuler.*

and test {} on project level, not nested in jacocoTestReport


Here is a solution for this problem in ANT. This can be adapted to gradle by adding the following under the jacocoTestReport task. Although this isn't really documented by jacoco, it seems like the only way to filter the test results for now.

afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it, exclude: 'excluded/files/**')
    })
}

This has been out for a while, but I just ran across this. I was struggling with all the exclusions needed. I found it was something much more simple for me. If you follow the Maven project layout style /src/main/java and /src/test/java, you simply need to put buildDir/classes/main in your classDirectories config like so:

afterEvaluate {
    jacocoTestReport {
        def coverageSourceDirs = ['src/main/java']
        reports {
            xml.enabled false
            csv.enabled false
            html.destination "${buildDir}/reports/jacocoHtml"
        }
        sourceDirectories = files(coverageSourceDirs)
        classDirectories = fileTree(
                dir: "${project.buildDir}/classes/main",
                excludes: [
                      //whatever here like JavaConfig etc. in /src/main/java
                     ]
        )
    }
}

The code below excludes classes from coverage verification as well:

jacocoTestCoverageVerification {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: "${project.buildDir}/classes/main",
                    exclude: ['**/packagename/**'])
        })
    }
}

add below config in gradle.properties file

coverageExcludeClasses=["com.example.package.elasticsearch.*", "com.example.package2.*",]

참고 URL : https://stackoverflow.com/questions/29887805/filter-jacoco-coverage-reports-with-gradle

반응형