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
sourceDirs
and jacocoReportPath
which I already have tried.sourceDirs += ['src/main/kotlin']
which sounds sensible but doesn't help. Same for, from the first link, sourceDirs = files(sourceSets.main.kotlin.srcDirs).files.absolutePath
.project.extensions.coveralls.sourceDirs += project.sourceSets.main.kotlin.srcDirs
which I tried.The question Kotlin code coverage in CI pipeline is phrased rather generally, but a comment points to discuss.kotlinlang.org where someone shows a way to improve the Jacoco result regarding kotlin, and the answer links to the Jacoco Gradle plugin which I use and works: when I run the jacocoTestReport
task a report is generated in build/reports/jacoco/test/
, both in xml and html.
The question Kotlin Test Coverage is also phrased general and answered with an unnecessarily complex build file from which I learned nothing new.
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.
[edit August 2020] @nbaztec wrote a plugin to support Kotlin, please see his answer.
Old answer:
Kotlin is not supported by Coveralls, see for example this open isse that was mentioned in the question as well (in the question it was also mentioned that the workaround presented there does not work): https://github.com/kt3k/coveralls-gradle-plugin/issues/77
Solution: try Codecov.io instead.
Install it to GitHub using the Marketplace and add to your .travis.yml
after_success:
- bash <(curl -s https://codecov.io/bash)
Then commit and push, done!
You can view the result (after the build finished) at https://codecov.io/gh/githubaccountname/reponame
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
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
Not an answer, but in case anyone else is struggling with nbaztec like me, I want to give an alternative that worked for me: https://github.com/kt3k/coveralls-gradle-plugin
And besides what is in README.md, I needed this detail in build.gradle:
coveralls {
sourceDirs += ['src/main/kotlin']
jacocoReportPath "${buildDir}/reports/jacoco/report.xml"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With