Android Gradle代码覆盖率

9

我有一个简单的安卓项目,其中包括测试用例。

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

我发现默认情况下Android的新构建系统提供基本测试结果。(太棒了!)

现在我想看到代码覆盖率。我知道如何使用Emma和Ant脚本设置,但我不想在这里运行Ant脚本。我觉得这样做会失去我使用新构建系统的意义。

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

但是,如果我尝试在ProjName构建文件中使用该插件,则会出现关于java插件的错误。我在tools.android.com上读到,添加java插件将生成此行为。我没有应用它,所以肯定是cobertura插件在应用。
如果我尝试在主构建文件中使用该插件,则不会看到Java错误,但现在我看到:

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

我该怎么办?

有人尝试在Android项目中使用Clover Gradle插件吗? - SoH
2
链接的问题是在我的问题之后约三个月提出的... 所以实际上那可能是我问题的重复。 - Sababado
2个回答

7

Android gradle插件v0.10添加了JaCoCo支持(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'
  }
}

我按照这篇文章的步骤,成功地将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')
}

请查看改进的脚本:https://gist.github.com/ultraon/54cca81ca159ed0a4a9ebf62e89c26ba - ultraon

6
Emma支持计划很快在新的Android构建系统中发布:http://tools.android.com/tech-docs/new-build-system/roadmap 到目前为止,还没有官方的方式通过Gradle在Android上运行emma。我猜测仪器化可以很容易地实现,但是,你将错过一种告诉Android在覆盖测试下运行测试的方法。此外,目前没有办法(据我所知)从设备中获取emma运行时覆盖数据。
这个项目可能会引起您的兴趣:https://github.com/stephanenicolas/Quality-Tools-for-Android。一旦emma进入Android Gradle插件,它将被更新。
----更新
该插件无法与Android一起工作,因为它使用不兼容Android插件的Java插件。

虽然Gradle的Emma插件依赖于Java插件(因此无法与Android插件一起使用),但是Gradle的JaCoCo插件似乎不依赖于Java插件,应该可以工作。 - sschuberth
您可以在上述提到的QAT项目中找到一个使用Jacoco进行仪器化的Gradle构建示例。不过,上次我尝试时,出现了一个回归问题,导致最新版本的Jacoco无法正常工作,但这个问题可能已经被修复了。 - Snicolas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接