-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin code coverage is not reported #37
Comments
Having the same issue. Any updates on this? |
Having this exact issue. Is there an ETA or a workaround? |
I ended up having to configure the tasks myself:
The key here is including the kotlin-classes directory in addition to the classes directory. I hope the makers of this plugin will address this. I can try looking and seeing if i can create a PR for this. Probably this line of code (74) in JacocoAndroidPlugin.groovy
just needs another value in there. |
Ah, I've just got it to work. The main problem was that I needed to enable some noLocationClasses stuff. If someone gets here, here's how to make it work:
android {
..
testOptions.unitTests.all {
jacoco.includeNoLocationClasses = true
}
} And thats it! I dont use this plugin because I already have a file that does pretty much this, but as @mattinger pointed out, you should include kotlin file tree If the dev wants my file (to check that out) here it is :) // File: jacoco.gradle
apply plugin: JacocoPlugin
final String JACOCO_FULL_REPORT_TASK = "jacocoFullReport"
tasks.withType(Test) {
testLogging {
events "FAILED"
exceptionFormat "full"
}
}
task(JACOCO_FULL_REPORT_TASK) {
it.group 'reporting'
}
Task jacocoTestReportTask = findOrCreateJacocoTestReportTask()
getVariants().all { variant ->
JacocoReport reportTask = createReportTask(variant)
jacocoTestReportTask.dependsOn reportTask
}
if (project.tasks.findByName(JACOCO_FULL_REPORT_TASK)) {
project.tasks."$JACOCO_FULL_REPORT_TASK".dependsOn jacocoTestReportTask
} else {
project.tasks.whenTaskAdded {
if (it.name.contentEquals(JACOCO_FULL_REPORT_TASK)) {
it.dependsOn jacocoTestReportTask
}
}
}
def findOrCreateJacocoTestReportTask() {
Task jacocoTestReportTask = project.tasks.findByName("jacocoTestReport")
if (!jacocoTestReportTask) {
jacocoTestReportTask = project.task("jacocoTestReport") {
group = "reporting"
}
}
return jacocoTestReportTask
}
def getVariants() {
if (project.android.hasProperty('libraryVariants')) {
return project.android.libraryVariants
} else {
return project.android.applicationVariants
}
}
def createReportTask(def variant) {
def sourceDirs = sourceDirs(variant)
def classesDir = classesDir(variant)
def testTask = testTask(variant)
def executionData = executionDataFile(testTask)
return project.task("jacoco${testTask.name.capitalize()}Report", type: JacocoReport) { JacocoReport reportTask ->
reportTask.dependsOn testTask
reportTask.group = "reporting"
reportTask.description = "Generates Jacoco coverage reports for the ${variant.name} variant."
reportTask.executionData = project.files(executionData)
reportTask.sourceDirectories = project.files(sourceDirs)
reportTask.classDirectories = classesDir
reportTask.reports {
csv.enabled false
html.enabled true
xml.enabled true
}
}
}
def sourceDirs(variant) {
variant.sourceSets.java.srcDirs.collect { it.path }.flatten()
}
def classesDir(variant) {
def filters = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/Manifest*.*',
'**/*Dagger*.*',
'**/*MembersInjector*.*',
'**/*_Provide*Factory*.*',
'**/*_Factory*.*' ]
def javaTree = fileTree(dir: variant.javaCompile.destinationDir, excludes: filters)
def kotlinTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variant}", excludes: filters)
files([ javaTree ], [ kotlinTree ])
}
def testTask(variant) {
project.tasks.withType(Test).find { task -> task.name =~ /test${variant.name.capitalize()}UnitTest/ }
}
def executionDataFile(Task testTask) {
testTask.jacoco.destinationFile.path
} UPDATE: Yet, seeing the jacoco bugtracker, it seems inlined methods are not being tracked (but affect the coverage), so be prepared to have a lower coverage than the one you expect |
Another option is to use the default tasks, and reconfigure them:
|
none of those works if project only have kotlin in test but java for the rest |
hey everyone, I've struggled a lot configuring apply plugin: "jacoco"
jacoco {
toolVersion = deps.test.jacocoVersion
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
task('jacocoReports') {
group = "Reporting"
description = "Generate Jacoco coverage reports for all variants"
}
variants().all { variant ->
def variantName = variant.name
def variantCapName = variant.name.capitalize()
def variantTask = task("jacoco${variantCapName}Report", type: JacocoReport, dependsOn: "test${variantCapName}UnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports for $variantCapName"
reports {
xml.enabled = true
html.enabled = true
csv.enabled = false
}
def fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',
'**/di/**',
'**/*Controller*.*', '**/*Navigator*.*', '**/*Layout*.*',
'**/*Store$State.*'
]
def classTree = fileTree(
dir: variant.javaCompiler.destinationDir,
excludes: fileFilter
) + fileTree(
dir: "$buildDir/tmp/kotlin-classes/$variantName",
excludes: fileFilter
)
sourceDirectories = files([
"src/main/java", "src/main/kotlin",
"src/$variantName/java", "src/$variantName/kotlin"
])
classDirectories = files([classTree])
executionData = files("${buildDir}/jacoco/junitPlatformTest${variantCapName}.exec")
}
jacocoReports.dependsOn variantTask
}
check.dependsOn jacocoReports
def variants() {
if (project.android.hasProperty('libraryVariants')) {
return project.android.libraryVariants
} else {
return project.android.applicationVariants
}
} Also, it solves #23 |
Once more, I've reviewed the whole coverage approach and found out there is no reason to exclude something from report but rather set custom violation rules for particular packages/classes. Here is the resulting config https://gist.github.com/almozavr/3cd46c4b4f0babbfc136db91924ab56e Sorry for an offtopic, but again would like to share a working config for android+jacoco link |
This tutorial provided a simpler way to make coverage work for Kotlin and/or Java projects: |
Fixed in 0.1.4 release. |
I've configured the jacoco plugin as directed in the instructions. However, the code coverage report does not include any classes written in Kotlin. The remainder of the report shows exactly what i'd expect it to, so in general, the integration works fine, with the exception of no coverage numbers for kotlin.
The text was updated successfully, but these errors were encountered: