Customizing an intTest task to use JUnit() after task definition has occurred


(Mark Petrovic) #1

I am using Gradle 1.3.

I have this canonical intTest-type task

sourceSets {
    intTest {
        compileClasspath = sourceSets.main.output + configurations.testRuntime
        runtimeClasspath = output + sourceSets.main.output + configurations.testRuntime
    }
}
  test {
    useTestNG()
}
  task intTest(type: Test) {
    useTestNG()
    testClassesDir = sourceSets.intTest.output.classesDir
    classpath = sourceSets.intTest.runtimeClasspath
    outputs.upToDateWhen { false }
    install.enabled = false
    uploadArchives.enabled = false
}

imported into a subproject build using

apply from: file("${rootDir}/gradle/inttest.gradle")

Most of my integration tests use TestNG. Hence my desire to default the task config to that using useTestNG().

However, there is a different subproject integration test that needs to use JUnit. I tried this, after the ‘apply from’, in that oddball subproject’s build.gradle to try to force JUnit:

intTest.doLast {
   useJUnit()
   ...
}

but the JUnit-based tests do not run. As if by the time the call to useJUnit() is made, it’s too late to configure the task.

Is it too late by this point to customize the intTest task? If not, what is a good way to achieve this late-binding of the task test runner to JUnit?

Thanks.


(Mark Petrovic) #2

Here is a test project that manifests the effect:

https://github.com/ae6rt/gradle-junit-override.git


(Peter Niederwieser) #3

‘intTest.doLast { … }’ runs after the tests have run. Why not just ‘intTest { … }’?


(Mark Petrovic) #4

Perfect. Sorry for the brain lapse - somehow I thought I remembered that doLast would append to the task definition.