command -Dkotest.tags="tagName" is not working

34 views Asked by At

Kotest ver.

testImplementation(platform("io.kotest:kotest-bom:5.8.0"))
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.3")
testImplementation("io.kotest:kotest-assertions-core")
testImplementation("io.kotest:kotest-assertions-json-jvm")
testImplementation("io.kotest:kotest-runner-junit5-jvm")
testImplementation("io.kotest:kotest-framework-datatest")
testImplementation("io.kotest:kotest-property")

I got a test class with custom tag BEAVER. When i try to run
./gradlew test -Dkotest.tags="BEAVER" all test are fired (even without the tag). example ./gradle test -Dkotest.tags="Linux & !Database" returns error zsh: event not found: Database (MacOS)

object BEAVER: Tag()
class TagPlaygroundTest: StringSpec({
tags(BEAVER)

    "should pass with tag".config(tags = setOf(BEAVER)) {
        3 shouldBe 3
    }
    "should fail with tag".config(tags = setOf(BEAVER)) {
        3 shouldBe 4
    }
})

I want to run only tagged tests by using either ./gradlew or custom gradle tasks

1

There are 1 answers

0
user2340612 On

The reason why that doesn't work is that -D properties are supplied to gradle/gradlew and Gradle doesn't know it has to propagate them to Kotest. You need to change your build.gradle or build.gradle.kts file for Gradle to forward those properties to Kotest.

From the Kotest documentation:

Special attention is needed in your gradle configuration

To use System Properties (-Dx=y), your gradle must be configured to propagate them to the test executors, and an extra configuration must be added to your tests:

Groovy:

test {
    //... Other configurations ...
    systemProperties = System.properties
}

Kotlin Gradle DSL:

val test by tasks.getting(Test::class) {
    // ... Other configurations ...
    systemProperties = System.getProperties().asIterable().associate {
        it.key.toString() to it.value
    }
}

This will guarantee that the system property is correctly read by the JVM.