kotlingradlejunit5gradle-kotlin-dsl

How do you create multiple test override tasks in a build.gradle.kts for different JUnit5 Tags


In this question JUnit 5 test suite choose test methods its shown how to do it in Gradle using the Groovy language...

How would that translate into the equivalent for a build.gradle.kts file...

Say we had tests denoted with @Tag("Type1") and @Tag("Type2")

And I want to create 2 custom tasks run_type2_tests and run_type1_tests


Solution

  • To register a new Gradle task use tasks.register like described in the Gradle docs. For a testing task use the built-in task type Test:

    tasks.register<Test>("run_type1_tests"){
    }
    

    Then configure included tags via Test.useJUnitPlatform:

    useJUnitPlatform {
        includeTags("Type1")
    }
    

    Final result:

    tasks.register<Test>("run_type1_tests") {
        useJUnitPlatform {
            includeTags("Type1")
        }
    }
    
    tasks.register<Test>("run_type2_tests") {
        useJUnitPlatform {
            includeTags("Type2")
        }
    }