Using additional test types with Test Suites (Incubating) Sample
You can open this sample in an IDE that supports Gradle. |
This sample shows how to add additional test types to a JVM project in Gradle. Your project may have many different kinds of tests—unit tests, integration tests, functional tests, etc. A JVM project in Gradle has a single Test
task, but other tasks can be added to represent each of these test types using the Test Suite Plugin.
This sample shows how to adopt an existing sample for use with Test Suites. |
Test Suites are an incubating feature, and the details described here may change. |
In this sample, we are testing Java projects with JUnit5; however, this applies to other JVM languages as well.
Concretely, we add a convention plugin in buildSrc
to share the integration test setup between multiple subprojects:
plugins {
java
}
version = "1.0.2"
group = "org.gradle.sample"
repositories {
mavenCentral()
}
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter("5.7.1")
}
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
implementation(project())
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
tasks.named("check") {
dependsOn(testing.suites.named("integrationTest"))
}
plugins {
id 'java'
}
version = '1.0.2'
group = 'org.gradle.sample'
repositories {
mavenCentral()
}
testing {
suites {
test {
useJUnitJupiter('5.7.1')
}
integrationTest(JvmTestSuite) {
dependencies {
implementation project()
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
tasks.named('check') {
dependsOn(testing.suites.integrationTest)
}
And apply it in an application project:
plugins {
id("myproject.java-conventions")
application
}
dependencies {
implementation(project(":list"))
implementation(project(":utilities"))
}
application {
mainClass = "org.gradle.sample.app.Main"
}
plugins {
id 'myproject.java-conventions'
id 'application'
}
dependencies {
implementation project(':utilities')
}
application {
mainClass = 'org.gradle.sample.app.Main'
}
And in library projects:
plugins {
id("myproject.java-conventions")
`java-library`
}
dependencies {
api(project(":list"))
}
plugins {
id 'myproject.java-conventions'
id 'java-library'
}
dependencies {
api project(':list')
}
These additional tests can be executed with the integrationTest
task or as part of lifecycle check
:
$ ./gradlew check BUILD SUCCESSFUL 14 actionable tasks: 14 executed
For more information, see section Configuring integration tests in the Testing in Java project chapter.