java - 使用 Gradle 过滤 JaCoCo 覆盖率报告

标签 java unit-testing gradle code-coverage 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 - Filtering Options (没有答案)

Exclude packages from Jacoco report using Sonarrunner and Gradle (不使用 )

JaCoCo - exclude JSP from report (它似乎适用于 ,我正在使用 )

Maven Jacoco Configuration - Exclude classes/packages from report not working (它似乎适用于 ,我正在使用 )

JaCoCo gradle plugin exclude (无法让它工作)

Gradle Jacoco - coverage reports includes classes excluded in configuration (看起来很接近,它使用了 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"]
        }
    }
}

问题:

生成 时如何排除某些包和类覆盖报告?

最佳答案

感谢,Yannick Welsch:

在谷歌搜索、阅读 Gradle 文档并浏览旧的 StackOverflow 帖子后,我在官方 上找到了这个答案论坛!

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

来源: https://issues.gradle.org/browse/GRADLE-2955

对于旧的 gradle 版本 < 5.x 可能需要使用 classDirectories = files(classDirectories.files.collect { 而不是 classDirectories.setFrom

我的 build.gradle Java/Groovy 项目的解决方案:

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/**'])
        })
    }
}

如您所见,我成功地向 exclude: 添加了更多内容,以便过滤一些包。

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

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/**'])
        })
    }
}

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

关于java - 使用 Gradle 过滤 JaCoCo 覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29887805/

相关文章:

java - 单击按钮后如何在面板中显示信息?

java - 如何在 JUnit 5 中使用少量枚举创建参数化测试?

java - 在java中,在源代码和测试之间共享资源的推荐方法是什么?

java - Netbeans、Gradle 和 Jogl,崩溃

Angular 抛出错误 "Couldn' t 跟随符号链接(symbolic link)”

java - EC2实例上的原子时间和系统时间差

java - 如何在Java中更好地处理HashMap中的数据?

java - 了解 E 类抛出的表达式

通过子类化 TestCase 进行 Python 参数化单元测试

android - 将字节码转换为 dex 时出错 : Cause: com. android.dex.DexException: