Jacoco和单元测试与Android的摇篮 - 插件&GT code覆盖率= 1.1 [英] Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1

查看:251
本文介绍了Jacoco和单元测试与Android的摇篮 - 插件&GT code覆盖率= 1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始整合 Android的摇篮 - 插件 1.1.0在我的项目之一。该项目采用 robolectric 2.4 来运行单元测试。

I recently started integration android-gradle-plugin 1.1.0 in one of my projects. The project uses robolectric 2.4 to run unit tests.

这是一个多模块项目有非常复杂的依赖关系(有些模块依赖于其他模块)。类似的东西:

It's a multi module project with very complex dependencies (Some modules depend on other modules). Something like that:

--> application-module (dependsOn: module1, module2, module-core)
    --> module1 (dependsOn: module-core)
    --> module2 (dependsOn: module-core)
    --> module-core (dependsOn: module3, module4)
        --> module3 (library dependencies)
        --> module4 (library dependencies)

有关更清画面,请参阅 jacoco,例如的项目。

For a more cleared picture please see jacoco-example project.

我试图整合JaCoCo来生成单元测试报告,但它接缝对我来说,它只能运行 androidTests 这是基本的仪器测试。

I tried to integrate JaCoCo to generate reports for the unit tests, but it seams to me that it runs only androidTests which are basically instrumentation tests.

在一些google'ing我已经碰到在GitHub和其他物品的几个项目,但它们主要都集中在 Android的摇篮 - 插件previous版本或正在使用其他第三方插件,像 Android的单元测试例如这里

After some google'ing I've came across a few projects on GitHub and other articles, but they mainly are focused on previous versions of the android-gradle-plugin or are using other third party plugins like android-unit-test for example here.

可能是我失去了我的能力,以谷歌。但有人可以点我的方向在哪里可以找到关于在Android的摇篮插件,以及如何只对单元测试运行jacoco任务?新的东西,一些单证

May be I've lost my ability to google. But can somebody point me in a direction where I can find some documentations regarding the new stuff in android gradle plugin and how to run the jacoco task only for unit tests?

更新

nenick的例子采用的脚本:

apply plugin: "jacoco"

configurations {
    jacocoReport
}

task jacocoReport(dependsOn: 'testDebug') << {
    ant {
        taskdef(name:'jacocoreport',
                classname: 'org.jacoco.ant.ReportTask',
                classpath: configurations.jacocoReport.asPath)

        mkdir dir: "${buildDir}/test-coverage-report"
        mkdir dir: "${buildDir}/reports/jacoco/test/"

        jacocoreport {
            executiondata = files("${buildDir}/jacoco/testDebug.exec")

            structure(name: "${rootProject.name}") {
                classfiles {
                    fileset (dir: "${buildDir}/intermediates/classes/debug") {
                        //exclude(name: '**/*_*.class')
                        exclude(name: '**/R.class')
                        exclude(name: '**/R$*.class')
                        exclude(name: '**/BuildConfig.class')
                    }
                }

                sourcefiles {
                    fileset dir: "src/main/java"
                    fileset dir: "${buildDir}/generated/source/buildConfig/debug"
                    fileset dir: "${buildDir}/generated/source/r/debug"
                }
            }

            xml destfile: "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
            html destdir: "${buildDir}/test-coverage-report/"
        }
    }
}

dependencies {
    jacocoReport 'org.jacoco:org.jacoco.ant:0.7.2.201409121644'
}

在该 ./ gradlew jacocoReport 执行并生成报告,但它显示了0(零)测试覆盖率,这是不可能的,因为至少有一半的类都是测试。

After that the ./gradlew jacocoReport executes and generates the report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.

UPDATE_2

尝试了这个<一href="http://raptordigital.blogspot.de/2014/08/$c$c-coverage-reports-using-robolectric.html">example.添加下一个任务我的摇篮构建文件之一:

Tried out this example. Adding the next task to one of my gradle build files:

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: "${buildDir}/intermediates/classes/debug",
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    sourceDirectories = files("${buildDir.parent}/src/main/java")
    additionalSourceDirs = files([
            "${buildDir}/generated/source/buildConfig/debug",
            "${buildDir}/generated/source/r/debug"
    ])
    executionData = files("${buildDir}/jacoco/testDebug.exec")

    reports {
        xml.enabled = true
        html.enabled = true
    }
}

同样的问题,生成报告,但code覆盖率仍为零。

Same issue, the reports are generated, but the code coverage is still zero.

UPDATE_3

据接缝,从UPDATE_2任务的工作,但仅适用于具有模块的应用插件:com.android.application(该报表的正确生成)。但对于模块的机器人库(应用插件:com.android.library)的报告显示零覆盖,altho该模块包含更多的测试,那么应用程序模块

It seams that the task from UPDATE_2 worked but only for the module with apply plugin: 'com.android.application' (The reports a generated correctly). But for modules that are android libraries (apply plugin: 'com.android.library') the reports show zero coverage, altho the modules contain more tests then the application module.

UPDATE_4

创建了一个简单的示例项目,演示了我的问题。目前,如果你运行 ./ gradlew jacocoReport 生成报告,但没有测试覆盖率则显示模块项目。请参阅此链接

Created a simple example project that demonstrates my issue. Currently if you run ./gradlew jacocoReport the report is generated, but no test coverage is displayed for the module projects. See this link

便签:当试验中AndoridUnitTests(白化的JUnit 4和Robolectric)JaCoCo报告显示,覆盖所有的模块

Short note: When the test where AndoridUnitTests (whiteout JUnit 4 and Robolectric) JaCoCo reports showed coverage for all the modules.

任何想法?

推荐答案

在在此我偶然发现了一些额外的搜索项目 我不得不做出一些修改,以便有解决方案,可以为我的项目类型的工作,但现在被正确生成的测试覆盖报告。

After some additional search I've stumbled upon this project I had to make some modifications so that there solution can work for my type of project, but now the test coverage reports are generated properly.

我已经把所采用的改变我的例如Gi​​tHub库的情况下,有人会在将来有类似的问题。

I've pushed the adopted changes to my example github repo in case someone will have a similar problem in the future.

这篇关于Jacoco和单元测试与Android的摇篮 - 插件&GT code覆盖率= 1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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