Include multiple sourcesets in the Jacoco's jacocoTestReport.xml file - gradle

I have a custom sourceSet defined in a Gradle based project, like this:
sourceSets {
componentTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/componentTest/java')
}
}
}
configurations {
componentTestImplementation.extendsFrom testImplementation
}
task componentTest(type: Test) {
dependsOn assemble
testClassesDirs = sourceSets.componentTest.output.classesDirs
classpath = sourceSets.componentTest.runtimeClasspath
outputs.upToDateWhen { false }
testLogging.showStandardStreams = true
mustRunAfter(test)
}
check.dependsOn(componentTest)
The sourceSet contains Cucumber features.
When running Jacoco, the generated jacocoTestReport.xml file doesn't seem to include the coverage generated by the Cucumber tests.
Here is the Jacoco configuration:
jacocoTestReport {
executionData(
file("${project.buildDir}/jacoco/test.exec"),
file("${project.buildDir}/jacoco/componentTest.exec"),
)
}
Is there anything I could do to get the same coverage in the xml file as I get in the exec files?

After further experiments it turns out the xml file includes the coverage from the componentTest sourceSet.
I asked this question because the coverage was not reflected correctly in SonarQube - I could not see the Cucumber tests from the componentTest in SonarQube. But it seems to me now, that the actual coverage values align with what the Jacoco report shows. I just had to exclude the same source classes in SonarQube as in Jacoco.

Related

How to create a Gradle Task to run only a specific Tests in Spring

I have a Spring Project where I wrote some Unit and Integration Tests. Now I want to create custom tasks for running all unit tests, all integration tests and one task for running both. But how can I do this?
I would suggest separating integration tests into separate source set. By default you already have 2 source sets, one for production code and one for tests. To create a new source set (create new directory under src/integrationTest/java) and add a following configuration using Junit5:
test {
useJUnitPlatform()
}
sourceSets {
integrationTest {
java.srcDir file("src/integrationTest/java")
resources.srcDir file("src/integrationTest/resources")
compileClasspath += sourceSets.main.output + configurations.testRuntime
runtimeClasspath += output + compileClasspath
}
}
For separate task:
task integrationTest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
useJUnitPlatform()
reports {
html.enabled true
junitXml.enabled = true
}
}
Now you have 3 tasks available:
gradlew test
gradlew integrationTest
gradlew check - runs both as it depends on Test task which both extend
If you also are using jacoco and want to merge the test results then you can have following tasks:
task coverageMerge(type: JacocoMerge) {
destinationFile file("${rootProject.buildDir}/jacoco/test.exec")
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
}
// aggregate all coverage data at root level
if (tasks.findByName("test")) {
tasks.findByName("test").finalizedBy coverageMerge
}
if (tasks.findByName("integrationTest")) {
tasks.findByName("integrationTest").finalizedBy coverageMerge
}
task codeCoverageReport(type: JacocoReport) {
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
subprojects.each {
if (!it.name.contains('generated')) {
sourceSets it.sourceSets.main
}
}
reports {
xml.enabled true
html.enabled true
html.setDestination(new File("${buildDir}/reports/jacoco"))
csv.enabled false
}
}
To run coverage report just execute
gradlew codeCoverageReport

Spring cloud contracts plugin change sourceset

I've started using Spring Cloud Contracts ('2.0.2.RELEASE') in my project and I have the following structure
src
|
-- main
-- test
-- integrationTest
-- contractTest
When I put my contracts and my base test class in test it was running fine. I want to move the contract tests that I have written into a separate sourceset, the contractTest sources. However, this will not work as the plugin generateContractTests task will still look in the test sourceset when trying to run.
I have made the following changes to my Gradle file
task contractTest(type: Test) {
description = 'Runs contract tests.'
group = 'verification'
testClassesDirs = sourceSets.contractTest.output.classesDirs
classpath = sourceSets.contractTest.runtimeClasspath
shouldRunAfter integrationTest
}
configurations {
contractTestImplementation.extendsFrom implementation
contractTestRuntimeOnly.extendsFrom runtimeOnly
}
sourceSets {
contractTest {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
contracts {
// testFramework = 'JUNIT5'
packageWithBaseClasses = 'com.test.testapi.contracts'
contractsDslDir = new File("${project.rootDir}/src/contractTest/resources/contracts/")
}
contractTestImplementation 'org.codehaus.groovy:groovy-all:2.4.6'
contractTestImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
contractTestImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api'
contractTestImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine'
I think I need to set the contract plugin property contractDependency, however, I am not sure and can't find an example to get the plugin to work with this different sourceset
TLDR; I want to be able to run my contract tests in a different folder
UPDATE - I am not sure but I think that it is not possible as in the Gradle plugin in the "GenerateServerTestsTask.groovy" file has the following which would appear to signify that the sourceSet is hardcoded to test throughout the code
project.sourceSets.test.groovy {
project.logger.
info("Registering ${getConfigProperties().generatedTestSourcesDir} as test source directory")
srcDir getConfigProperties().getGeneratedTestSourcesDir()
}
For future reference, I was able to get it working by creating a custom task to deregister the generated sources from the test sourceset so that they wouldn't be compiled by compileTestJava and could be run via my own contractTests task.
sourceSets {
contractTest {
java {
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
srcDir file('src/contractTest/java')
srcDirs += file("${buildDir}/generated-contract-sources/")
}
resources.srcDir file('src/contractTest/resources')
}
}
task deregisterContractTestSources() {
doLast {
project.sourceSets.test.java {
project.logger.info('Removing any *Spec classes from the test sources')
exclude '**/*Spec*'
}
}
}
compileTestJava.dependsOn deregisterContractTestSources
task contractTests(type: Test) {
description = 'Runs contract tests'
group = 'verification'
testClassesDirs = sourceSets.contractTest.output.classesDirs
classpath = sourceSets.contractTest.runtimeClasspath
}
contracts {
baseClassForTests = 'com'
generatedTestSourcesDir = file("${buildDir}/generated-contract-sources")
generatedTestResourcesDir = file("${buildDir}/generated-contract-resources/")
testFramework = "JUNIT5"
contractsDslDir = new File("${projectDir}/src/contractTest/resources/contracts/")
nameSuffixForTests = 'Spec'
basePackageForTests = 'com'
}

Kotlin Add Integration Tests Module

I am trying to add another module to my Kotlin project specifically for integration tests - living alongside the standard test module creating by the kotlin plugin. Here is my current configuration to add the sourceset:
sourceSets {
testIntegration {
java.srcDir 'src/testIntegration/java'
kotlin.srcDir 'src/testIntegration/kotlin'
resources.srcDir 'src/testIntegration/resources'
compileClasspath += main.output
runtimeClasspath += main.output
}
}
configurations {
provided
testCompile.extendsFrom provided
testIntegrationCompile.extendsFrom testCompile
testIntegrationRuntime.extendsFrom testRuntime
}
task testIntegration(type: Test) {
testClassesDirs = sourceSets.testIntegration.output.classesDirs
classpath = sourceSets.testIntegration.runtimeClasspath
}
This seems to work, however IntelliJ does not pick up the new source set as a test module. I can manually mark it, but it resets every time Gradle runs. This also means that Intellij fills in the Output Path and not the Test Output Path fields in the project structure settings.
To fix that the below configuration works:
apply plugin: 'idea'
idea {
module {
testSourceDirs += project.sourceSets.testIntegration.java.srcDirs
testSourceDirs += project.sourceSets.testIntegration.kotlin.srcDirs
testSourceDirs += project.sourceSets.testIntegration.resources.srcDirs
}
}
However, this seems to instruct IntelliJ that the Test Output Path is \out\test\classes which is the same as the standard 'test' module and causes conflict issues. I want it to keep the output path as the original \out\testIntegration\classes which would have otherwise been used.
Is there any way to instruct IntelliJ to pick up this new test source set correctly and fill in the right output path?
If you'd like to configure the idea gradle plugin with your custom test sourceSets in a kotlin gradle build script you can do it like that:
val testIntegrationSrcDirName = "testIntegration"
sourceSets {
...
create(testIntegrationSrcDirName) {
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().output // take also unit test source and resources
runtimeClasspath += sourceSets.main.get().output + sourceSets.test.get().output // take also unit test source and resources
}
}
...
idea {
module {
testSourceDirs = testSourceDirs + sourceSets[testIntegrationSrcDirName].allSource.srcDirs
}
}
val testIntegrationImplementation: Configuration by configurations.getting {
extendsFrom(configurations.implementation.get())
}
configurations["${testIntegrationSrcDirName}RuntimeOnly"].extendsFrom(configurations.runtimeOnly.get())
...

Spock test in gradle could not be configured in a separate sourceSet

I have been trying to configure spock in the gradle project. So with the following configuration, it works out.
apply plugin: 'groovy'
My test cases are in the folder src/test/groovy. This works fine.I am able to run the test case.
However , I want to separate the integration tests into a separate folder structure - src/itest/groovy.
For this I added the following:
sourceSets {
itest {
srcDir file('src/itest/groovy')
}
resourceDir ..
compileClassPath ..
}
configurations {
itestCompile.extendsfrom testCompile
}
I am not able to copy the entire code here because of org restrictions. But I did try what all variations I could get online and it did not work!!
I always got the error:
The task compileItestGroovy was not found in the project.
I did some research and this task is added by the groovy plugin by default. Still the task could not be found and my build is in limbo. It would be great if you could help me up with this.
P.S This project also has other plugins such as java as the source code is in java.
Command to run - ./gradlew clean build
Gradle version - 2.2.1
I tried similar set up on my home pc with gradle version 3.5 and it works fine.
Given the following build.gradle file:
apply plugin: 'groovy'
repositories {
jcenter()
}
sourceSets {
integration {
groovy {
compileClasspath += main.output
runtimeClasspath += main.output
srcDirs = ['src/integration/groovy']
}
resources.srcDir file('src/integration/resources')
}
}
configurations {
// By default, integration tests have the same dependencies as standard tests
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
// This is only available for the integration tests
integrationCompile 'com.github.ben-manes.caffeine:caffeine:2.5.5'
}
tasks.create('integrationTest', Test) {
dependsOn 'compileIntegrationGroovy'
group = 'Verification'
description = 'Runs the integration tests'
// GRADLE 2.2
testClassesDir = sourceSets.integration.output.classesDir
// GRADLE 3.5
// testClassesDirs = sourceSets.integration.output.classesDirs
classpath = sourceSets.integration.runtimeClasspath
}
tasks.findByName('check').dependsOn 'integrationTest'
You should be able to stick integration tests inside src/integration/groovy and ./gradlew check will run both the standard tests, AND the integration tests

Jacoco coverage report of JUnit tests for a Gradle multiproject

I am having difficulty combining Jacoco coverage report for a Gradle multi-project. For simplicity, I have created a simplified version that is structured as follows. I have the example project on GitHub.
+ root
+ common-a
+ common-b
+ project-a
+ project-b
The root, project-a, and project-b are Spring Boot applications. For some context, a project is meant to be a micro-service. Thus, the root project is simply running all micro-services in one process.
Not all projects have unit and/or integration tests. common-a subproject does not have any tests; common-b, project-a, and project-b projects have unit and integration tests; the root project has only integration tests.
The Jacoco coverage reports for subprojects are correct. It is able to combine both unit and integration tests into one cohesive report. However, the root project does not produce the cohesive report correctly.
I have tried a number of solutions with any success. Most of the solutions I found assume that the root project is simply a logical container and only the subprojects has test code. The results of the solutions I tried fall into one of the following three categories.
No Jacoco report generated at all; the build/reports/jacoco directory is missing
Partial Jacoco report for only sources in root project
Full Jacoco coverage report for root and subproject but 0% coverage; it is supposed to be 100% coverage
The following is the current build.gradle file.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
}
}
// settings for all projects
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
group 'io.github.chriszhong'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
jcenter()
}
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:2.0.5.RELEASE'
}
}
sourceSets {
integrationTest {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
java {
srcDir 'src/test-integration/java'
}
resources {
srcDir 'src/test-integration/resources'
}
}
}
configurations {
integrationTestCompile {
extendsFrom testCompile
}
integrationTestRuntime {
extendsFrom testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.integrationTest.java.srcDirs
testSourceDirs += sourceSets.integrationTest.resources.srcDirs
}
}
task integrationTest(type: Test) {
check {
dependsOn integrationTest
}
testClassesDir sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
task testReport(type: TestReport) {
dependsOn check
destinationDir file("${reporting.baseDir}/unified/html")
reportOn test, integrationTest
}
task jacocoMerge(type: JacocoMerge) {
dependsOn check
executionData test, integrationTest
// temporary hack to fix a bug requiring that there be at least one test case in each source set for coverage data to be generated;
// it should be if there is at least one test case in any of the source sets
executionData = files(executionData.findAll({ it.exists() }))
}
tasks.withType(Test) {
reports {
html {
destination file("${reporting.baseDir}/${task.name}/html")
}
}
}
tasks.withType(JacocoReport) {
dependsOn jacocoMerge
executionData jacocoMerge.destinationFile
// temporary hack to fix a bug requiring that there be at least one test case in each source set for coverage data to be generated;
// it should be if there is at least one test case in any of the source sets
// executionData = files(executionData.findAll({ it.exists() }))
}
}
apply plugin: 'spring-boot'
version '1.0-SNAPSHOT'
dependencies {
compile project(':project-a')
compile project(':project-b')
testCompile 'junit:junit'
integrationTestCompile 'org.springframework.boot:spring-boot-starter-test'
}
testReport {
dependsOn check, subprojects.testReport
reportOn test, integrationTest, subprojects.test, subprojects.integrationTest
}
jacocoMerge {
dependsOn check, subprojects.jacocoMerge
executionData test, integrationTest, subprojects.jacocoMerge.destinationFile
// temporary hack to fix a bug requiring that there be at least one test case in each source set for coverage data to be generated;
// it should be if there is at least one test case in any of the source sets
executionData = files(executionData.findAll({ it.exists() }))
}
//jacocoTestReport {
// dependsOn jacocoMerge
// executionData test, integrationTest, subprojects.test, subprojects.integrationTest
// executionData jacocoMerge.destinationFile
// temporary hack to fix a bug requiring that there be at least one test case in each source set for coverage data to be generated;
// it should be if there is at least one test case in any of the source sets
// executionData = files(executionData.findAll({ it.exists() }))
// sourceDirectories = files(sourceSets.main.java.srcDirs, subprojects.sourceSets.main.java.srcDirs)
// classDirectories = files(sourceSets.main.output.classesDir, subprojects.sourceSets.main.output.classesDir)
//}
task wrapper(type: Wrapper) {
gradleVersion '2.14'
distributionUrl "https://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
}
I have been banging my head at this problem for the past week and is at my wits end. I greatly appreciate any help I can get in resolving this problem.

Resources