Friday, 16 April 2021

Code coverage for Kotlin files in SonarQube

EDIT: Actually SonarQube is not reporting the coverage because Jacoco it self is not showing that in it's report, I was seeing some coverage for the package I was checking and realized those are just the Java files.

I am using Jacoco to get the code coverage report, the report seems fine for all the Java files, for the newly added Kotlin files, it is showing 0%. Locally when I run the task jacocoTestReport, the report at /Users/muser/AndroidProjects/app-android/app/build/reports/jacoco/jacocoTestReport/html shows proper coverage report for Java files but not the Kotlin files (attached at the end) under the same package

These are the configurations in build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "org.sonarqube"
apply plugin: 'jacoco'


jacoco {
    toolVersion = "0.8.6"
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testMobileDebugUnitTest', 'createMobileDebugCoverageReport']) {

    reports {
        xml.enabled = true
        html.enabled = true
        xml.destination file("$project.buildDir/reports/jacoco.xml")
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def javaClasses = fileTree(dir: "$project.buildDir/intermediates/javac/mobileDebug", excludes: fileFilter)
    def kotlinClasses = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/mobileDebug}", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"
    def mobileSrc = "$project.projectDir/src/mobile/java"
    sourceDirectories.setFrom(files([mainSrc], [mobileSrc]))
    classDirectories.setFrom(files([javaClasses], [kotlinClasses]))
    executionData.setFrom(fileTree(dir: project.buildDir, includes: ['jacoco/testMobileDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec']))
}

android {
    …
    …
    tasks.sonarqube.dependsOn(jacocoTestReport)

    sonarqube {
        properties {
                def appProject = project(':app')
                property "sonar.projectName", projectName
                property "sonar.projectKey", projectKey
                property "sonar.host.url", “…”
             property "sonar.login", “…”
                property "sonar.password", “…”
                property "sonar.sources", "src/main/java"
                property "sonar.projectVersion", appProject.android.defaultConfig.versionName + appProject.android.defaultConfig.versionCode

                property "sonar.projectDescription", “Android Mobile App“
                property "sonar.java.binaries", "build/intermediates/javac/mobileDebug/classes/com/…”
                property "sonar.tests", "src/test/java, src/testMobile/java"
                property "sonar.exclusions", “…”
                property "sonar.coverage.exclusions", “…”

                property "sonar.java.coveragePlugin", "jacoco"
                property "sonar.coverage.jacoco.xmlReportPaths", "$project.buildDir/reports/jacoco.xml"
                property "sonar.android.lint.report", "build/outputs/lint-results-debug.xml"
            }
    }
}

What I changed recently is - include Kotlin support and add new Kotlin files. And in build.gradle:

Removed property "sonar.language", "java"

Changed

def javaClasses = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
classDirectories.setFrom(files([javaClasses]))

To

def javaClasses = fileTree(dir: "$project.buildDir/intermediates/javac/mobileDebug", excludes: fileFilter)
def kotlinClasses = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/mobileDebug}", excludes: fileFilter)
classDirectories.setFrom(files([javaClasses], [kotlinClasses]))

Changed

property "sonar.jacoco.reportPaths", "build/jacoco/testMobileDebugUnitTest.exec"

To

property("sonar.coverage.jacoco.xmlReportPaths", "$project.buildDir/reports/jacoco.xml")

Also my SonarQube version on web is Version 4.4.05142

The Jacoco report on my local machine shows coverage only for Java files, Kotlin files are not even shown in the list. It should have at least showed the files with 0% coverage. All the files shown below are only Java files

enter image description here



from Code coverage for Kotlin files in SonarQube

No comments:

Post a Comment