JUnit5标记特定的gradle任务

问题描述 投票:19回答:2

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

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

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

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

到目前为止,很好。

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

gradle junit5
2个回答
31
投票

基于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
  • ,订单被交换

历史

提示

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

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

build.gradle.kts

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

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

7
投票

我提出了一个问题:https://github.com/junit-team/junit5/issues/579(由Sam Brannen建议)。

© www.soinside.com 2019 - 2024. All rights reserved.