How to configure test src dir?

(Maciek Opała) #1

I’ve the following test structure: $projectRoot/src/test/groovy/unit $projectRoot/src/test/groovy/integration $projectRoot/src/test/groovy/functional

I’d like to implement separate task to run the tests separately for each folder:

task testUnit(type: Test) {

testSrcDir = // what goes here? }

No idea how to configure the path, any clues?

(Peter Niederwieser) #2

First of all, you should model these as separate source sets. See the Gradle User Guide and/or samples in the full Gradle distribution (e.g. the ‘java/withIntegrationTests’ sample) for how to do this.

The ‘testSrcDirs’ property is only relevant when using TestNG. However you’ll have to configure several other properties. Again, see the ‘java/withIntegrationTests’ sample and also the ‘Test’ task type in the Gradle Build Language Reference.

(Maciek Opała) #3

Thanks for the prompty response Peter. I’ve found example and trying to set it up. Will there be any differences for sources in groovy cause after first try it’s not working? I suppose there shouldn’t be.

(Peter Niederwieser) #4

Apart from applying the ‘groovy’ plugin and declaring a Groovy dependency, there shouldn’t be a difference.

(Maciek Opała) #5

plugin is applied of course, unfortunately it doesn’t work.

apply plugin: 'groovy'
  sourceSets {
    unitTest {
        groovy.srcDirs = [file('src/test/groovy/unit')]
        resources.srcDirs = [file('src/test/resources')]
  dependencies {
          testCompile 'org.spockframework:spock-core:0.6-groovy-1.8'
    testRuntime 'cglib:cglib-nodep:2.2.2'
      unitTestCompile sourceSets.main.output
    unitTestCompile configurations.testCompile
    unitTestCompile sourceSets.test.output
    unitTestRuntime configurations.testRuntime
task testUnit(type: Test) {
      testClassesDir = sourceSets.unitTest.output.classesDir
    classPath = sourceSets.unitTest.runtimeClasspath
    beforeTest { descriptor ->
        logger.lifecycle("Running test: " + descriptor)

No idea, what’s going wrong… Of course my build.gradle has much more configuration but it doesn’t affects the current problem (probably). Am I missing something?

(Peter Niederwieser) #6

Please always provide the exact error message.

One thing that I can spot is that the first two dependency declarations aren’t assigned to a configuration (e.g. ‘compile’), which makes them no-ops. Hence even compilation of Groovy code shouldn’t work.

There’s also a spelling mistake in the configuration of the ‘Test’ task (‘classPath’ should be ‘classpath’).

(Maciek Opała) #7

Thanks Peter. Compilation was working well, I just haven’t rewritten configuration for ‘gradleApi()’ and ‘localGroovy()’. I’ve fixed the spelling mistake in ‘classpath’ and now tests run as I expected. Thank You very much for helping me to configure it well.