How to include/exclude junit5 tags in gradle cmd?

Viewed 2986

I want to execute tagged JUnit 5 tests, eg. only slow tests, with gradle.

I want to do the same like this in maven:

mvn test -Dgroups="slow"

But what is the equivalent in gradle? Or is there anything at all?

To execute all JUnit 5 tests which are marked with @Tag("slow"). I know it's quite simple to create a dedicated task like this:

tasks.withType(Test::class.java).configureEach {
  useJUnitPlatform() {
    includeTags("slow")
  }
}

But I have a lot of different tags and I don't want to have a task for each single tag. Or worse, having one task for all permutations.

Another possibility would be to pass self defined properties to the task like this

tasks.withType(Test::class.java).configureEach {
  val includeTagsList = System.getProperty("includeTags", "")!!.split(",")
          .map { it.trim() }
          .filter { it.isNotBlank() }
  if (includeTagsList.isNotEmpty()) {
    includeTags(*includeTagsList.toTypedArray())
  }
}
2 Answers

You can create a separate task and to choose do you want to run the task or to skip it. For example add this code in build.gradle:

def slowTests= tasks.register("slowTests", Test) {
    useJUnitPlatform {
        includeTags "slow"
    }
}

Now if you want to run only slow tests:

./gradlew clean build -x test slowTests
Related