在机器人工作室Jacoco code覆盖 [英] Jacoco Code Coverage in android studio

查看:495
本文介绍了在机器人工作室Jacoco code覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生Jacoco code覆盖报告。 我已经使用AndroidTestCase我的测试类。

我一直使用testCoverageEnabled真实,使用默认的Andr​​oid -studio默认jacoco发现, ./gradlew connectedCheck或createDebugCoverageReport 打造全成的百分比/失败测试案例,但没有覆盖报告。

然后我试图jacoco {toolVersion0.7.1.201405082137}和任务jacocoTestReport(类型:JacocoReport,dependsOn:testDebug)。我曾试图改变dependsOn值与各种任务。该报告显示0(零)的测试覆盖率,这是不可能的,因为至少一半的所有类的进行测试。

我按照堆栈溢出在过去几天的各种公认的答案。结果是否定的。

我的摇篮文件:

  buildscript {
    库{
        mavenLocal()
        mavenCentral()
    }
    依赖{
        类路径com.android.tools.build:gradle:1.0.0
    }
}

应用插件:com.android.application
应用插件:jacoco

安卓{
    compileSdkVersion 21
    buildToolsVersion21.1.2

    defaultConfig {
        的applicationIDtest.gradle.com.myapplicationtestgradle
        的minSdkVersion 21
        targetSdkVersion 21
        版本code 1
        VERSIONNAME1.0
    }

    buildTypes {
        推出 {
            minifyEnabled假
            proguardFiles getDefaultProguardFile(
            ProGuard的-android.txt'),'proguard-rules.pro
        }
        调试{
            testCoverageEnabled真
        }
    }

    jacoco {
        版本0.7.1.201405082137
    }

    packagingOptions {
        排除的LICENSE.txt
    }
}


jacoco {
    toolVersion0.7.1.201405082137
}

任务jacocoTestReport(类型:JacocoReport,dependsOn:testDebug){
    组=报告

    说明=生成Jacoco覆盖报告

   //排除自动生成的类和测试
    高清的FileFilter = ['** / R.class','** / R $ *类的,
    '** / BuildConfig。*','** /清单*。*',
     机器人/**/*.*']
    高清debugTree =文件树(导演:
    $ {project.buildDir} /中间体/班/调试,
    不包括:一个FileFilter)
    DEF mainSrc =$ {project.projectDir} / src目录/主/ Java的

    sourceDirectories =文件([mainSrc])
    classDirectories =文件([debugTree])
    additionalSourceDirs =文件([
            $ {buildDir} /生成/来源/ buildConfig /调试,
            $ {buildDir} /生成/来源/ R /调试
    ])
    executionData =文件树(导演:project.projectDir,包括:
                    ['** / *。EXEC','** / *。EC'])

    报告{
        xml.enabled =真
        xml.destination =$ {buildDir} /jacocoTestReport.xml
        csv.enabled = FALSE
        html.enabled =真
        html.destination =$ {buildDir} /报告/ jacoco
    }
}


依赖{
    编译文件树(导演:库,包括:['的* .jar'])
    androidTestCompile('的JUnit:JUnit的:4.11'){
        排除模块:hamcrest芯
    }
}
 

解决方案

我看到你已经得到它的工作,但是,有获取单元测试执行数据更简单的方法。我最近一直在寻找到这个问题,以及,我居然做了充分的写上去的今天早些时候

在我的情况,我不想创建一个额外的摇篮的任务,因为我想生成为现有工作流程的一部分的报告。我也不想明确添加Jacoco插件,如<一个href="https://android.googlesource.com/platform/tools/base/+/master/build-system/gradle-core/src/main/groovy/com/android/build/gradle/internal/coverage/JacocoPlugin.groovy"相对=nofollow>谷歌已经中的DUP的Jacoco Ant任务为仪器测试的覆盖报告。

除了设置属性 android.jacoco.version buildTypes.debug.testCoverageEnabled ,我增加了以下为 testDebug JVM参数生成执行数据:

project.afterEvaluate {   DEF追加=追加=真   DEF destFile =destfile = $ buildDir /输出/ code-报道/连接/ coverage.ec   testDebug.jvmArgs-javaagent:$ buildDir /中间体/ jacoco / jacocoagent.jar = $追加,$ destFile   createDebugCoverageReport.dependsOn testDebug }

这追加单元测试执行数据由 connectedAndroidTest 生成覆盖率文件,所以您的报告反映了仪器测试和单元测试,而不是每个单独的变种。

注意 connectedAndroidTest 覆盖范围的文件,创建报表时考虑到这一点。如果任务 testDebug 没有任何变化,并运行 createDebugCoverageReport ,它只能反映你的仪表测试覆盖。因此,更改了单元测试。 Linux命令触摸可能是有用的在这里,虽然我还没有尝试过。

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

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.

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.

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.

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.

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
}

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.

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.

这篇关于在机器人工作室Jacoco code覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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