gradlejunit5

How to include/exclude junit5 tags in gradle cmd?


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())
  }
}

Solution

  • The last time I checked, Gradle didn't have built-in support for configuring JUnit Platform include and exclude tags via the command line, so you'll have to go with your second approach.

    But... there's no need to split, map, and filter the tags: just use a tag expression instead: https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions