如何在 gradle cmd 中包含/排除 junit5 标签?

xte*_*mi2 6 gradle junit5

我想执行标记的 JUnit 5 测试,例如。只有缓慢的测试,使用 gradle。

我想在 Maven 中做同样的事情:

mvn test -Dgroups="slow"
Run Code Online (Sandbox Code Playgroud)

但是 gradle 中的等价物是什么?或者到底有什么吗?

执行所有标有 的 JUnit 5 测试@Tag("slow")。我知道创建这样的专用任务非常简单:

tasks.withType(Test::class.java).configureEach {
  useJUnitPlatform() {
    includeTags("slow")
  }
}
Run Code Online (Sandbox Code Playgroud)

但我有很多不同的标签,我不想为每个标签都有一个任务。或者更糟糕的是,所有排列都有一个任务。

另一种可能性是将自定义属性传递给任务,如下所示

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

Sam*_*nen 3

我上次检查时,Gradle 没有内置支持通过命令行配置 JUnit Platform 包含和排除标签,因此您必须使用第二种方法。

但是......不需要分割、映射和过滤标签:只需使用标签表达式即可: https: //junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions