xtermi2
xtermi2

Reputation: 769

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

Upvotes: 8

Views: 7478

Answers (3)

netmikey
netmikey

Reputation: 2550

To recap the OP's findings and Sam Brennan's remark, the most concise way I found to achieve this is (Groovy Syntax):

tasks.withType(Test) {
    useJUnitPlatform({
        if (project.hasProperty('excludeTags')) {
            excludeTags(project.excludeTags)
        }
    })
}

and then on the command line:

gradle test -PexcludeTags=MUTED

Upvotes: 0

xyz
xyz

Reputation: 937

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

Upvotes: 2

Sam Brannen
Sam Brannen

Reputation: 31247

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

Upvotes: 4

Related Questions