Functional Test and Integration Test Targets in Gradle

* Update February 13th, 2012: Thanks to Ben Ripkens for updates to match the new Gradle API.

While searching online, I found many suggestions for how to add a new test target to a Gradle script. Most of them were wrong and others didn’t properly separate the integration test target from standard targets. After not finding a solution, I came up with one on my own.

This example sets up integration tests for Groovy .

Create a source set

This will separate the integration test code from other code, allowing it to be built separately.

  • The classpath in the example gives integration tests access to all application and test classes
  • The source location will be src/integrationTest/groovy
sourceSets {
  integrationTest {
    compileClasspath = sourceSets.main.output + configurations.testRuntime
    runtimeClasspath = output + sourceSets.main.output + configurations.testRuntime
    groovy {
      srcDir 'src/integrationTest/groovy'
    }
  }
}

Add the target

 

task integrationTest(type: Test) {
  testClassesDir = sourceSets.integrationTest.output.classesDir
  classpath = sourceSets.integrationTest.runtimeClasspath
}

 


  1. Ben Ripkens says:

    Thank you for sharing this. FYI, please note that the API changed with recent versions of Gradle and that certain parts of the API became deprecated. With the new version, it looks like this:

    sourceSets {
    integrationTest {
    compileClasspath = sourceSets.main.output + configurations.testRuntime
    runtimeClasspath = output + sourceSets.main.output + configurations.testRuntime
    groovy {
    srcDir ‘src/integrationTest/groovy’
    }
    }
    }

    task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    }

  2. ronen says:

    Thank you!

    this should have been easier to find in gradle docs

Leave a Reply to Ben Ripkens