Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give System property to my test via Kotlin Gradle and -D

When I run a test in Gradle I would like to pass some properties:

./gradlew test -DmyProperty=someValue

So in my Spock test I will use to retrieve the value:

def value = System.getProperty("myProperty")

Im using the kotlin gradle dsl. When I try and use 'tasks.test' as in this documentation: https://docs.gradle.org/current/userguide/java_testing.html#test_filtering

'test' is not recognised in my build.gradle.kts file.

I'm assuming I would need to use something similar to the answer in the post below but it is not clear how it should be done in the using the gradle kotlin DSL.

How to give System property to my test via Gradle and -D

like image 922
2hardproblems Avatar asked Jun 17 '19 10:06

2hardproblems


People also ask

How do you set a system property in gradle?

To define system properties for our Gradle build we can use the command line option --system-prop or -D . But we can also add the values for system properties in the gradle. properties file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp.


2 Answers

The answers from your linked question are translatable 1:1 to the kotlin DSL. Here is a full example using junit5.

dependencies {
    // ...
    testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
    testImplementation(kotlin("test-junit5"))
}

tasks.withType<Test> {
    useJUnitPlatform()

    // Project property style - optional property.
    // ./gradlew test -Pcassandra.ip=xx.xx.xx.xx
    systemProperty("cassandra.ip", project.properties["cassandra.ip"])

    // Project property style - enforced property.
    // The build will fail if the project property is not defined.
    // ./gradlew test -Pcassandra.ip=xx.xx.xx.xx
    systemProperty("cassandra.ip", project.property("cassandra.ip"))

    // system property style
    // ./gradlew test -Dcassandra.ip=xx.xx.xx.xx
    systemProperty("cassandra.ip", System.getProperty("cassandra.ip"))
}
like image 61
Florian Gutmann Avatar answered Oct 18 '22 04:10

Florian Gutmann


This example demos three ways of passing system properties to the junit test. Two of them specifies the system properties one at a time. The last avoids having to forward declare each system property by taking all system properties available to the gradle runtime and passes them to the junit test harness.

tasks.withType<Test> {
    useJUnitPlatform()

    // set system property using a property specified in gradle
    systemProperty("a", project.properties["a"])

    // take one property that was specified when starting gradle
    systemProperty("a", System.getProperty("a"))

    // take all of the system properties specified when starting gradle
    // which avoids copying each property over one at a time
    systemProperties(System.getProperties().toMap() as Map<String,Object>)
}
like image 20
Chris K Avatar answered Oct 18 '22 02:10

Chris K