Last active
August 17, 2023 09:08
-
-
Save vab2048/c9c1599002225c1f96041f1c3c13fdbc to your computer and use it in GitHub Desktop.
Configure integrationTest additional source set for gradle project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Configuration for additional source sets for holding integration tests for the application. | |
* | |
* Resources used: | |
* - https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/ | |
* - https://spin.atomicobject.com/2018/07/18/gradle-integration-tests/ | |
* - https://github.com/unbroken-dome/gradle-testsets-plugin | |
* | |
* Suggested Use: | |
* - Place this script in the `gradle` directory. | |
* - Add the following to the relevant `build.gradle` file you want to configure integration tests for: | |
* apply from: "$rootDir/gradle/integrationTest.gradle" | |
* - Use the `integrationTestImplementation` and `integrationTestRuntimeOnly` configurations in the | |
* `dependencies` block where needed. | |
*/ | |
sourceSets { | |
// Define the name of the new source set as 'integrationTest'. | |
integrationTest { | |
// Includes the compiled main code when compiling integration tests. | |
compileClasspath += sourceSets.main.output | |
// Includes the compiled main code when running integration tests. | |
runtimeClasspath += sourceSets.main.output | |
} | |
} | |
// If the 'idea' plugin is enabled then configure it further. | |
if (project.plugins.findPlugin('idea')) { | |
idea { | |
module { | |
// Marks the integration test’s Java source directories for Intellij IDEA’s configuration. | |
testSourceDirs += sourceSets.integrationTest.java.srcDirs | |
// Marks the integration test’s resource directories for Intellij IDEA’s configuration. | |
testResourceDirs += sourceSets.integrationTest.resources.srcDirs | |
// Configures Intellij IDEA to scope the integration tests as TEST. | |
scopes.TEST.plus += [ configurations.integrationTestCompile ] | |
} | |
} | |
} | |
configurations { | |
// `integrationTestImplementation` extending from `implementation` means that all the declared dependencies of | |
// the production code also become dependencies of the integration tests. | |
integrationTestImplementation.extendsFrom implementation | |
// Same for the `integrationTestRuntimeOnly` configuration | |
integrationTestRuntimeOnly.extendsFrom runtimeOnly | |
} | |
/* | |
* Create a new Gradle task called `integrationTest` which runs our integration tests. | |
*/ | |
task integrationTest(type: Test) { | |
// We want to use the JUnit 5 platform to execute our integration test. | |
useJUnitPlatform() | |
// Describes this task for reports and user interfaces such as when running ./gradlew tasks. | |
description = 'Runs the integration tests.' | |
// Groups this task under Verification for reports and user interfaces such as when running ./gradlew tasks. | |
group = 'verification' | |
// Sets the code to test as the compiled code from the integrationTest source set. | |
testClassesDirs = sourceSets.integrationTest.output.classesDirs | |
// Sets the runtime classpath to be as defined in the integrationTest source set. | |
classpath = sourceSets.integrationTest.runtimeClasspath | |
// Forces Gradle to always run the integration tests when asked to. By default, Gradle attempts to optimize | |
// task execution by not re-running tasks whose inputs have not changed. Since integration tests may fail due | |
// to external systems, we want to run them even if no code has changed. | |
outputs.upToDateWhen { false } | |
// Enforces task ordering (not task dependency). We use `mustRunAfter test` rather than `dependsOn test` because we | |
// do not always want to run unit tests when we run integration tests. So: | |
// `./gradlew integrationTest test` - will run `test` then `integrationTest`. | |
// `./gradlew integrationTest` - will only run `integrationTest` (since it does not depend on `test`). | |
mustRunAfter test | |
} | |
// Enforces that integration tests will be run when `./gradlew check` is run. This is because semantically we would | |
// expect the `check` task to verify that all systems (including integrations) are working. | |
check.dependsOn integrationTest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment