When building a Gradle plugin, some tests only work when you run them in the context of a Gradle build, i.e. you need Gradle to be running to get access to the full Gradle context. Starting such a process (by GradleRunner) is slow and it may not be possible to do all your testing via ProjectBuilder or plain unit tests. As such, you may wish to separate out your unit tests from the longer running tests. You can do this via integration or functional tests.
You can create a new suite of tests (which we will call functional tests for the purpose of this blog post) by editing your build.gradle
file. The snippet below is for running the tests using Spock (i.e. the tests are written in Groovy).
1// build.gradle23// define the source used for your functional tests4sourceSets {5 functionalTest {6 groovy {7 srcDir file('src/test/groovy')8 }9 resources {10 srcDir file('src/test/resources')11 }12 compileClasspath += sourceSets.main.output + configurations.testRuntime13 runtimeClasspath += output + compileClasspath14 }15}161718// define a test task that will execute these tests19task functionalTest(type: Test) {20 testClassesDirs = sourceSets.functionalTest.output.classesDirs21 classpath = sourceSets.functionalTest.runtimeClasspath22}2324// let's make functionalTest a dependency on the check task25check.dependsOn functionalTest
If we were building a Gradle plugin using the java-gradle-plugin development plugin, then you can make the functional tests have the gradleTestKit()
dependency injected by the plugin by configuring the gradlePlugin
closure:
1gradlePlugin {2 testSourceSets sourceSets.functionalTest3 plugins {4 // as per normal5 }6}
Further details can be found in the Gradle docs.