Android Gradle 代码覆盖率 [英] Android Gradle Code Coverage

查看:30
本文介绍了Android Gradle 代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有测试用例的简单 android 项目.

I have a simple android project with test cases.

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

我看到默认情况下android的新构建系统默认提供基本测试结果.(万岁!)

I see that by default android's new build system provides basic test results by default. (Hooray!)

现在我也想查看代码覆盖率.我知道如何使用 Emma 和 Ant 脚本进行设置,但是我不想在这里运行 Ant 脚本.我觉得这会违背我使用新构建系统的目的.

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. I feel that would defeat the purpose of me using the new build system.

我尝试了一些在 Github 上找到的 Cobertura 插件.特别是其中之一:https://github.com/stevesaliman/gradle-cobertura-plugin

I've tried a few Cobertura plugins that were found on Github. One in particular: https://github.com/stevesaliman/gradle-cobertura-plugin

但是,如果我尝试在 ProjName 构建文件中使用该插件,则会收到有关 java 插件的错误.我在 tools.android.com 上读到添加 java 插件会产生这种行为.我没有应用它,所以必须使用 cobertura 插件.
如果我尝试在主构建文件中使用该插件,那么我看不到 java 错误,但现在我看到了:

However if I try to use the plugin in the ProjName build file then I get errors about the java plugin. I read on tools.android.com that adding the java plugin will generate this behavior. I'm not applying it so the cobertura plugin must be.
If I try to use the plugin in the main build file then I don't see the java errors but now i see:

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

我该怎么办??

推荐答案

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

JaCoCo support was added to the Android gradle plugin 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'
  }
}

我能够通过关注 http://chrisjenx.com/gradle-robolectric-jacoco-dagger/.

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')
}

这篇关于Android Gradle 代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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