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
}

 

Posted in Java by Connor Garvey at January 11th, 2011.
Tags: , , ,

3 Responses to “Functional Test and Integration Test Targets in Gradle”

  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