Related
Below is my sonarqube properties snippet:
sonarqube {
properties{
property "sonar.junit.reportPaths", "build/test-results/testDebugUnitTest/*.xml"
property("sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacocoTestReport.xml"
}
}
Jacoco configuration and properties are working fine, how did I confirm this?
I created a java class and wrote a unit test for it, sonarqube recognises this and recorded it as part of the Code Coverage, while it basically ignore all the Kotlin file test.
I went ahead to change a kotlin file to java and also write a UnitTest for it and yes, it got recorgnised and add as part of the Code Coverage, again, the kotlin file tests were ignored.
Below is my Jacoco.gradle by the way:
apply plugin: 'jacoco'
ext {
coverageExclusions = [
'**/*Activity*.*',
'**/*Fragment*.*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
]
}
jacoco {
toolVersion = '0.8.6'
reportsDir = file("$buildDir/reports")
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
tasks.withType(Test) {
finalizedBy jacocoTestReport // report is always generated after tests run
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group = "Reporting"
description = "Generate Jacoco coverage reports for Debug build"
reports {
xml.enabled(true)
html.enabled(true)
xml.destination(file("build/reports/jacocoTestReport.xml"))
}
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: coverageExclusions)
def mainSrc = "/src/main/java"
additionalSourceDirs.from = files(mainSrc)
sourceDirectories.from = files([mainSrc])
classDirectories.from = files([debugTree])
executionData.from = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
After so much research and debugging, and also with info's from #LarryX answer above, I was able to play around the classes as he said, below is my working Jacoco.gradle file.
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.4"
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
project.afterEvaluate {
(android.hasProperty('applicationVariants')
? android.'applicationVariants'
: android.'libraryVariants')
.all { variant ->
def variantName = variant.name
def unitTestTask = "test${variantName.capitalize()}UnitTest"
def androidTestCoverageTask = "create${variantName.capitalize()}CoverageReport"
tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
"$unitTestTask",
"$androidTestCoverageTask"
]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"
reports {
html.enabled(true)
xml.enabled(true)
csv.enabled(true)
}
def excludes = [
// data binding
'android/databinding/**/*.class',
'**/android/databinding/*Binding.class',
'**/android/databinding/*',
'**/androidx/databinding/*',
'**/BR.*',
// android
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
// butterKnife
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
// dagger
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class',
'**/di/module/*',
'**/*_Factory*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
// kotlin
'**/*MapperImpl*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*Component*.*',
'**/*BR*.*',
'**/Manifest*.*',
'**/*$Lambda$*.*',
'**/*Companion*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
'**/*MembersInjector*.*',
'**/*_MembersInjector.class',
'**/*_Factory*.*',
'**/*_Provide*Factory*.*',
'**/*Extensions*.*',
// sealed and data classes
'**/*$Result.*',
'**/*$Result$*.*'
]
def javaClasses = fileTree(dir: variant.javaCompileProvider.get().destinationDir,
excludes: excludes)
def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}",
excludes: excludes)
classDirectories.setFrom(files([
javaClasses,
kotlinClasses
]))
def variantSourceSets = variant.sourceSets.java.srcDirs.collect { it.path }.flatten()
sourceDirectories.setFrom(project.files(variantSourceSets))
def androidTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])
executionData(files([
"$project.buildDir/jacoco/${unitTestTask}.exec",
androidTestsData
]))
}
}
}
Take note of :
def javaClasses = fileTree(dir: variant.javaCompileProvider.get().destinationDir,
excludes: excludes)
def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}",
excludes: excludes)
classDirectories.setFrom(files([
javaClasses,
kotlinClasses
]))
Also, take note of project.afterEvaluate I'm not sure this is relevant but cause I think you must have run and generated your test before this section runs anyways.
This solution also works for a library if not an actual android application.
And then my sonarqube.gradle file below.
apply plugin: "org.sonarqube"
sonarqube {
properties {
property "sonar.host.url", "http://localhost:9000/"
property "sonar.projectKey", "fair"
property "sonar.projectName", "fair"
property "sonar.login", "de9d79fe4d3aef9879567afc91a2ce465038d9be"
property "sonar.projectVersion", "${android.defaultConfig.versionName}"
property "sonar.junit.reportsPath", "build/test-results/testDebugUnitTest"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.android.lint.report", "build/reports/lint-results.xml"
property "sonar.jacoco.reportPaths", "build/jacoco/testDebugUnitTest.exec"
property "sonar.jacoco.itReportPath", fileTree(dir: project.projectDir, includes: ["**/*.ec"])
property "sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacoco/testDebugUnitTestCoverage/testDebugUnitTestCoverage.xml"
}
}
tasks.sonarqube.dependsOn ":app:testDebugUnitTestCoverage"
Finally my build.gradle(project)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
classpath "org.jacoco:org.jacoco.core:0.8.7"
classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.3")
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Please check jacoco config for java + kotlin project:
http://vgaidarji.me/blog/2017/12/20/how-to-configure-jacoco-for-kotlin-and-java-project/
Key is to include kotlin directories in sourceDirectories and classeDirectories
I try to display the tests coverage in SonarQube of my Android Kotlin app of the code in my app module. I can generate the jacoco coverage results and display SonarQube qualymetrics but the problem is that the tests coverage is not displayed in SonarQube :
https://imgur.com/a/xOjxLl1
in my build.gradle of my project I have :
...
dependencies {
classpath 'org.jacoco:org.jacoco.core:0.8.3'
}
...
in my build.gradle of my app module
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.8.3'
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled true
}
debug {
testCoverageEnabled true
}
testOptions {
animationsDisabled true
unitTests {
returnDefaultValues = true
includeAndroidResources = true
}
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*' ]
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
}
sonarqube {
properties {
property 'sonar.projectKey', 'mySonarKey'
property 'sonar.projectName', 'myProjectName'
property "sonar.host.url", "http://localhost:9000" // local sonar address
property "sonar.java.binaries", 'target/classes,app/build/tmp/kotlin-classes'
property "sonar.sources", 'src/main/java'
property "sonar.tests", 'src/androidTest/java'
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.coverage.jacoco.xmlReportPaths", "./build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml" //link to xml file that generated by jacoco
}
}
...
I generate my jacoco report with :
gradlew clean jacocoTestReport
And generate my sonarqube analysis with :
gradlew sonarqube
the jacoco report is generated myAppName\app\build\reports\jacoco\jacocoTestReport\jacocoTestReport.xml
I also have an html folder next to the report where I can see that my test coverage generated with the right coverage.
So the question is what can I change to display my generated jacoco coverage in SonarQube ?
(I'm not sure about the sonarqube.properties that I add in my app module build.gradle)
I had the same issue for a long time and the solution is to change your path in the build.gradle :
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
To :
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/snapshot", excludes: fileFilter)
Otherwise, the .xml generated won't include the coverage for your whole source code and sonarqube will display 0% coverage as a result
Note : my target folder was called 'snapshot' but it might be different for you
I have an Android app which is made up of 2 modules:
App - UI
Submodule - has most of the business logic
For each of them I have a gradle task to validate code coverage:
App: UI Code coverage (Espresso)
Submodule: Unit tests code coverage
As a requirement for the client, I need to merge those two reports to get the overall/global code coverage of the app.
Note: I'm using Gradle version 3.1.2.
App Gradle file:
apply plugin: 'jacoco'
android {
testBuildType "uiTest"
...
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
debuggable true
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
matchingFallbacks = ['debug']
}
// TESTS
// unitTest will be used to run unit tests.
unitTest.initWith(buildTypes.debug) //Beware the buildType this depends on MUST be above on the gradle file
unitTest {
applicationIdSuffix ".unitTest"
versionNameSuffix "-unitTest"
testCoverageEnabled = true
matchingFallbacks = ['unitTest', 'debug']
}
// uiTest will be used to run ui tests.
uiTest.initWith(buildTypes.debug) //Beware the buildType this depends on MUST be above on the gradle file
uiTest {
applicationIdSuffix ".uiTest"
versionNameSuffix "-uiTest"
testCoverageEnabled = true
matchingFallbacks = ['uiTest', 'debug']
}
...
SubModule Gradle file:
apply plugin: 'jacoco'
android {
testBuildType "uiTest"
buildTypes {
debug {
}
unitTest {
initWith(buildTypes.debug)
testCoverageEnabled = true
}
uiTest {
initWith(buildTypes.debug)
testCoverageEnabled = true
}
...
}
I've tried several ways, this one below indeed merges the tests.. but the coverage is not appearing correctly:
The task for creating UI Test coverage in the app:
//UI Test Coverage filtered (we need to run unit tests of App to be able to use Jacoco to filter)
task createTestReport(type: JacocoReport, dependsOn: [':app:testUnitTestUnitTest', ':app:createUiTestAndroidTestCoverageReport']) {
reports {
html.enabled = true
}
def fileFilter = [
//Android stuff
'**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BR$*.class',
'**/BuildConfig.*',
'android/**/*.*',
//Data Binding
'**/*databinding',
'**/*binders',
'**/*layouts',
'**/Manifest*.*',
'**/*Test*.*',
"**/services/**/model/**",
//Utils
'**/utils/*.*',
'**/utils/**/*.*'
]
//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"
sourceDirectories = files([mainSrc, debugSrc])
def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["**/*.ec"])
def appOtherAndroidTests = fileTree(dir: "${buildDir}/outputs/androidTest-results/connected/", includes: ["**/*.ec"])
classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec", appAndroidTests, appOtherAndroidTests)
}
The task for creating Unit test coverage in the sub-module:
//Unit Test Coverage filtered
task createTestReport(type: JacocoReport, dependsOn: ['testUnitTestUnitTest']) {
reports {
html.enabled = true
}
def fileFilter = ['**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BR$*.class',
'**/BuildConfig.*',
'**/*databinding/**/*.*',
'**/Manifest*.*',
'**/*Test*.*',
"**/services/**/model/**",
'android/**/*.*',
'**/utils/*.*',
'**/utils/**/*.*']
//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"
sourceDirectories = files([mainSrc, debugSrc])
classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec")
}
The task for creating Global coverage in the app:
//Global Test Coverage
task createGlobalTestReport(type: JacocoReport,
dependsOn: [':app:testUnitTestUnitTest', ':app:createTestReport',
':submodule:testUnitTestUnitTest']) {
reports {
html.enabled = true
}
def fileFilter = [
//Android stuff
'**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BR$*.class',
'**/BuildConfig.*',
'android/**/*.*',
//Data Binding
'**/*databinding',
'**/*binders',
'**/*layouts',
'**/Manifest*.*',
'**/*Test*.*',
"**/services/**/model/**",
//Utils
'**/utils/*.*',
'**/utils/**/*.*'
]
// Note: **/reviews/ReviewService*.* was added as BazaarVoice cannot be mocked
//To support Java coverage on Unit tests
def debugAppTree = fileTree(dir: "${buildDir}/intermediates/classes/unitTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def debugKotlinAppTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/unitTest", excludes: fileFilter)
def debugSdkTree = fileTree(dir: "..//build/intermediates/classes/unitTest", excludes: fileFilter)
def debugKotlinSdkTree = fileTree(dir: "../submodule/build/tmp/kotlin-classes/unitTest", excludes: fileFilter)
def mainAppSrc = "${project.projectDir}/src/main/java"
def debugAppSrc = "${project.projectDir}/src/debug/java"
def mainSdkSrc = "../submodule/src/main/java"
def debugSdkSrc = "../submodule/src/debug/java"
sourceDirectories = files([mainAppSrc, debugAppSrc,
mainSdkSrc, debugSdkSrc])
def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["**/*.ec"])
def sdkAndroidTests = fileTree(dir: "../submodule/build/outputs/code-coverage/connected/", includes: ["**/*.ec"])
classDirectories = files([debugAppTree, debugSdkTree,
debugKotlinAppTree, debugKotlinSdkTree])
executionData = files("${buildDir}/jacoco/testUnitTestUnitTest.exec"
, "../submodule/build/jacoco/testUnitTestUnitTest.exec"
, appAndroidTests
, sdkAndroidTests
)
}
Any help would be much appreciated
I'm not sure it works with multiple build types.
Try merging it into a single build type and:
App Gradle file:
apply plugin: 'jacoco'
android {
testBuildType "automationTest"
...
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-debug"
debuggable true
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
matchingFallbacks = ['debug']
}
// TESTS
automationTest {
applicationIdSuffix ".automationTest"
versionNameSuffix "-automationTest"
testCoverageEnabled = true
matchingFallbacks = ['automationTest', 'debug']
}
...
SubModule Gradle file:
apply plugin: 'jacoco'
android {
buildTypes {
debug {
}
automationTest {
initWith(buildTypes.debug)
testCoverageEnabled = true
}
release {
initWith(buildTypes.debug)
}
...
}
The task for creating Unit test coverage in the sub-module:
task createUnitTestReport(type: JacocoReport, dependsOn: ['testAutomationTestUnitTest']) {
reports {
html.enabled = true
}
def fileFilter = ['**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BR$*.class',
'**/BuildConfig.*',
'**/*databinding/**/*.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*']
//To support Java coverage on Unit tests
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/automationTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/automationTest", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
def debugSrc = "${project.projectDir}/src/debug/java"
sourceDirectories = files([mainSrc, debugSrc])
classDirectories = files([debugTree], [kotlinDebugTree])
executionData = files("${buildDir}/jacoco/testAutomationTestUnitTest.exec")
}
The task for creating Global coverage in the app:
task createGlobalTestReport(type: JacocoReport,
dependsOn: [':app:createUiTestReport', ':submodule:createUnitTestReport']) {
reports {
html.enabled = true
}
def fileFilter = [
//Android stuff
'**/R.class',
'**/BR.class',
'**/R$*.class',
'**/BR$*.class',
'**/BuildConfig.*',
'android/**/*.*',
//Data Binding
'**/*databinding',
'**/*binders',
'**/*layouts',
'**/Manifest*.*',
'**/*Test*.*'
]
//To support Java coverage on Unit tests
def debugAppTree = fileTree(dir: "${buildDir}/intermediates/classes/automationTest", excludes: fileFilter)
//To support Kotlin coverage on Unit tests
def debugKotlinAppTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/automationTest", excludes: fileFilter)
def debugSdkTree = fileTree(dir: "../submodule/build/intermediates/classes/automationTest", excludes: fileFilter)
def debugKotlinSdkTree = fileTree(dir: "../submodule/build/tmp/kotlin-classes/automationTest", excludes: fileFilter)
def mainAppSrc = "${project.projectDir}/src/main/java"
def debugAppSrc = "${project.projectDir}/src/debug/java"
def mainSdkSrc = "../submodule/src/main/java"
def debugSdkSrc = "../submodule/src/debug/java"
sourceDirectories = files([mainAppSrc, debugAppSrc,
mainSdkSrc, debugSdkSrc])
classDirectories = files([debugAppTree, debugSdkTree,
debugKotlinAppTree, debugKotlinSdkTree])
def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code-coverage/connected/", includes: ["*.ec"])
executionData = files("${buildDir}/jacoco/testAutomationTestUnitTest.exec"
, "../submodule/build/jacoco/testAutomationTestUnitTest.exec"
, appAndroidTests
)
}
I use a plugin by palantir to create aggregate coverage reports for my different modules - com.palantir.jacoco-full-report
Basically, in your root gradle file, you need to add this in your dependencies:
classpath 'com.palantir:jacoco-coverage:0.4.0'
After that, when you run ./gradlew test jacocoFullReport a test report in build/reports/jacoco/jacocoFullReport/ that evaluates the coverage yielded by all subprojects combined is created.
I can setup spoon with:
spoon {
//...
codeCoverage = true
}
to generage coverage.ec files.
How can I generate the reports in .xml or .html?
You can do this creating your own task using the built-in Jacoco Gradle plugin:
apply plugin: 'jacoco'
task jacocoTestReport(type: JacocoReport, dependsOn: ['<taskThatProducesEcFile>']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', <another filters...>]
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files([<your_path_to_ec_file>])
}
I currently am using the createDebugAndroidTestCoverageReport to run my android instrumentation tests and generate a coverage report. The only issue that I am running into is that there are packages that are generated from Realm and Databinding and these classes are also being included in my coverage report. How can I configure jacoco to exclude these packages?
test {
jacoco {
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpFile = file("$buildDir/jacoco/classpathdumps")
excludes = []
}
}
Click here for more details on official Jacoco Gradle Plugin
When you are defining the debugTree you can add the fileFilters to be excluded.
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/package/**']
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
}