Integration Testing A Gradle Application

Separating integration tests from unit tests is a standard best practice. We can use small, fast unit tests during development, and large, slow tests periodically to detect regression bugs and integration issues. So our build system should make it easy to run integration tests, right?

Unfortunately, integration testing with maven is not so easy. In maven, integration testing is not a first-class citizen, though as mentioned in this link you can configure it to your liking with special configuration of the failsafe plugin. There are also other ways to set up integration testing with JUnit Categories, or by using the test file name. Both of these techniques require special configuration of the surefire plugin.

These techniques seem like a lot of work (and non-standardized work, at that!) for something that should be easy to do for a build system.

Fortunately for gradle users, setting up integration tests is easy!

In the build.gradle file, with just a few lines we can define a location for integration test files and define a task to run them. The code looks like this:

sourceSets {
    // Note that just declaring this sourceset creates two configurations.
    intTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output

configurations {
    intTestCompile.extendsFrom testCompile
    intTestRuntime.extendsFrom testRuntime

task intTest(type:Test){
    description = "Run integration tests (located in src/intTest/...)."
    testClassesDir = project.sourceSets.intTest.output.classesDir
    classpath = project.sourceSets.intTest.runtimeClasspath

Originally when we ran “gradle test”, any test files in src/test/java will be run. Now we also have the option to run “gradle intTest” so that test files in src/intTest/java will be run. Easy, right? With the configuration given above, the integration test does not depend on test, so we can run unit tests separately from integration tests, or both together with “gradle test intTest”. Finally, the build does not depend on the integration tests, so we can build the system without necessarily running the integration tests (although we could make the build depend on passing integration tests if we wanted).

Hopefully this demonstrates the ease of configuration with gradle and encourages us to write more automated tests.

Happy testing!

1 Comment

Filed under Software Engineering

One response to “Integration Testing A Gradle Application

  1. Pingback: How to set up unit tests for Android / Gradle | Topics in Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s