How to add JUnitOptions to a JUnit test task


(Martin Ahrer) #1

I have multiple source sets that contain separated * unit tests * component tests * integration tests

For regular testing a invocation of gradle test componentTest integrationTest will just execute all of the tests.

For a build pipeline build job individual tests from component tests and integration tests are annotated with a JUnit category annotation @CommitStage. In this situation a gradle invocation gradle componentTest integrationTest now should only select the annotated classes for execution (apply a category filter).

What is the best approach with the least effort. Can I add a JUnit category selector using a system property / gradle property?


(René Groeschke) #2

you can specify the junit group you want to test in the gradle test task. The java plugin chapter in the gradle userguide(http://www.gradle.org/docs/current/userguide/java_plugin.html) contains an example (chapter 23.12.5 - test grouping)

cheers, René


(Martin Ahrer) #3

Looks like my question was not really clear enough. I have seen that gradle supports JUnit categories out-of-the-box. But my question really should have been if I need to define a new task for each test task that runs in the build-pipeline and needs the JUnit categories filter added.

Right now I have a task integrationTest

task integrationTest(type: Test) {

testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

}

Would that mean that I have to add a task pipelineIntegrationTest

task pipelineIntegrationTest(type: Test) {

testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

useJUnit {

includeCategories ‘org.gradle.junit.CategoryA’

}

}

And then the same for componentTest for which a pipelineComponentTest is implemented.

Is there a simple way that allows to “extend” (inherit) from an existing task definition so I don’t have to repeat the base task definition and don’t duplicate that.


(Peter Niederwieser) #4

There is no way to clone an existing task definition, but you can factor out common configuration into a configuration rule. For example:

def getTasks(name) {
    tasks.matching { it.name.toLowerCase().contains(name) }
}
  getTasks("integrationTest").all {
    testClassesDir = sourceSets.integrationTest.output.classesDir
      classpath = sourceSets.integrationTest.runtimeClasspath
    }
  task integrationTest(type: Test)
  task pipelineIntegrationTest(type: Test) {
      useJUnit {
          includeCategories 'org.gradle.junit.CategoryA'
      }
}