简体   繁体   English

Android Gradle代码覆盖率

[英]Android Gradle Code Coverage

I have a simple android project with test cases. 我有一个简单的android项目与测试用例。

ProjNameProject
--build.gradle
--ProjName
----build.gradle

I see that by default android's new build system provides basic test results by default. 我看到默认情况下,android的新构建系统默认提供基本的测试结果。 (Hooray!) (万岁!)

Now I want to see code coverage as well. 现在我想看看代码覆盖率。 I know how to set this up using Emma and Ant scripts, however I don't want to run Ant scripts here. 我知道如何使用Emma和Ant脚本进行设置,但是我不想在这里运行Ant脚本。 I feel that would defeat the purpose of me using the new build system. 我觉得这会破坏我使用新构建系统的目的。

I've tried a few Cobertura plugins that were found on Github. 我尝试过在Github上找到的一些Cobertura插件。 One in particular: https://github.com/stevesaliman/gradle-cobertura-plugin 特别是: https//github.com/stevesaliman/gradle-cobertura-plugin

However if I try to use the plugin in the ProjName build file then I get errors about the java plugin. 但是,如果我尝试在ProjName构建文件中使用插件,那么我会收到有关java插件的错误。 I read on tools.android.com that adding the java plugin will generate this behavior. 我在tools.android.com上读到,添加java插件会产生这种行为。 I'm not applying it so the cobertura plugin must be. 我没有应用它,所以cobertura插件必须。
If I try to use the plugin in the main build file then I don't see the java errors but now i see: 如果我尝试在主构建文件中使用插件,那么我没有看到java错误,但现在我看到:

Could not find net.sourceforge.cobertura:cobertura:1.9.4.1.
    Required by:
        :ProjNameProject:unspecified

What do I do?? 我该怎么办??

JaCoCo support was added to the Android gradle plugin v0.10 ( http://tools.android.com/tech-docs/new-build-system ). JaCoCo支持已添加到Android gradle插件v0.10( http://tools.android.com/tech-docs/new-build-system )。

Enable in the tested Build Type with testCoverageEnabled = true

android {
  jacoco {
    version = '0.6.2.201302030002'
  }
}

I was able to get JaCoCo coverage working with Robolectric by following http://chrisjenx.com/gradle-robolectric-jacoco-dagger/ . 我可以通过http://chrisjenx.com/gradle-robolectric-jacoco-dagger/让JaCoCo报道与Robolectric合作。

apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'jacoco'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:19.1.+'

    androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
    androidTestCompile 'junit:junit:4.11'
    androidTestCompile 'org.robolectric:robolectric:2.3'
    androidTestCompile 'com.squareup:fest-android:1.0.+'
}

robolectric {
    // Configure the set of classes for JUnit tests
    include '**/*Test.class'
    exclude '**/*AbstractRobolectricTestCase.class'

    // Configure max heap size of the test JVM
    maxHeapSize = "2048m"
}

jacoco {
    toolVersion = "0.7.1.201405082137"
}

//Define coverage source.
//If you have rs/aidl etc... add them here.
def coverageSourceDirs = [
    'src/main/java',
    'src/gen'
]

...

// Add JaCoCo test reporting to the test task
// http://chrisjenx.com/gradle-robolectric-jacoco-dagger/
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }

    // Class R is used, but usage will not be covered, so ignore this class from report
    classDirectories = fileTree(
        dir: './build/intermediates/classes/debug',
        excludes: ['**/R.class',
                   '**/R$*.class'
    ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebug.exec')
}

Emma support is planned to be released soon within the new Android build system : http://tools.android.com/tech-docs/new-build-system/roadmap 计划在新的Android构建系统中很快发布Emma支持: http//tools.android.com/tech-docs/new-build-system/roadmap

Up to now there is no official way to run emma with android via gradle. 到目前为止,没有正式的方法来通过gradle运行emma与android。 I guess instrumenting can be achieved pretty easily but then, you will miss a way to tell Android to run tests with coverage on. 我猜测仪器可以很容易地实现,但是,你会错过一种告诉Android运行覆盖测试的方法。 Moreover, there is currently no way (to the best of my knowledge) to pull down emma runtime coverage data from the device. 此外,目前还没有办法(据我所知)从设备中提取emma运行时覆盖率数据。

This project can interest you : https://github.com/stephanenicolas/Quality-Tools-for-Android . 这个项目可能会让您感兴趣: https//github.com/stephanenicolas/Quality-Tools-for-Android It will be updated as soon as emma will make it into Android Gradle plugin. 一旦emma进入Android Gradle插件,它就会更新。

----UPDATE ---- UPDATE

This plugin has no chance to work with Android has it uses the Java plugin which is incompatible with Android plugin. 如果它使用与Android插件不兼容的Java插件,则此插件无法使用Android。

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

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