Search code examples
gradlekotlinjacococoverallsgradle-kotlin-dsl

How to upload test reports of Kotlin sources to Coveralls?


I want to upload my Jacoco test report to Coveralls automatically after my Travis build finishes. It works for Java, but how to configure it for Kotlin?

Error message

I can generate a Jacoco test report locally and on Travis, but when Travis tries to submit to coveralls it fails with message

> Task :coveralls
No source file found on the project: "kotlin-template-project"
With coverage file: /home/travis/build/myname/myreponame/build/reports/jacoco/test/jacocoTestReport.xml

Google links me to the Gradle plugin implementation which shows where it throws this message, which tells me (I think) that the Jacoco report file is found but not the source files which coveralls apparently needs.

What I tried

Hence, I tried pointing the coveralls task to my source files, in all of these ways:

coveralls {
    sourceDirs += allprojects.sourceSets.main.allSource.srcDirs.flatten()
    sourceDirs += files(sourceSets.main.kotlin.srcDirs).files.absolutePath
    project.extensions.coveralls.sourceDirs += project.sourceSets.main.kotlin.srcDirs
    sourceDirs += ['src/main/kotlin']
    jacocoReportPath = 'build/reports/jacoco/test/jacocoTestReport.xml'
    sourceDirs += ['src/test/kotlin']
    sourceDirs += ["${projectDir}/src/main/kotlin"]
}

I also tried adding sourceSets project.sourceSets.main to the jacocoTestReport task.

Project setup

My minimal build.gradle file:

plugins {

    id 'org.jetbrains.kotlin.jvm' version '1.2.50'
    id 'java' // Required by at least JUnit.

    // Test coverage
    id 'jacoco'

    // Upload jacoco coverage reports to coveralls
    id 'com.github.kt3k.coveralls'  version '2.8.2'
}

dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

    // JUnit 5
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
    testRuntime 'org.junit.platform:junit-platform-console:1.2.0'

    // Kotlintest
    testCompile 'io.kotlintest:kotlintest-core:3.1.6'
    testCompile 'io.kotlintest:kotlintest-assertions:3.1.6'
    testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.6'

    // Spek
    testCompile 'org.jetbrains.spek:spek-api:1.1.5'
    testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
}

test {
    // Enable JUnit 5 (Gradle 4.6+).
    useJUnitPlatform()

    // Always run tests, even when nothing changed.
    dependsOn 'cleanTest'

    // Show test results.
    testLogging {
        events "passed", "skipped", "failed"
    }
}

// Test coverage reporting
jacocoTestReport {
    // Enable xml for coveralls.
    reports {
        html.enabled = true
        xml.enabled = true
        xml.setDestination(file("${buildDir}/reports/jacoco/test/jacocoTestReport.xml"))
    }
}

coveralls {
    sourceDirs += ['src/main/kotlin']
    jacocoReportPath = 'build/reports/jacoco/test/jacocoTestReport.xml'
}

Related issues

PS Actually I want to use the Gradle Kotlin DSL, but since nobody seems to use it I'm asking this question for Gradle. But in the end I want this question solved for the Kotlin DSL as well.


Solution

  • Had a similar experience with a variety of QA products not supporting or only partially supporting Kotlin codebases. Tried submitting support PRs to a couple of projects to no avail.

    In the end ended up going with Coveralls and contributed a Kotlin focused plugin for the platform

    https://github.com/nbaztec/coveralls-jacoco-gradle-plugin

    Usage

    Include the plugin in your build.gradle.kts (similar for build.gradle files):

    plugins {
        jacoco
        id("com.github.nbaztec.coveralls-jacoco")
    }
    

    Then set the environment variable COVERALLS_REPO_TOKEN to the token from your Coveralls page.

    Now you can use the coverallsJacoco task to publish a coverage report.

    For more information and usage in CI, see https://github.com/nbaztec/coveralls-jacoco-gradle-plugin