JUnit5标签特定的gradle任务 [英] JUnit5 tag-specific gradle task

查看:260
本文介绍了JUnit5标签特定的gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下注释标记集成测试:

I use the following annotation to tag my integration tests:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("integration-test")
public @interface IntegrationTest {
}

这是我在build.gradle中用来从gradle build中排除这些测试的过滤器:

This is the filter I use in build.gradle to exclude these tests from gradle build:

junitPlatform {
    filters {
        tags {
            exclude 'integration-test'
        }
    }
}

到目前为止,很好.

现在,我想提供一个专门运行我的集成测试的Gradle任务-推荐的方法是什么?

Now I would like to offer a Gradle task which specifically runs my integration tests – what's the recommended approach?

推荐答案

基于 https://github.com/gradle/gradle/issues/6172#issuecomment-409883128

在2020年修订,以考虑懒惰的任务配置和Gradle 5.查看较旧版本的答案历史记录.

plugins {
    id "java"
}

def test = tasks.named("test") {
    useJUnitPlatform {
        excludeTags "integration"
    }
}

def integrationTest = tasks.register("integrationTest2", Test) {
    useJUnitPlatform {
        includeTags "integration"
    }
    shouldRunAfter test
}

tasks.named("check") {
    dependsOn integrationTest
}

正在跑步

  • gradlew test将运行不集成的测试
  • gradlew integrationTest将仅运行集成测试
  • gradlew check将先运行test,然后再运行integrationTest
  • gradlew integrationTest test将先运行test,然后再运行integrationTest
    注意:由于shouldRunAfter
  • ,订单已调换

    Running

    • gradlew test will run tests without integration
    • gradlew integrationTest will run only integration test
    • gradlew check will run test followed by integrationTest
    • gradlew integrationTest test will run test followed by integrationTest
      note: order is swapped because of shouldRunAfter
      • Gradle 4.6+ supports JUnit 5 natively
      • JUnit 5 deprecated their plugin: https://github.com/junit-team/junit5/issues/1317
      • JUnit 5 deleted plugin: 'org.junit.platform.gradle.plugin'
      • JUnit 5 closed junit5#579 (same as OP's question) as won't-fix (due to decommissioning their plugin)
      • Gradle supports the above feature: https://github.com/gradle/gradle/issues/6172

      注意:尽管上述方法有效,但IntelliJ IDEA很难推断出内容,因此,我建议使用更明确的版本,其中键入所有内容并完全支持代码完成:

      ... { Test task ->
          task.useJUnitPlatform { org.gradle.api.tasks.testing.junitplatform.JUnitPlatformOptions options ->
              options.includeTags 'integration'
          }
      }
      

      build.gradle.kts

      根项目Kotlin DSL插件,用于在Gradle 5.6.4的所有模块中配置集成测试

      build.gradle.kts

      Root project Kotlin DSL drop-in for configuring integration tests in all modules in Gradle 5.6.4

      allprojects {
          plugins.withId("java") {
              @Suppress("UnstableApiUsage")
              this@allprojects.tasks {
                  val test = "test"(Test::class) {
                      useJUnitPlatform {
                          excludeTags("integration")
                      }
                  }
                  val integrationTest = register<Test>("integrationTest") {
                      useJUnitPlatform {
                          includeTags("integration")
                      }
                      shouldRunAfter(test)
                  }
                  "check" {
                      dependsOn(integrationTest)
                  }
              }
          }
      }
      

      这篇关于JUnit5标签特定的gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆