使用 Gradle 过滤 JaCoCo 覆盖率报告 [英] Filter JaCoCo coverage reports with Gradle

查看:49
本文介绍了使用 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/FilteringOptionshttps://github.com/jacoco/jacoco/issues/14

相关 StackOverflow 链接:

JaCoCo &Gradle - 过滤选项(无答案)

使用 Sonarrunner 和 Gradle 从 Jacoco 报告中排除包(不使用 )

JaCoCo - 从报告中排除 JSP(它似乎适用于 ,我正在使用 )

Maven Jacoco 配置 - 从报告不起作用(它似乎适用于 ,我正在使用 )

JaCoCo gradle 插件排除(无法让它工作)

Gradle Jacoco - 覆盖率报告包括配置中排除的类(看起来很接近,它使用了doFirst,对我不起作用)

我尝试过的示例:

应用插件:'java'应用插件:'jacoco'构建脚本{存储库{MavenCentral()jcenter()}}存储库{jcenter()}jacocoTestReport {报告{xml {启用 true//工作服插件取决于 xml 格式报告}html {启用真}}测试 {雅可{目标文件 = 文件($buildDir/jacoco/jacocoTest.exec")classDumpFile = 文件($buildDir/jacoco/classpathdumps")excludes = ["projecteuler/**"]//<-- 不起作用//排除 = [projecteuler"]}}}

问题:

如何在生成 报道?

解决方案

感谢,Yannick Welsch:

在 Google 搜索、阅读 Gradle 文档并浏览较旧的 StackOverflow 帖子后,我在官方 论坛!

jacocoTestReport {后评价{classDirectories.setFrom(files(classDirectories.files.collect {文件树(目录:它,排除:'com/blah/**')}))}}

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

对于较旧的 gradle 版本

5.x 可能需要使用classDirectories = files(classDirectories.files.collect { 而不是 classDirectories.setFrom

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

应用插件:'java'应用插件:'jacoco'jacocoTestReport {报告{xml {启用 true//工作服插件取决于 xml 格式报告}html {启用真}}后评价{classDirectories = 文件(classDirectories.files.collect {文件树(目录:它,排除:['codeeval/**','破解代码/part3knowledgebased/**','**/Chapter7ObjectOrientedDesign**','**/Chapter11Testing**','**/Chapter12SystemDesignAndMemoryLimits**','投影仪/**'])})}}

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

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

其他项目(如 Android)的自定义任务:

应用插件:'jacoco'任务 jacocoReport(类型:JacocoReport){报告{xml {启用 true//工作服插件取决于 xml 格式报告}html {启用真}}后评价{classDirectories = 文件(classDirectories.files.collect {文件树(目录:它,排除:['codeeval/**','破解代码/part3knowledgebased/**','**/Chapter7ObjectOrientedDesign**','**/Chapter11Testing**','**/Chapter12SystemDesignAndMemoryLimits**','投影仪/**'])})}}

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

Problem:

I have a project with and I want to be able to filter certain classes and/or packages.

Related Documentation:

I have read the following documentation:

Official site: http://www.eclemma.org/jacoco/index.html

Official docs for : https://gradle.org/docs/current/userguide/jacoco_plugin.html

Official Github issues, working on coverage: https://github.com/jacoco/jacoco/wiki/FilteringOptions https://github.com/jacoco/jacoco/issues/14

Related StackOverflow Links:

JaCoCo & Gradle - Filtering Options (No answer)

Exclude packages from Jacoco report using Sonarrunner and Gradle (Not using )

JaCoCo - exclude JSP from report (It seems to work for , I am using )

Maven Jacoco Configuration - Exclude classes/packages from report not working (It seems to work for , I am using )

JaCoCo gradle plugin exclude (Could not get this to work)

Gradle Jacoco - coverage reports includes classes excluded in configuration (Seems very close, it used doFirst, did not work for me)

Example of what I have tried:

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"]
        }
    }
}

Question:

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.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: 'com/blah/**')
        }))
    }
}

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

For older gradle versions < 5.x may need to use classDirectories = files(classDirectories.files.collect { instead of classDirectories.setFrom

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

这篇关于使用 Gradle 过滤 JaCoCo 覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆