未为代码覆盖率生成 JaCoCo XML 报告

时间:2021-01-11 14:57:40

标签: gradle code-coverage jacoco gradle-kotlin-dsl kotlin-multiplatform

我目前有一个 KMM 多平台库的 gradle 构建脚本,如下所示。我正在尝试使用 JaCoCo 来报告测试结果和代码覆盖率。我可以在 HTML 中的某处看到测试结果,但我看不到任何地方生成的测试结果和代码覆盖率的 XML 报告。如何更改此 gradle 文件以便生成 XML 报告?
我正在使用 ./gradlew testCoverage

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    kotlin("plugin.serialization") version "1.4.10"
    id("com.android.library")
    id("kotlin-android-extensions")
    jacoco
}
group = "xxx"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}

val coroutinesVersion = "1.3.9-native-mt"
val ktorVersion = "1.4.0"

kotlin {
    android()
    ios {
    }
    sourceSets {
        val commonMain by getting {         
            dependencies {
                // ...
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization:$ktorVersion")
                implementation("io.ktor:ktor-client-cio:$ktorVersion")
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.0")
                implementation("io.ktor:ktor-client-android:$ktorVersion")
            }
        }

        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:$ktorVersion")
            }

            val targetName = System.getenv("TARGET_NAME") ?: ""

            if (targetName.endsWith("xxx")){
                kotlin.srcDirs("src/prod/kotlin")
            } else {
                kotlin.srcDirs("src/dev/kotlin")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))

                api("io.ktor:ktor-client-mock:$ktorVersion")
                api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
            }
        }

        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.12")
            }
        }
    }
}
android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }

    flavorDimensions("environment")
    productFlavors {

        create("dev") {
            dimension = "environment"
        }

        create("prod") {
            dimension = "environment"
        }
    }
}

jacoco {
    toolVersion = "0.8.5"
}

val jacocoTestReport by tasks.creating(JacocoReport::class.java) {
    reports {
        xml.isEnabled = true
        csv.isEnabled = false
        html.isEnabled = false
    }
}

val jacocoTestCoverageVerification by tasks.creating(JacocoCoverageVerification::class.java) {
    dependsOn(jacocoTestReport)
    violationRules {
        rule {
            limit {
                minimum = "0.8".toBigDecimal()
            }
        }
    }
}

val testCoverage by tasks.registering {
    group = "verification"
    description = "Runs the unit tests with coverage."

    dependsOn("test", jacocoTestReport, jacocoTestCoverageVerification)
    tasks["jacocoTestReport"].mustRunAfter("test")
    tasks["jacocoTestCoverageVerification"].mustRunAfter("jacocoTestReport")
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION")?.toUpperCase() ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)

PS.:我故意屏蔽了代码中一些敏感的字符串。

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,并通过指定源目录和执行数据来解决它。您可能需要根据您的项目更改这些值。

    sourceDirectories.setFrom(files(project.projectDir))
    executionData.setFrom(
            fileTree(project.projectDir) {
                setIncludes(setOf("**/**/*.exec", "**/**/*.ec"))
            }
    )