John Tipper
Navigate back to the homepage

Gradle – Integration Testing for Gradle Plugin Development

John Tipper
July 18th, 2018 · 1 min read

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.gradle
2
3// define the source used for your functional tests
4sourceSets {
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.testRuntime
13 runtimeClasspath += output + compileClasspath
14 }
15}
16
17
18// define a test task that will execute these tests
19task functionalTest(type: Test) {
20 testClassesDirs = sourceSets.functionalTest.output.classesDirs
21 classpath = sourceSets.functionalTest.runtimeClasspath
22}
23
24// let's make functionalTest a dependency on the check task
25check.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.functionalTest
3 plugins {
4 // as per normal
5 }
6}

Further details can be found in the Gradle docs.

More articles from John Tipper

How to connect multiple AWS accounts to Direct Connect using PrivateLink

Connect multiple AWS accounts to Direct Connect using PrivateLink

August 16th, 2021 · 6 min read

How to execute Liquibase changesets against ClickHouse

Executing Liquibase changesets against ClickHouse

February 8th, 2021 · 4 min read
© 2018–2021 John Tipper
Link to $https://twitter.com/john_tipperLink to $https://github.com/john-tipperLink to $https://www.linkedin.com/in/john-tipper-5076395