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'
}
Related
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.
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
What do I want to do?
I want to tell CodeNarc which folders/directories it is supposed to scan/analyse.
I cant find anything about that on the official site (http://codenarc.sourceforge.net/) or on the gradle plugin documentation (https://docs.gradle.org/current/userguide/codenarc_plugin.html).
Results of my Research:
The only related thing I found was in the grails CodeNarc plugin documentation (https://grails.org/plugin/codenarc). There it says you can configure which source files are analysed by CodeNarc. But this seems to be the case only for the grails CodeNarc plugin not the gradle version.
The gradle CodeNarc documentation describes 3 tasks, one of them with the following:
codenarcSourceSet — CodeNarc
Runs CodeNarc against the given source set’s Java source files
Based on the description I thought I might be able to do something with that.
But when I look for all tasks my gradle Project got only "codenarcMain" and "codenarcTest".
My Set-up:
My gradle project got the following structure:
.git
-> ...
.gradle
-> ...
config
-> codenarc -> MyRuleset.groovy
gradle
-> ...
src
-> main
-> groovy -> ...
-> ressources
-> test
-> groovy -> ...
-> ressources
vars
-> ...
.gitignore
build.gradle
gradlew
gradlew.bat
ReadMe.txt
settings.gradle
My build.gradle looks like that:
plugins {
// Apply the groovy plugin to add support for Groovy
id 'groovy'
id 'codenarc'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
maven { url "http://repo.jenkins-ci.org/releases/" }
maven { url "http://central.maven.org/maven2/" }
}
dependencies {
// https://mvnrepository.com/artifact/org.jenkins-ci.main/jenkins-core
compile group: 'org.jenkins-ci.main', name: 'jenkins-core', version: '2.179'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins/cloudbees-folder
compile 'org.jenkins-ci.plugins:cloudbees-folder:5.3#jar'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-api
compile 'org.jenkins-ci.plugins.workflow:workflow-api:2.35#jar'
// https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-job
compile 'org.jenkins-ci.plugins.workflow:workflow-job:2.32#jar'
// https://mvnrepository.com/artifact/com.cloudbees/groovy-cps
compile group: 'com.cloudbees', name: 'groovy-cps', version: '1.28'
// https://mvnrepository.com/artifact/org.codenarc/CodeNarc
codenarc group: 'org.codenarc', name: 'CodeNarc', version: '1.1'
// Use the latest Groovy version for building this library
implementation 'org.codehaus.groovy:groovy-all:2.5.6'
// Use the awesome Spock testing and specification framework
testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
}
codenarc {
configFile = file("${project.projectDir}/config/codenarc/MyRuleset.groovy")
reportFormat = 'html'
reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
maxPriority1Violations = 0
maxPriority2Violations = 0
maxPriority3Violations = 0
}
Specific goal:
I basically want CodeNarc to also scan scripts inside the "vars" folder/directory.
But it seems to do that only inside the src/main/groovy and src/test/groovy.
I also don't know exactly what the default paths are because I didn't find it in the documentation.
Seems like all you have to do is add the following to your gradle.build
sourceSets {
main {
groovy {
srcDirs = ['src/main', 'vars']
}
}
}
This way CodeNarc scans everything inside the src/main folder and everything inside the vars folder. (https://discuss.gradle.org/t/gradle-codenarc-plugin-how-to-change-target-folder-for-the-scan/32102/2)
[edit]
It is even possible to define your own codeNarc task:
sourceSets {
main {
groovy {
srcDirs = ['src']
}
resources {
srcDirs = []
}
}
test {
groovy {
srcDirs = ['test']
}
resources {
srcDirs = []
}
}
vars {
compileClasspath += main.compileClasspath
compileClasspath += main.output
groovy {
srcDirs = ['vars']
}
resources {
srcDirs = []
}
}
}
codenarc {
toolVersion = '1.3'
configFile = file("${project.projectDir}/config/codenarc/ruleset.groovy")
sourceSets = [sourceSets.main, sourceSets.test, sourceSets.vars]
reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
}
codenarcMain {
configFile = file("${project.projectDir}/config/codenarc/MainRuleset.groovy")
}
codenarcTest {
configFile = file("${project.projectDir}/config/codenarc/TestRuleset.groovy")
}
codenarcVars {
configFile = file("${project.projectDir}/config/codenarc/VarsRuleset.groovy")
}
I'm using Geb/Spock for automated testing. I'm using Gradle as my build tool.
I'd like to call different gradle tasks to build and run a specific spec(test) or a suite of specs.
I dont know enough about the gradle build lifecycle to completely understand what is going on here: https://github.com/geb/geb-example-gradle/blob/master/build.gradle
plugins {
id "idea"
id "groovy"
id "com.energizedwork.webdriver-binaries" version "1.4"
id "com.energizedwork.idea-base" version "1.4"
}
ext {
// The drivers we want to use
drivers = ["firefox", "chrome", "chromeHeadless"]
ext {
groovyVersion = '2.4.12'
gebVersion = '2.2'
seleniumVersion = '3.6.0'
chromeDriverVersion = '2.32'
geckoDriverVersion = '0.18.0'
}
}
repositories {
mavenCentral()
}
dependencies {
// If using Spock, need to depend on geb-spock
testCompile "org.gebish:geb-spock:$gebVersion"
testCompile("org.spockframework:spock-core:1.1-groovy-2.4") {
exclude group: "org.codehaus.groovy"
}
testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"
// If using JUnit, need to depend on geb-junit (3 or 4)
testCompile "org.gebish:geb-junit4:$gebVersion"
// Drivers
testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}
webdriverBinaries {
chromedriver chromeDriverVersion
geckodriver geckoDriverVersion
}
drivers.each { driver ->
task "${driver}Test"(type: Test) {
group JavaBasePlugin.VERIFICATION_GROUP
outputs.upToDateWhen { false } // Always run tests
systemProperty "geb.build.reportsDir", reporting.file("geb/$name")
systemProperty "geb.env", driver
}
}
test {
dependsOn drivers.collect { tasks["${it}Test"] }
enabled = false
}
tasks.withType(Test) {
maxHeapSize = "1g"
jvmArgs '-XX:MaxMetaspaceSize=128m'
testLogging {
exceptionFormat = 'full'
}
}
tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.memoryMaximumSize = '256m'
}
I've tried inserting the following into build.gradle:
task dataGen {
include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}
task sanity {
include '**com.company.project.spec.sanity.*'
}
But calling these tasks (gradle sanity) results in a build failure:
Could not find method include() for arguments [**com.company.project.spec.util/DataGenerationUtilSpec.groovy] on task ':dataGen' of type org.gradle.api.DefaultTask
Obviously there's existing build instructions since I can call gradle build and all the specs run on Chrome, I'm just not sure how to add more tasks
I think these 2 tasks are test tasks so it should look like that :
task dataGen (type: Test) {
include '**com.company.project.spec.util/DataGenerationUtilSpec.groovy'
}
task sanity (type: Test) {
include '**com.company.project.spec.sanity.*'
}
You can use Spock annotation to control the test or the Spec, see example here.
You will have to define annotation classes and define the Spock config file to use that annotation. You then annotate the specific Specification (or test).
Now you will have to define the Spock config file in the task or from a parameter.
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())
...