简体   繁体   English

如何使用 Espresso 测试获取 Android 项目的覆盖率

[英]How to obtain coverage for Android project using Espresso tests

I used to write Android tests using Robotium and retrieve the coverage using Emma.我曾经使用 Robotium 编写 Android 测试并使用 Emma 检索覆盖范围。

Recently I changed to use Espresso tests and I'm having troubles to retrieve coverage of Instrumentation tests.最近我改用 Espresso 测试,但在检索 Instrumentation 测试的覆盖率时遇到了麻烦。 I can only retrieve coverage for Unit tests that use Robolectric.我只能检索使用 Robolectric 的单元测试的覆盖率。 I'm currently using gradle and Jacoco to do that.我目前正在使用 gradle 和 Jacoco 来做到这一点。 The best tutorial I found which helped me to get to this point was: https://blog.gouline.net/2015/06/23/code-coverage-on-android-with-jacoco/我发现帮助我达到这一点的最佳教程是: https : //blog.gouline.net/2015/06/23/code-coverage-on-android-with-jacoco/

Is it possible to retrieve coverage of Espresso tests that use Android instrumentation?是否可以检索使用 Android 检测的 Espresso 测试的覆盖范围?

The android gradle plugin has a built-in feature. android gradle插件具有内置功能。

Just set testCoverageEnabled parameter to true in your build.gradle file: 只需在build.gradle文件中将testCoverageEnabled参数设置为true

android {
   buildTypes {
      debug {
         testCoverageEnabled = true
      }
   }
}

Then use: 然后使用:

./gradlew connectedCheck

or 要么

./gradlew createDebugCoverageReport

It will produce a test coverage report in the directory of the module: 它将在模块目录中生成测试覆盖率报告:

/build/outputs/reports/coverage/debug/

Just open the index.html 只需打开index.html

Example: 例:

在此输入图像描述

Coverage report in Android with Jacoco (Java Code Coverage) Android中使用Jacoco的覆盖率报告(Java代码覆盖率)

Android Gradle plugin >=3.x Android Gradle插件> = 3.x.

add the new Jacoco version in the classpath dependencies 在类路径依赖项中添加新的Jacoco版本

project build.gradle file 项目build.gradle文件

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'org.jacoco:org.jacoco.core:0.8.0'
    }
}

app build.gradle file app build.gradle文件

// app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

jacoco {
    toolVersion = '0.8.0'
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

...

Create task eg jacocoTestReport 创建任务,例如jacocoTestReport

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

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

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
    ])
}

Read more here 在这里阅读更多

Android Gradle plugin <3.x Android Gradle插件<3.x
Android Gradle plugin >=3.x Android Gradle插件> = 3.x.

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

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