Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android Gradle代码覆盖大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_0@
我有一个简单的 android项目与测试用例.
ProjNameProject
--build.gradle
--ProjName
----build.gradle

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

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

我尝试过几个在Github上发现的Cobertura插件.特别是:
https://github.com/stevesaliman/gradle-cobertura-plugin

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

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

该怎么办??

解决方法

JaCoCo支持添加到Android毕业插件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/之后获得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')
}

大佬总结

以上是大佬教程为你收集整理的Android Gradle代码覆盖全部内容,希望文章能够帮你解决Android Gradle代码覆盖所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。