Software QA FYI - SQAFYI

Getting Started With Gradle: Integration Testing

By: Petri Kainulainen

Because the standard project layout of a Java project defines only one test directory (src/test), we have no standard way to add integration tests to our Gradle build.
If we want to use the standard project layout, we can add integration tests to our Gradle build by using one of the following options:

We can add our integration tests to the same directory than our unit tests. This is an awful idea because integration tests are typically a lot slower than unit tests. If we decide to use this approach, the length of our feedback loop is a lot longer than it should be.

We can create a new project and add our integration tests to that project. This makes no sense because it forces us to transform our project into a multi-project build. Also, if our project is already a multi-project build, we are screwed. We can of course add all integration tests to the same project or create new integration test project for each tested project, but it would be less painful to shoot ourselves in the foot.
It is clear that we need a better way. This blog post describes how we create a Gradle build that fulfils the following requirements:
Integration and unit tests must have different source directories. The src/integration-test/java directory must contain the source code of our integration tests and the src/test/java directory must contain the source code of our unit tests.

Integration and unit tests must have separate resource directories. The src/integration-test/resources directory must contain the resources of our integration tests. The src/test/resources directory must contain the resources of our unit tests.

We must be able to configure compile time and runtime dependencies for our integration tests.
We must be able to run either our unit tests or integration tests.
We must be able to run all tests.
If an integration test fails, our build must fail as well.
Integration and unit tests must have separate HTML reports.

Letís start by configuring the source and resource directories of our integration tests.

Configuring the Source and Resource Directories of Our Integration Tests

We can add new source and resource directories to our Gradle build by using the sourceSets build script block. Armed with this information, we can configure the source and resource directories of our integration tests by following these steps:

Create a new source set called integrationTest.
Ensure that the output of the main and test source sets is added to the compile time classpath.
Ensure that the output of the main and test source sets is added to the runtime classpath.
Set the source directory of our integration tests to src/integration-test/java.
Set the resource directory of our integration tests to src/integration-test/resources.

When we are done, our build.gradle file should have the following sourceSets build script block right after the repositories build script block:
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
Additional Reading:

Section 23.2 Java Plugin Ė Source sets of Gradle User Guide
The DSL Reference of the sourceSets build script block
The DSL Reference of the SourceSetOutput

When we run the command: gradle properties at the command prompt, we will see a long list of the projectís properties. The properties that are relevant for this blog posts are shown in the following:

gradle properties
:properties

------------------------------------------------------------
Root project
------------------------------------------------------------
configurations: [configuration ':archives', configuration ':compile',
configuration ':default', configuration ':integrationTestCompile',
configuration ':integrationTestRuntime', configuration ':runtime',
configuration ':testCompile', configuration ':testRuntime']

sourceSets: 1
sources: [Java source 'main:java', JVM resources 'main:resources', Java source 'test:java', JVM resources 'test:resources', Java source 'integrationTest:java', JVM resources 'integrationTest:resources']

BUILD SUCCESSFUL
Total time: 3.34 secs

As we can see, we added a new source and resource directories to our Gradle build. The interesting this is that when we created a new source set, the Java plugin added two new dependency configurations to our build:

The integrationTestCompile configuration is used to declare the dependencies that are required when our integration tests are compiled.
The integrationTestRuntime configuration is used to declare the dependencies that are required to run our integration tests. This configuration contains all dependencies that are added to the integrationTestCompile configuration.

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Getting Started With Gradle: Integration Testing