【问题标题】:JaCoCo returning 0% Coverage with Kotlin and Android 3.0JaCoCo 使用 Kotlin 和 Android 3.0 返回 0% 覆盖率
【发布时间】:2017-08-02 14:49:59
【问题描述】:

我正在尝试检查我在 Kotlin 中编写的测试用例的代码覆盖率。当我执行./gradlew createDebugCoverageReport --info 时,我的coverage.ec 文件为空,我的报告显示我的覆盖率为0%。请注意,测试用例是 100% 成功的。谁能想到我的coverage.ec文件一直返回0字节的任何原因?

我到处搜索都没有运气。

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'jacoco'


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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


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

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation files('pathtosomejarfile')

}



jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")

}



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: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/androidTest/java"

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

【问题讨论】:

标签: android kotlin code-coverage jacoco


【解决方案1】:

您可以通过为生成的 .class 文件定义两个不同的目录来逐行覆盖 Java 和 Kotlin 代码:

def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)

然后,只需在您的 classDirectories 中包含两个 fileTree:

classDirectories.from = files([debugTree], [kotlinDebugTree])

【讨论】:

  • 这是根据 stackoverflow.com/a/45354933/3286489 中的报告。但是如果仔细观察,它报告的是测试代码的覆盖率,而不是应用程序代码。 :(
  • 建议的解决方案应该有效。我不知道这篇文章,并以类似的方式修复它。为此写了一篇博文:vgaidarji.me/blog/2017/12/20/…
  • 谢谢@VeaceslavGaidarji!
  • classDirectories 已弃用。那么答案应该是什么
  • @charithaamarasinghe 使用classDirectories.from 而不是classDirectories
【解决方案2】:

几天后,我想出了解决这个问题的办法。对于那些遇到类似问题的人: 在您的中间文件夹中应该有一个 tmp 文件夹。此文件夹包含 Kotlin 文件的 .class 文件。如果您将 fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter) 的路径更改为这些类文件所在的位置,Jacoco 将为您生成代码覆盖率!请注意,使用此方法您将无法查看完整的逐行概览。

【讨论】:

  • 但是为什么呢?它不适用于 -> {buildDir}/tmp/kotlin-classes/universalDebugAndroidTest/ 路径吗?
【解决方案3】:

尝试使用与 JaCoCo 和 IntelliJ 兼容的新 kotlinx-kover Gradle 插件。
它解决了 inline 函数报告为 0% 甚至更多的问题。

plugins {
     id("org.jetbrains.kotlinx.kover") version "0.4.1"
}

应用后,该插件无需额外配置即可开箱即用。

观看其YouTube announcement video 并从this youtrack issue 跟踪其路线图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2016-11-11
    • 1970-01-01
    • 2017-05-31
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多