How to add a new set of sources in Gradle?

I want to add integration tests to my Gradle construct (version 1.0). They must be run separately from my regular tests because they require that the webapp be deployed to localhost (they are testing this webapp). Tests should be able to use the classes defined in my main set of sources. How to do it?

+61
java build integration-testing gradle source-sets
Jul 20 '12 at 14:30
source share
5 answers

It took me a while to figure out, and the online resources were small. Therefore, I wanted to document my decision.

This is a simple gradle build script that has an intTest source in addition to the main and test source sets:

apply plugin: "java" sourceSets { // Note that just declaring this sourceset creates two configurations. intTest { java { compileClasspath += main.output runtimeClasspath += main.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 } 
+84
Jul 20 '12 at 14:30
source share

This is how I achieved this without using configurations{ } .

 apply plugin: 'java' sourceCompatibility = JavaVersion.VERSION_1_6 sourceSets { integrationTest { java { srcDir 'src/integrationtest/java' } resources { srcDir 'src/integrationtest/resources' } compileClasspath += sourceSets.main.runtimeClasspath } } task integrationTest(type: Test) { description = "Runs Integration Tests" testClassesDir = sourceSets.integrationTest.output.classesDir classpath += sourceSets.integrationTest.runtimeClasspath } 

Tested using: Gradle 1.4 and Gradle 1.6

+26
May 29 '13 at 16:34
source share

To summarize both old answers (get the best and minimum vitality of both worlds):

some kind words:

  • first we need to define sourceSet:

     sourceSets {
         integrationTest
     }
    
  • Then we extend the sourceSet from the test, so we use the test runtimeClasspath (which includes all excerpts from the AND test itself) as the classpath for the derived sourceSet

     sourceSets {
         integrationTest {
             compileClasspath + = sourceSets.test.runtimeClasspath
             runtimeClasspath + = sourceSets.test.runtimeClasspath // ***)
         }
     }
    
    • *** note) somehow this update / extension for sourceSets.integrationTest.runtimeClasspath is necessary, but should be inappropriate, since runtimeClasspath always extends output + runtimeSourceSet , does not receive it
  • we define the highlighted task only to run integration tests

     task integrationTest (type: Test) {
     }
    
  • tell the test task in which test classes and classes should be used, instead of the default values โ€‹โ€‹from the "test" sourceset

     task integrationTest (type: Test) {
         testClassesDir = sourceSets.integrationTest.output.classesDir
         classpath = sourceSets.integrationTest.runtimeClasspath
     }
    
  • (optional) automatic start after test

     integrationTest.dependsOn test
    
  • (optional) automatic start with check

     check.dependsOn integrationTest
    
  • (optional) add java resources to the sourceSet to enable automatic discovery and create these "partial" in your IDE. that is, IntelliJ IDEA will automatically create sourceSet java directories and resources for each set if it does not exist

     sourceSets {
          integrationTest {
              java
              resources
          }
     }
    

TL; DR

 apply plugin: 'java' // apply the runtimeClasspath from "test" sourceSet to the new one // to include any needed assets: test, main, test-dependencies and main-dependencies sourceSets { integrationTest { // not necessary but nice for IDEa's java resources compileClasspath += sourceSets.test.runtimeClasspath // somehow this redeclaration is needed, but should be irrelevant // since runtimeClasspath always expands compileClasspath runtimeClasspath += sourceSets.test.runtimeClasspath } } // define custom test task for running integration tests task integrationTest(type: Test) { testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath } integrationTest.dependsOn test 

Recalling:

unfortunately, the sample code is on github.com/ gradle / gradle / subprojects / docs / src / samples / java / customizedLayout / build. gradle or ... / gradle /.../ withIntegrationTests / build.gradle does not seem to cope with this or has another / more complicated / for me there is no clear solution anyway!

+9
Jun 17 '16 at 12:49
source share

The nebula-facet module eliminates the pattern:

 apply plugin: 'nebula.facet' facets { integrationTest { parentSourceSet = 'test' } } 

For integration tests, even this is done for you , just apply:

 apply plugin: 'nebula.integtest' 

The Gradle portal portal associations for each of them:

+5
Aug 09 '16 at 18:03
source share

Here is what works for me as Gradle 4.0.

 sourceSets { integrationTest { compileClasspath += sourceSets.test.compileClasspath runtimeClasspath += sourceSets.test.runtimeClasspath } } task integrationTest(type: Test) { description = "Runs the integration tests." group = 'verification' testClassesDirs = sourceSets.integrationTest.output.classesDirs classpath = sourceSets.integrationTest.runtimeClasspath } 

Starting with version 4.0, Gradle now uses separate class directories for each language in the source set. Therefore, if your build script uses sourceSets.integrationTest.output.classesDir , you will see the following failure warning.

Gradle now uses separate output directories for each JVM language, but this assembly assumes a single directory for all classes from the source set. This behavior is deprecated and should be removed in Gradle 5.0.

To get rid of this warning, just switch to sourceSets.integrationTest.output.classesDirs . See Gradle 4.0 Notes for more information.

0
Aug 02 '17 at 15:31 on
source share



All Articles