gradle jacocoTestReport is not working? - gradle

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?

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.

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"
}
}

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

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:
To make available sourcesets of all modules, looping over subprojects and accumulating sourcesets worked. subprojects.sourceSets.main.allSource.srcDirs did not work.
sonar.jacoco.reportPaths is deprecated. We need to use sonar.coverage.jacoco.xmlReportPaths. Check the documentation here

Related

jacocoTestReport always SKIPPED

Expected Behavior
jacocoTestReport work
Current Behavior
jacocoTestReport SKIPPED
Context
I created a task whose type is Test, and jacocoTestReport depends on the task. When I ran the task, jacocoTestReport did not work and I got the following information
jacocoTestReport SKIPPED
I find if I use the task test directly, jacocoTestReport worked fine. It makes me confused
the following code caused the above issue
plugins {
id 'java'
id 'jacoco'
}
repositories {
mavenCentral()
}
dependencies {
}
task myTest(type: Test) {
useTestNG()
useJUnitPlatform()
finalizedBy jacocoTestReport
reports {
junitXml.required = false
html.required = true
}
jacoco {
enabled = true
destinationFile = layout.buildDirectory.file("jacoco/${name}.exec").get().asFile
}
}
jacocoTestReport {
// tests are required to run before generating the report
dependsOn myTest
reports {
xml.required = false
csv.required = false
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
}
Steps to Reproduce
example project
Your Environment
Gradle 7.1.1
It is expected, not an issue. just because I use the jacocoTestReport in a wrong way
see https://github.com/gradle/gradle/issues/18271

jacocoRootReport only shows coverage from last project of multi-project gradle build

I'm upgrading my Gradle 4 multi-project setup to Gradle 6. I've followed instructions here:
https://stackoverflow.com/a/56181389
and I've bumped the jacoco version to 0.8.5. Right now the only problem is that the human-readable coverage report seems to be missing most of the coverage data that it was showing under the old version. It seems that the coverage report is only reflecting the last (most recently tested) project. It used to work fine under Gradle 4. I'm using Java 8.
I ran the gradle build using --debug, and I notice that the test.exec file is deleted repeatedly, once for each subproject that has tests. I think this is the problem, but I don't know how to prevent deletion of this file.
2020-04-16T09:16:21.048-0600 [DEBUG] [org.gradle.internal.file.impl.DefaultDeleter] Deleting /Users/bishop/dev/aep/edge-profile-lookup-target/build/jacoco/test.exec
Can someone please help me fix this so that all of the coverage (from each of the tests which ran against each of the sub projects) appear in a single coverage report?
Here are the parts of the main build.gradle file that seem relevant:
buildscript {
ext {
jacocoVersion = '0.8.5'
...
}
...
}
allprojects {
...
apply plugin: 'jacoco'
...
}
subprojects {
tasks.withType(Test) {
// redirect all coverage data to one file
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
}
jacoco {
toolVersion = jacocoVersion
}
jacocoTestReport {
additionalSourceDirs.from = files(sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(sourceSets.main.allSource.srcDirs)
classDirectories.from = files(sourceSets.main.output.collect {
fileTree(dir: it, exclude: project.properties['BUILD_COVERAGE_EXCLUSIONS'].tokenize(','))
})
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
}
}
task jacocoRootReport(type: JacocoReport) {
dependsOn = subprojects.test
additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(subprojects.jacocoTestReport.classDirectories)
executionData.from = files(subprojects.jacocoTestReport.executionData)
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
onlyIf = {
true
}
doFirst {
executionData.from = files(executionData.findAll {
it.exists()
})
}
}
...
apply plugin: 'jacoco'
configurations.create("jacoco")
configurations {
jacoco
}
dependencies {
jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: jacocoVersion
jacocoAnt group: 'org.ow2.asm', name: 'asm', version: asmVersion
}
task copyRuntimeLibs(type: Copy) {
from configurations.jacoco
into "$rootProject.buildDir/libs"
}
build.dependsOn copyRuntimeLibs
The following:
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
configures Jacoco to always use the same output file.
So the issue is most likely that more recent Gradle versions work on separating colliding outputs.
I would recommend looking at the recently published sample on how to setup Jacoco in a multi project instead of attempting to rely on colliding outputs.

Gradle Multi-Project Integration Tests with Junit5

How can I create integrations tests using Gradle 5.2 and JUnit 5.3 in a multi-project build file?
This is my current build file:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.10"
classpath 'org.owasp:dependency-check-gradle:5.0.0-M2'
}
}
allprojects {
defaultTasks 'clean', 'build', 'publish', 'installDist'
group = 'coyote'
version = '0.1.0'
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: "com.github.spotbugs"
apply plugin: 'org.owasp.dependencycheck'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'
repositories {
jcenter()
mavenCentral()
}
dependencies {
testImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
spotbugsPlugins ("com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0")
}
tasks.withType(Test) {
useJUnitPlatform()
}
apply from: "$rootDir/integration-test.gradle"
check.dependsOn jacocoTestCoverageVerification
check.dependsOn dependencyCheckAnalyze
spotbugs {
effort = "max"
reportLevel = "low"
ignoreFailures = true
showProgress = true
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
check.dependsOn jacocoTestReport
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
}
The integration-test.gradle file I am applying on line 46 contains the following:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}
Everything seems to work well within Intellij, but JUnit5 is not being added to the classpath, so any tests in the itest directories cannot find the JUnit libraries.
Running gradlew itest fails with similar results with the JUnit classes not being found.
I have tried to add use useJUnitPlatform() directly in the itest task, but with no success. I have also tried placing everything in the build.gradle file with no success.
Eventually, I'd like to use the same pattern to define load and security testing, running them separately as part of a CI/CD pipeline so placing everything neatly in their own directories under their respective projects is preferred to mixing everything in one test directory and using tags.
This is also helping other teams model CI/CD practices, so using Gradle 5 and JUnit 5 as designed is preferred as opposed to work-arounds or hacks to make things work. It's acceptable to learn that these versions don't work together or that there are currently defects\issues preventing this approach. Hopefully this is not the issue and I'm just missing something simple.
The solution is I needed to include the JUnit 5 dependencies myself. The following is the correct integration-test.gradle file:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
dependencies {
itestImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
itestRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}

CorDapp JaCoCo Code Coverage

I have a Corda based project with several CorDapp sub projects. I've been looking to add JaCoCo code coverage to this project. I'm looking to have a single code coverage report draw in an aggregate report of all the subproject JaCoCo reports.
To add JaCoCo to a maven project with several maven sub projects, I followed this blog entry https://lkrnac.net/blog/2016/10/aggregate-test-coverage-report/. After we ran the build ./gradlew clean test and got our reports, one of our team members noted that the whitelists weren't being created properly anymore when we ran ./gradlew clean deployNodes.
I've gone back to the base Kotlin CorDapp template found here https://github.com/corda/cordapp-template-kotlin to rule out if it's something we've done wrong with our project structure/gradle. Without JaCoCo added, I see all the whitelist entries I would expect. Once I add the JaCoCo code, I only see the 5 default Corda whitelist entries, and none of my added contract entries.
I'm using JaCoCo version 0.8.1 and coveralls version 2.6.3. The changes I've made are all within the build.gradle file for the root directory cordapp-template-kotlin:
subprojects {
repositories {
mavenCentral()
}
apply plugin: 'jacoco'
apply plugin: 'java'
group = 'net.lkrnac.blog'
version = '1.0-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
dependencies {
testCompile("junit:junit:4.12")
}
jacoco {
toolVersion = jacoco_version
}
//command for generating subproject coverage reports
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination file("${buildDir}/jacocoHtml")
}
}
}
def publishedProjects = subprojects.findAll()
task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
dependsOn(publishedProjects.test)
additionalSourceDirs = files(publishedProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories = files(publishedProjects.sourceSets.main.allSource.srcDirs)
classDirectories = files(publishedProjects.sourceSets.main.output)
executionData = files(publishedProjects.jacocoTestReport.executionData)
doFirst {
executionData = files(executionData.findAll { it.exists() })
}
reports {
html.enabled = true // human readable
xml.enabled = true // required by coveralls
}
}
coveralls {
sourceDirs = publishedProjects.sourceSets.main.allSource.srcDirs.flatten()
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml"
}
tasks.coveralls {
dependsOn jacocoRootReport
}
I believe that the problem is coming from simply adding a task where JacocoReport as a parameter. Any thoughts how I could proceed to have both code coverage, along with building my whitelists correctly?
I have managed to find how to fix the coverage/whitelisting issue. I started stripping away what seemed to be unnecessary code within the subprojects spec, and found that removing everything except the apply plugin:, jacoco, and jacocoTestReport commands yielded both the root Jacoco code coverage, along with the necessary whitelisting. I didn't need to change any of the other code above to get the whitelisting to work.
For reference, subprojects now looks like this:
subprojects {
apply plugin: 'jacoco'
apply plugin: 'kotlin'
jacoco {
toolVersion = jacoco_version
}
//command for generating subproject coverage reports
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination file("${buildDir}/jacocoHtml")
}
}
}

Allure report is empty when use Allure2+Junit5+Gradle+Selenide

My build.gradle is:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'io.qameta.allure'
defaultTasks 'clean', 'test'
ext.junitJupiterVersion = '5.0.0-M4'
ext.selenideVersion = '4.4.3'
compileTestJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
options.encoding = 'UTF-8'
options.compilerArgs += "-parameters"
}
compileJava.options.encoding = 'UTF-8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
jcenter()
mavenCentral()
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
classpath 'io.qameta.allure:allure-gradle:2.3'
}
}
allure {
aspectjweaver = true
autoconfigure = true
version = '2.1.1'
}
configurations {
agent
}
dependencies {
// JUnit5
compile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
compile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
// Selenide
compile("com.codeborne:selenide:${selenideVersion}") {
exclude group: 'junit'
}
// Allure
agent 'org.aspectj:aspectjweaver:1.8.10'
compile 'ru.yandex.qatools.allure:allure-junit-adaptor:1.4.23'
compile 'io.qameta.allure:allure-junit5:2.0-BETA6'
}
junitPlatform {
platformVersion = "1.0.0-M5"
enableStandardTestTask = true
}
task runJupiter(type: JavaExec) {
jvmArgs '-ea'
jvmArgs "-javaagent:${configurations.agent.singleFile}"
classpath = project.sourceSets.test.runtimeClasspath
main 'org.junit.platform.console.ConsoleLauncher'
args '--scan-class-path'
args "--reports-dir=${buildDir}/allure-results"
finalizedBy 'allureReport'
}
test.dependsOn runJupiter
Tests are finished successfully and three folders are created automatically:
{projectDir}\allure-results with .json file
{projectDir}\build\test-results\junit-platform with TEST-junit-jupiter.xml file
{projectDir}\build\reports\allure-report
I tried to open .json and .xml result locally via allure command line (CLI). The allure report is opened but it is blank:
this is a report view
I suppose my mistake in gradle dependencies. I quite confused which libraries and versions should be used for JUnit5+Allure2+Gradle+Selenide+Java8?
The JUnit Platform Gradle plugin does currently not use the test task (it needs changes in Gradle core in order to do so). Thus, things like test.doFirst {...} are not going to work.
Instead of using the plugin, you should be able to create your own task that runs the ConsoleLauncher and add the JVM agent there. See https://stackoverflow.com/a/43512503/6327046 for an example.

Resources