android studio 中的 Jacoco 代码覆盖率 [英] Jacoco Code Coverage in android studio

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

问题描述

我正在尝试生成 Jacoco 代码覆盖率报告.我已经将 AndroidTestCase 用于我的测试类.

I am trying to generate Jacoco code coverage report. I have used AndroidTestCase for my test classes.

我发现使用 testCoverageEnabled true 并使用默认的 android -studio 默认 jacoco,./gradlew connectedCheck 或 createDebugCoverageReport创建成功/失败测试用例的百分比,但没有覆盖率报告.

I have found using testCoverageEnabled true and using default android -studio default jacoco, ./gradlew connectedCheck or createDebugCoverageReport create the percentage of successfull/fail test cases, but no coverage report.

然后我尝试了 jacoco {toolVersion "0.7.1.201405082137"} 和任务 jacocoTestReport(type:JacocoReport,dependsOn: "testDebug").我试图通过各种任务更改dependsOn 值.报告显示 0(零)测试覆盖率,这是不可能的,因为至少有一半的类都经过了测试.

Then I have tried jacoco {toolVersion "0.7.1.201405082137"}, and task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug"). I have tried to change the dependsOn value with various task. The report shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.

在过去的几天里,我遵循了各种公认的堆栈溢出答案.结果是否定的.

I have followed various accepted answer of stack overflow in last couple of days. The result is negative.

我的gradle文件:

My gradle file:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'        
    }
}

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

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "test.gradle.com.myapplicationtestgradle"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false            
            proguardFiles getDefaultProguardFile(
            'proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }

    jacoco {
        version "0.7.1.201405082137"
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}


jacoco {
    toolVersion "0.7.1.201405082137"
}

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

   // exclude auto-generated classes and tests
    def fileFilter = ['**/R.class', '**/R$*.class', 
    '**/BuildConfig.*', '**/Manifest*.*',           
     '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])
    additionalSourceDirs = files([
            "${buildDir}/generated/source/buildConfig/debug",
            "${buildDir}/generated/source/r/debug"
    ])
    executionData = fileTree(dir: project.projectDir, includes: 
                    ['**/*.exec', '**/*.ec'])

    reports {
        xml.enabled = true
        xml.destination = "${buildDir}/jacocoTestReport.xml"
        csv.enabled = false
        html.enabled = true
        html.destination = "${buildDir}/reports/jacoco"
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
}

推荐答案

我看到您已经开始使用它了,但是,还有一种更简单的方法来获取单元测试执行数据.我最近也在研究这个,实际上我写了一篇完整的文章今天早些时候.

I see that you already got it working, however, there's a simpler method for getting Unit Test execution data. I recently was looking into this as well, I actually made a full write up earlier today.

在我的情况下,我不想创建额外的 Gradle 任务,因为我希望生成报告作为现有工作流程的一部分.我也不想明确添加 Jacoco 插件,如 Google 已经复制了 Jacoco Ant 任务,用于仪器测试的覆盖率报告.

In my situation, I didn't want to create an additional Gradle task as I wanted the report to be generated as a part of the existing workflow. I also didn't want to explicitly add the Jacoco plugin, as Google already dups the Jacoco Ant tasks for the coverage reports for Instrumentation Tests.

除了设置属性android.jacoco.versionbuildTypes.debug.testCoverageEnabled,我在testDebug JVM 中添加了以下内容生成执行数据的参数:

In addition to setting the properties android.jacoco.version and buildTypes.debug.testCoverageEnabled, I added the following to the testDebug JVM arguments to generate execution data:

project.afterEvaluate {
  def append = "append=true"
  def destFile = "destfile=$buildDir/outputs/code-coverage/connected/coverage.ec"
  testDebug.jvmArgs "-javaagent:$buildDir/intermediates/jacoco/jacocoagent.jar=$append,$destFile"

  createDebugCoverageReport.dependsOn testDebug
}

这会将单元测试执行数据附加到由 connectedAndroidTest 生成的覆盖文件中,因此您的报告会同时反映仪器测试和单元测试,而不是单独反映每个变体.

This appends the Unit Test execution data to the coverage file generated by connectedAndroidTest, so your report reflects both Instrumentation Tests and Unit Tests, rather than each variant individually.

请注意,connectedAndroidTest 会覆盖覆盖率文件,请在创建报告时考虑到这一点.如果任务 testDebug 没有任何更改,并且您运行 createDebugCoverageReport,它只会反映您的 Instrumentation Test 覆盖率.因此,对您的单元测试进行更改.Linux 命令 touch 在这里可能很有用,虽然我还没有尝试过.

Note that connectedAndroidTest overwrites the coverage file, take this into account when creating your report. If the task testDebug doesn't have any changes, and you run createDebugCoverageReport, it will only reflect your Instrumentation Test coverage. So, make a change to your Unit Tests. The Linux command touch may be useful here, although I haven't tried yet.

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

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