prashanth-g
prashanth-g

Reputation: 1233

Gradle jacocoTestReport not producing report

I have tried to get code coverage in a spring-gradle project using gradle jacoco plugin.

The build.gradle contains the following

apply plugin: "jacoco"

jacoco {
  toolVersion = "0.7.1.201405082137"
  reportsDir = file("$buildDir/customJacocoReportDir")
}
    
jacocoTestReport {
  reports {
    xml.enabled false
    csv.enabled false
    html.destination "${buildDir}/jacocoHtml"
  }
}

I then ran

gradle test jacocoTestReport

Where after only the file test.exec is generated in build/reports folder.

Other than that nothing happens.

How can I get the HTML report?

Upvotes: 30

Views: 74903

Answers (5)

Mugeesh Husain
Mugeesh Husain

Reputation: 428

build.gradle file using gradle 8.2

sonar {
  properties {
      property 'sonar.host.url', 'http://localhost:9000'
      property 'sonar.login', 
      'squ_b715dcf08655b02c3e32ae1ad0d6987ab239ec64'
      property 'sonar.verbose', true
      property 'sonar.qualitygate.wait', true
      property 'sonar.projectKey', 'sonarqube-jacoco-code-coverage'
  }
}

jacocoTestReport {
  reports {
     xml.required = true
  }
}
test.finalizedBy jacocoTestReport

tasks.named('sonar').configure {
 dependsOn test
}

tasks.named('test') {
  useJUnitPlatform()
}

Upvotes: 0

M. Amer
M. Amer

Reputation: 1136

Unfortunately, none of these answers worked for me.

I had a similar issue.
Only different in not having the exec file generated.
And because of that , I found that the jacocoTestReport was simply "skipped".

I got it fixed by adding :

apply plugin: 'jacoco'

test {
  useJUnitPlatform()
  finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
    ...
    ...
    ...
    ...
}

That's because I'm using Junit5 with spring boot 2.X

Upvotes: 5

Ashwini Mutalik Desai
Ashwini Mutalik Desai

Reputation: 91

subprojects {
    apply(plugin: 'org.jetbrains.kotlin.jvm')

    repositories {
        jcenter()
        mavenCentral()
   }
}

task codeCoverageReport(type: JacocoReport) {

    // Gather execution data from all subprojects
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects
    subprojects.each {
        sourceSets it.sourceSets.main
    }

    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport.dependsOn {
    subprojects*.test
}

sonarqube {
    properties {
        property "sonar.projectKey", "your_project_key"
        property "sonar.verbose", true
        property "sonar.projectName", "Your project name"
        property "sonar.coverage.jacoco.xmlReportPaths", "${rootDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
    }
}

Command to run test with coverage:

./gradlew codeCoverageReport
./gradlew sonarqube -x test (test is excluded since already run and sonarqube by default executes test)

The second command can be ignored if sonarqube is not being used.

Two things to be noted that made it work:

  1. To make available sourcesets of all modules, looping over subprojects and accumulating sourcesets worked. subprojects.sourceSets.main.allSource.srcDirs did not work.
  2. sonar.jacoco.reportPaths is deprecated. We need to use sonar.coverage.jacoco.xmlReportPaths. Check the documentation here

Upvotes: 1

prashanth-g
prashanth-g

Reputation: 1233

Following helped . its in samples/testing/jacaco of gradle-2.3-all.zip from https://gradle.org/releases/

apply plugin: "java"

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.7.1.201405082137"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile "junit:junit:4.+"
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}


jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

Upvotes: 18

Tyler Liu
Tyler Liu

Reputation: 20356

You don't have to configure reportsDir/destinationFile

Because jacoco has default values for them.

build.gradle:

plugins {
    id 'java'
    id 'jacoco'
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled true
    }
}

repositories {
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Run gradle test jacocoTestReport

You can find the test report in ./build/reports/jacoco/test directory.

HTML output is in ./build/reports/jacoco/test/html directory.

Upvotes: 21

Related Questions