Separate target in gradle for Integration Tests

Using gradle, there is possibility to divide unit tests from in example module or integration tests stage. Even there is possibility to create few tests stages for different purposes.

Let’s imagine that in code there is need to have unit tests, module tests, functional tests and integration tests. To achieve that, one of the best way is to create separation in gradle modules at packages and run related tests according to selected method. For such a solution may be used gradle feature offering tasks.

 

At first there have to exclude classes with chosen pattern from goal “test”. To do that, add following in build.gradle (in example in section subprojects at root):

test {
		exclude '**/*IntegrationOfTest*', '**/*IntegrationTest*'
	}
build.gradle

 

Next we would like to have test reports in different targets. It may be important when job executed is in CI and we would like to zip a folder with test results. Probably also dividing may be needed when file collision is in generated files.

//Ensure that the HTML reports of unit and integration tests are written to different directories.
tasks.withType(Test) {
    ignoreFailures = true
    reports.html.destination = file("${reporting.baseDir}/${name}")
}
build.gradle

 

Finally need to create a build.gradle for module that will contain separated tests.

apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'idea'


configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

sourceSets {
    integrationTest {
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

dependencies {


    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
    testCompile group: 'org.assertj', name: 'assertj-core', version: '3.1.0'
    testCompile group: 'org.assertj', name: 'assertj-guava', version: '2.0.0'
   
    integrationTestCompile project(':your-backend')	
}

configurations.all {
    // check updates almost every build
    // resolutionStrategy.cacheChangingModulesFor 60, 'seconds'
}

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

//Ensure that the HTML reports of unit and integration tests are written to different directories.
tasks.withType(Test) {
    ignoreFailures = true
    reports.html.destination = file("${reporting.baseDir}/${name}")
}
build.gradle for integration tests

 

To run just use:

gradle build integrationTest

 

You may use as many tasks defined in gradle as you need.

This solution may be helpful in Nested Tests in JUnit and Integration Tests with heavy application in JUnit. Solution described in Integration Tests in Java with Docker also uses that one.

Posted in: Sortware

Tagged as: ,

Advertisements
Separate target in gradle for Integration Tests

Are you using Gradle?

Thank you for the vote!

Leave a Reply

Be the First to Comment!

Leave a Reply

  Subscribe  
Notify of