首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用jacocoTestCoverageVerification的安卓检查代码覆盖率阈值

使用jacocoTestCoverageVerification的安卓检查代码覆盖率阈值
EN

Stack Overflow用户
提问于 2017-05-02 19:13:22
回答 2查看 1.3K关注 0票数 3

我想要的:

我想在Android gradle中匹配代码覆盖率阈值到最小值,比如60%等。

我尝试过的东西

stack overflow question i have looked into

jacoco plugin gradle

我正面临的问题

我的Gradle文件是这样写的:

代码语言:javascript
复制
apply plugin: 'com.android.application'

apply plugin: 'jacoco'


jacoco {
    toolVersion = "0.7.6.201602180812"

    reportsDir = file("$buildDir/customJacocoReportDir")
}

def coverageSourceDirs = [
        'src/main/java',
]

jacocoTestReport {

reports {

        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}



jacocoTestCoverageVerification {

    violationRules {
        rule {
            limit {
                minimum = 0.5
            }
        }

        rule {
            enabled = false
            element = 'CLASS'
            includes = ['org.gradle.*']

            limit {
                counter = 'LINE'
                value = 'TOTALCOUNT'
                maximum = 0.3
            }
        }
    }
}

现在,当我同步我的gradle文件时,我遇到了以下错误

在类型为org.gradle.api.Project的项目':app‘上找不到参数build_5t0t9b9hth6zfihsyl5q2obv8$_run_closure2@41a69b20的方法jacocoTestReport()。

如果我注释jacocoTestReport任务,那么

在类型为org.gradle.api.Project的项目':app‘上找不到参数build_5t0t9b9hth6zfihsyl5q2obv8$_run_closure2@3da790a8的方法jacocoTestCoverageVerification()。

我不能理解这里到底发生了什么。为什么jacocoTestCoverageVerification方法不在插件中。我做错了什么。

是不是gradle从android插件中选择了jacoco插件?

我已经尝试过提到jacocoTestCoverageVerification版本到0.6.3,正如文档中提到的那样,have方法是在这个版本之上编写的。

如果有人能解决这个问题,那将是非常有帮助的。

如果需要任何其他信息,请让我知道。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2019-03-04 09:00:19

这个问题一直困扰着我好几次,每次关键字android jacocoTestCoverageVerification将我带到这个页面,但没有得到任何答案。最后,我成功地让jacoco工作了,我想在这里分享我的解决方案。

gradle找不到jacocoTestCoverageVerification和jacocoTestReport的原因是For projects that also apply the Java Plugin, the JaCoCo plugin automatically adds the following tasks jacocoTestReport and jacocoTestCoverageVerification。这意味着对于没有应用java插件的项目,它不会添加 jacocoTestReport和jacocoTestCoverageVerification。

所以我们必须自己添加它们。

点击链接:Code Coverage for Android Testing,我们可以添加任务jacocoTestReport。

一样的方式,我们也可以添加任务jacocoTestCoverageVerification。

完整函数如下所示

代码语言:javascript
复制
// https://engineering.rallyhealth.com/android/code-coverage/testing/2018/06/04/android-code-coverage.html
ext.enableJacoco = { Project project, String variant ->
    project.plugins.apply('jacoco')

    final capVariant = variant.capitalize()

    StringBuilder folderSb = new StringBuilder(variant.length() + 1)
    for (int i = 0; i < variant.length(); i++) {
        char c = variant.charAt(i)
        if (Character.isUpperCase(c)) {
            folderSb.append('/')
            folderSb.append(Character.toLowerCase(c))
        } else {
            folderSb.append(c)
        }
    }
    final folder = folderSb.toString()


    project.android {
        buildTypes {
            debug {
                testCoverageEnabled true
            }
        }

        testOptions {
            unitTests.all {
                jacoco {
                    //You may encounter an issue while getting test coverage for Robolectric tests.
                    //To include Robolectric tests in the Jacoco report, one will need to set the includeNolocationClasses flag to true.
                    // This can no longer be configured using the android DSL block, thus we search all tasks of Test type and enable it

                    includeNoLocationClasses = true
                }
            }
        }
        jacoco {
            version = '0.8.1'
        }
    }

    project.jacoco {
        toolVersion = '0.8.1'
    }

    project.tasks.create(
            name: 'jacocoTestCoverageVerification',
            type: JacocoCoverageVerification,
            dependsOn: ["test${capVariant}UnitTest",
                        "create${capVariant}CoverageReport"
            ]
    ) {
        onlyIf = {
            true
        }

        violationRules {
            rule {
                limit {
                    minimum = 0.5
                }
            }

            rule {
                enabled = false
                element = 'CLASS'
                includes = ['org.gradle.*']

                limit {
                    counter = 'LINE'
                    value = 'TOTALCOUNT'
                    maximum = 0.3
                }
            }
        }
        def coverageSourceDirs = [
                "src/main/java",
                "src/main/kotlin"
        ]

        def fileFilter = [
                '**/R.class',
                '**/R$*.class',
                '**/*$ViewInjector*.*',
                '**/*$ViewBinder*.*',
                '**/BuildConfig.*',
                '**/*_MembersInjector.class',
                '**/Dagger*Component.class',
                '**/Dagger*Component$Builder.class',
                '**/*Module_*Factory.class',
                '**/*_MembersInjector.class',
                '**/Dagger*Subcomponent*.class',
                '**/*Subcomponent$Builder.class',
                '**/Manifest*.*'
        ]

        def javaClasses = fileTree(
                dir: "${project.buildDir}/intermediates/javac/$folder",
                excludes: fileFilter
        )
        def kotlinClasses = fileTree(
                dir: "${project.buildDir}/tmp/kotlin-classes/$variant",
                excludes: fileFilter
        )

        group = "Reporting"
        description = "Applying Jacoco coverage verification for the ${project.name} with the " +
                "$variant variant."
        classDirectories = files([javaClasses], [kotlinClasses])
        additionalSourceDirs = files(coverageSourceDirs)
        sourceDirectories = files(coverageSourceDirs)
        executionData = fileTree(dir: "${project.buildDir}", includes: [
                "jacoco/testDebugUnitTest.exec",
                "outputs/code_coverage/debugAndroidTest/connected/*.ec",
                "outputs/code_coverage/connected/*.ec" //Check this path or update to relevant path
        ])
    }
    project.tasks.create(
            name: 'jacocoTestReport',
            type: JacocoReport,
            dependsOn: ["test${capVariant}UnitTest",
                        "create${capVariant}CoverageReport"
            ]
    ) {
        def coverageSourceDirs = [
                "src/main/java",
                "src/main/kotlin"
        ]

        def fileFilter = [
                '**/R.class',
                '**/R$*.class',
                '**/*$ViewInjector*.*',
                '**/*$ViewBinder*.*',
                '**/BuildConfig.*',
                '**/*_MembersInjector.class',
                '**/Dagger*Component.class',
                '**/Dagger*Component$Builder.class',
                '**/*Module_*Factory.class',
                '**/*_MembersInjector.class',
                '**/Dagger*Subcomponent*.class',
                '**/*Subcomponent$Builder.class',
                '**/Manifest*.*'
        ]

        def javaClasses = fileTree(
                dir: "${project.buildDir}/intermediates/javac/$folder",
                excludes: fileFilter
        )
        def kotlinClasses = fileTree(
                dir: "${project.buildDir}/tmp/kotlin-classes/$variant",
                excludes: fileFilter
        )

        group = "Reporting"
        description = "Generate Jacoco coverage reports for the ${project.name} with the " +
                "$variant variant."
        classDirectories = files([javaClasses], [kotlinClasses])
        additionalSourceDirs = files(coverageSourceDirs)
        sourceDirectories = files(coverageSourceDirs)
        executionData = fileTree(dir: "${project.buildDir}", includes: [
                "jacoco/testDebugUnitTest.exec",
                "outputs/code_coverage/debugAndroidTest/connected/*.ec",
                "outputs/code_coverage/connected/*.ec" //Check this path or update to relevant path
        ])
        onlyIf = {
            true
        }
        println project
        println "current $project buildDir: $buildDir project buildDir: ${project.buildDir}"
        System.out.flush()
        reports {
            html.enabled = true
            html.destination file("reporting/jacocohtml")
        }
    }


}

Gist version

票数 4
EN

Stack Overflow用户

发布于 2017-05-29 20:14:52

首先检查你的gradle verison:对于也应用了Java插件的项目,JaCoCo插件会自动添加以下任务:

Gradle3.3 (jacocoTestReport任务) https://docs.gradle.org/3.3/userguide/jacoco_plugin.html#sec:jacoco_tasks

Gradle3.5 (jacocoTestReport,jacocoTestCoverageVerification任务) https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_tasks

也许对于早期版本的gradle,您需要添加jacoco依赖项:

代码语言:javascript
复制
...
dependencies {
сlasspath (
[group: 'org.jacoco', name: 'org.jacoco.agent', version: version: '0.7.7.201606060606'],
[group: 'org.jacoco', name: 'org.jacoco.ant', version: version: '0.7.7.201606060606']
)}
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43736464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档