Android - Jacoco代码覆盖忽略了Robolectric测试 [英] Android - Jacoco code coverage ignores Robolectric tests

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

问题描述

尝试使用Jacoco在Android上的Robolectric测试中获取代码覆盖率但是在创建报告时它只是拒绝承认我的Robolectric测试。

Trying to get Code coverage on my Robolectric tests in Android utilising Jacoco but it simply refuses to acknowledge my Robolectric tests when creating the reports.

我的jacoco.gradle文件如下:

My jacoco.gradle file is as follows:

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.6.201602180812"
}

project.afterEvaluate {

    android.applicationVariants.all { variant ->
        def name = variant.name
        def testTaskName = "test${name.capitalize()}UnitTest"

        tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${name.capitalize()} build."

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

            sourceDirectories = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs))
            executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")

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

通过此设置,我可以获得Coverage报告,但我获得了0%的覆盖率,尽管有Robole src / test / java中的ctric测试。

With this setup I can get Coverage reports but I get 0% coverage despite having Robolectric tests in "src/test/java".

如果我将以下代码添加到该文件中:

If I add in the following code to that file:

android {
    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }
}

Gradle尝试同步时出现以下错误:

I get the following error when Gradle tries to sync:


错误:没有这样的属性:类的includeNoLocationClasses:
org.gradle.testing.jacoco.plugins.JacocoTaskExtension_Decorated

Error:No such property: includeNoLocationClasses for class: org.gradle.testing.jacoco.plugins.JacocoTaskExtension_Decorated

我知道我需要让Gradle 2.13使用这个includeNoLocationClasses值,所以在graddle-wrapper.properties中我有以下内容:

I know that I need to have Gradle 2.13 to use this includeNoLocationClasses value so in graddle-wrapper.properties I have the following:

#Wed Apr 10 15:27:10 PDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions-snapshots/gradle-2.13-20160228000026+0000-all.zip

我很确定我是使用Android插件版本1.5运行Gradle 2.13

I am pretty certain I am running Gradle 2.13 with Android plugin version 1.5

在我的应用程序Gradle文件中,我有以下内容:

In my apps Gradle file I have the following:

//...

apply from: 'jacoco.gradle'

//..

testOptions {
    unitTests.returnDefaultValues = true
}

//...

debug {
    testCoverageEnabled true
}

我用来运行保险的命令是:

And the command I use to run the coverage is:


./ gradlew createDebugCoverageReport

./gradlew createDebugCoverageReport

所以我想知道为什么我得到includeNoLocationClasses错误尽管使用了正确的Gradle版本?除此之外,我可能做错了,Jacoco没有在src / test.java中接受Robolectric测试?

So I am wondering why I get the includeNoLocationClasses error despite using the correct Gradle version? And outside of that maybe I am doing something wrong where Jacoco isn't picking up the Robolectric tests in src/test.java ?

推荐答案

我没有完全看到你 build.gradle ,但是要让你拥有那个标志:

I don't see you build.gradle completely, but to have that flag in you have to:


  1. 使用 gradle 2.13 +

  2. 使用 jacoco 0.7.6.201602180812

  1. Use gradle 2.13+
  2. Use jacoco 0.7.6.201602180812

您确定使用 gradle 正确的版本。所以,我认为,问题只在于使用了错误的 jacoco

You're sure that you use gradle proper version. So, I think, the issue is only in using wrong jacoco.

提及 jacoco {toolVersion =0.7.6.201602180812} 不会影响 gradle DSL。你应该添加更新的 jacoco 插件:

Mentioning jacoco {toolVersion = "0.7.6.201602180812"} doesn't influence gradle DSL. You should add newer jacoco plugin:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.jacoco:org.jacoco.core:...'
    }
}

你应该申请插件,你已经在做了:

And you should apply plugin, which you're already doing:

apply from: 'jacoco'

之后这样的配置你不需要 jacoco {toolVersion =...} 更多。

After such configuraiton you don't need jacoco {toolVersion = "..."} more.

注意:考虑要更新到较新的 android gradle 插件, 2.2.x 已经稳定。 jacoco 还有更新的版本 0.7.7.201606060606

Note: consider to update to newer android gradle plugin, 2.2.x is already stable. jacoco also has newer version already 0.7.7.201606060606

还有一个注意事项:如果你请参阅Android Studio中的原始问题,检查您是否默认使用包装器并检查您是否将包装器指向gradle 2.13

One more note: if you see original issue in Android Studio, check that you use wrapper by default and check that you pointed wrapper to gradle 2.13

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

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