3

I use Kotlin for server side, and I want to add test coverage (by using JaCoCo but can be any other open source). At the moment, I don't find any documentation to explain how to enable/add coverage report to kotlin project based on kotest which actually works. (Yes I tried the official documentation)

build.gradle.kts:

import Libraries.coroutines
import Libraries.koTest
import Libraries.koTestCore
import Libraries.mockk
import Libraries.testcontainers
import Libraries.testcontainersPostgres

plugins {
    java
    `kotlin-dsl` version "1.3.5" apply false
    kotlin("jvm") version Versions.kotlin_version
    kotlin("plugin.serialization") version Versions.kotlin_version apply false
    id("com.diffplug.gradle.spotless") version Plugins.spotless
    id("com.palantir.docker") version Plugins.docker apply false
    id("com.palantir.docker-run") version Plugins.docker apply false
    id("com.google.protobuf") version Plugins.protobuf apply false
    id("org.flywaydb.flyway") version Plugins.flyway apply false
}

allprojects {
    group = "my-app"
    version = "2.0"

    apply(plugin = "kotlin")
    apply(plugin = "com.diffplug.gradle.spotless")

    repositories {
        mavenCentral()
        jcenter()
    }

    tasks {
        compileKotlin {
            dependsOn(spotlessApply)
            kotlinOptions {
                jvmTarget = "11"
            }
        }
        compileTestKotlin {
            kotlinOptions {
                jvmTarget = "11"
            }
        }
    }

    java {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    tasks.withType<Test> {
        useJUnitPlatform()
        maxParallelForks = 1
        environment("ENV", "test")
    }

    afterEvaluate {
        project.configurations.forEach {
            // Workaround the Gradle bug resolving multiplatform dependencies.
            if (it.name.contains("Proto")) {
                it.attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage::class.java, Usage.JAVA_RUNTIME))
            }
        }
    }
}

subprojects {
    spotless {
        kotlin {
            ktlint().userData(mapOf("disabled_rules" to "no-wildcard-imports"))
            trimTrailingWhitespace()
            endWithNewline()
        }
    }

    dependencies {
        implementation(coroutines)
        testImplementation(koTest)
        testImplementation(koTestCore)
        testImplementation(mockk)
        testImplementation(testcontainers)
        testImplementation(testcontainersPostgres)
    }

    configurations {
        all {
            resolutionStrategy.setForcedModules("org.apache.httpcomponents:httpclient:4.5.9")
        }
    }
}

Any help will be great. Thanks in advance.

Shai M.
  • 1,284
  • 4
  • 17
  • 30

1 Answers1

3

in my experience, Jacoco and Kotest interoperate just fine out of the box. I'm using it and all I need to do is the following:

plugins {
    jacoco
    // The rest of your plugins
}
// Your build, as it was before

You can inspect a working Jacoco + Kotest build here.

To verify that it works, you can e.g., feed the Jacoco generated data to services such as Codecov.io. First, generate the report in XML format:

tasks.jacocoTestReport {
    reports {
        xml.isEnabled = true
    }
}

And then use the Codecov.io service to fetch and analyze the results:

bash <(curl -s https://codecov.io/bash)

As you can see in this report, Jacoco is doing its job without further configuration.

Danilo Pianini
  • 966
  • 8
  • 19