简体   繁体   English

结合androidTest的jacoco覆盖并测试

[英]Combine jacoco coverage from androidTest and test

Since the release of 'com.android.tools.build:gradle:1.1.0' I'm moving most of my java test code from androidTest to the test folder because the JVM tests are a lot faster. 自从'com.android.tools.build:gradle:1.1.0'发布以来,我将大部分java测试代码从androidTesttest文件夹,因为JVM测试速度要快得多。 But I cannot move all tests. 但我不能移动所有测试。 I really need the device tests because of some ContentProvider stuff. 我真的需要设备测试,因为有一些ContentProvider东西。

I've had 100% code coverage before I started migrating. 在开始迁移之前,我已经有100%的代码覆盖率。 When I'm currently running the jacoco code coverage I get 40% for the androidTest folder and 71% for the test folder. 当我正在运行jacoco代码覆盖时,我获得androidTest文件夹的40%和test文件夹的71%。 My code is 100% tested but I have no report proofing this. 我的代码经过100%测试,但我没有报告证明这一点。

Is there a way to combine both reports? 有没有办法将两份报告合并? I found JacocoMerge but couldn't get it to work. 我找到了JacocoMerge,但无法让它工作。

Here is the output of the androidTest folder: build/outputs/reports/coverage/debug/index.html 这是androidTest文件夹的输出: build/outputs/reports/coverage/debug/index.html

And here the output of the test folder build/reports/jacoco/generateJacocoTestReports/html/index.html generated with this gradle task: 这里是使用此gradle任务生成的test文件夹build/reports/jacoco/generateJacocoTestReports/html/index.html输出:

def coverageSourceDirs = [
        '../library/src/main/java'
]

task generateJacocoTestReports(type: JacocoReport, dependsOn: "test") {
    group = "Reporting"
    description = 'Generate Jacoco Robolectric unit test coverage reports'

    classDirectories = fileTree(
            dir: '../library/build/intermediates/classes/debug',
            excludes: ['**//*R.class',
                       '**//*R$*.class',
                       '***/*//*$ViewInjector*.*',
                       '**//*BuildConfig.*',
                       '**//*Manifest*.*']
            )
    sourceDirectories = files(coverageSourceDirs)
    additionalSourceDirs = files(coverageSourceDirs)
    executionData = files('../library/build/jacoco/testDebug.exec')
}

不确定你是否仍然需要这个,但我最近发布了Gradle插件,它可以帮助你: https//github.com/paveldudka/JacocoEverywhere

There is also the gradle plugin https://github.com/palantir/gradle-jacoco-coverage that acording to docs can do the job, too. 还有gradle插件https://github.com/palantir/gradle-jacoco-coverage ,根据文档也可以完成这项工作。

I haven-t tried it for one submodul with two different test-parts but it works well for merging the testparts of to two submoduls. 我没有尝试过一个具有两个不同测试部件的子模块,但它适用于将两个子模块的测试部件合并。

See Gradle jacoco coverage report with more than one submodule(s)? 请参阅Gradle jacoco覆盖率报告,其中包含多个子模块? for details 详情

In case you use Jenkins with the JaCoCo plugin you can just configure all jacoco.exec and emma.ec files in "Path to exec files" to have a combined coverage reported. 如果您将Jenkins与JaCoCo插件一起使用,您只需配置“执行文件路径”中的所有jacoco.exec和emma.ec文件即可报告组合覆盖率。

connectedAndroidTest will result in emma.ec files somewhere in "outputs" by default. connectedAndroidTest默认会在“输出”中的某处产生emma.ec文件。

JacocoMerge task can be used to merge 2 or more jacoco execution data. JacocoMerge任务可用于合并2个或更多jacoco执行数据。

Below task can be added to the root gradle file and on successful execution of this task, merged execution data can be found under root build directory. 可以将以下任务添加到根gradle文件中,并且在成功执行此任务时,可以在根构建目录下找到合并的执行数据。 ( build/jacoco/mergeJacocoReport.exec ) build / jacoco / mergeJacocoReport.exec

evaluationDependsOnChildren()
//Missing this might be a problem in fetching JacocoReport tasks from sub-modules.

 task mergeJacocoReport(type: org.gradle.testing.jacoco.tasks.JacocoMerge) {
    group "Jacoco Report"
    description "Merge Jacoco Code Coverage Report"

    def executionFiles = fileTree("$rootProject.rootDir", {
        includes = ['**/*.exec']
    })

    setExecutionData(executionFiles)

}

subprojects.each { $project ->
    def tasks = $project.tasks.withType(JacocoReport)

    if (tasks != null) {
        mergeJacocoReport.dependsOn << tasks
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM