JUnit5 tag-specific gradle task

前端 未结 2 604
一生所求
一生所求 2021-02-04 01:42

I use the following annotation to tag my integration tests:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention         


        
相关标签:
2条回答
  • 2021-02-04 02:01

    I filed an issue: https://github.com/junit-team/junit5/issues/579 (as suggested by Sam Brannen).

    Meanwhile, I am using a project property as a workaround:

    junitPlatform {
        filters {
            tags {
                exclude project.hasProperty('runIntegrationTests') ? '' : 'integration-test'
            }
        }
    }
    

    Consequently, integrations tests will be skipped with:

    gradle test

    but will be included with:

    gradle test -PrunIntegrationTests

    0 讨论(0)
  • 2021-02-04 02:10

    Based on https://github.com/gradle/gradle/issues/6172#issuecomment-409883128

    Amended in 2020 to take lazy task configuration and Gradle 5 into account. See answer's history for older versions.

    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
    }
    

    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

    History

    • 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

    Tip

    Note: while the above works, IntelliJ IDEA has a hard time inferring stuff, so I suggest to use this more explicit version where everything is typed and code completion is fully supported:

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

    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)
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题