This was once written for Gradle 2.x / 3.x in 2016 and is far outdated!! Please have a look at the documented solutions in Gradle 4 and up
To sum up both old answers (get best and minimum viable of both worlds):
some warm words first:
first, we need to define the sourceSet
:
sourceSets {
integrationTest
}
next we expand the sourceSet
from test
, therefor we use the test.runtimeClasspath
(which includes all dependenciess from test
AND test
itself) as classpath for the derived sourceSet
:
sourceSets {
integrationTest {
compileClasspath += sourceSets.test.runtimeClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath // ***)
}
}
sourceSets.integrationTest.runtimeClasspath
is needed, but should be irrelevant since runtimeClasspath
always expands output + runtimeSourceSet
, don't get itwe define a dedicated task for just running integration tests:
task integrationTest(type: Test) {
}
Configure the integrationTest
test classes and classpaths use. The defaults from the java
plugin use the test
sourceSet
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
(optional) auto run after test
integrationTest.dependsOn test
(optional) add dependency from check
(so it always runs when build
or check
are executed)
tasks.check.dependsOn(tasks.integrationTest)
(optional) add java,resources to the sourceSet
to support auto-detection and create these "partials" in your IDE. i.e. IntelliJ IDEA will auto create sourceSet
directories java and resources for each set if it doesn't exist:
sourceSets {
integrationTest {
java
resources
}
}
tl;dr
apply plugin: 'java'
// apply the runtimeClasspath from "test" sourceSet to the new one
// to include any needed assets: test, main, test-dependencies and main-dependencies
sourceSets {
integrationTest {
// not necessary but nice for IDEa's
java
resources
compileClasspath += sourceSets.test.runtimeClasspath
// somehow this redeclaration is needed, but should be irrelevant
// since runtimeClasspath always expands compileClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
// define custom test task for running integration tests
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
tasks.integrationTest.dependsOn(tasks.test)
referring to:
Unfortunatly, the example code on github.com/gradle/gradle/subprojects/docs/src/samples/java/customizedLayout/build.gradle or …/gradle/…/withIntegrationTests/build.gradle seems not to handle this or has a different / more complex / for me no clearer solution anyway!