Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include/exclude junit5 tags in gradle cmd?

Tags:

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())
  }
}
like image 325
xtermi2 Avatar asked Mar 01 '20 10:03

xtermi2


2 Answers

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

like image 134
Sam Brannen Avatar answered Oct 04 '22 22:10

Sam Brannen


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
like image 35
xyz Avatar answered Oct 04 '22 22:10

xyz