How to pass system properties to the tests in gradle in the smart way?
Asked Answered
O

2

6

build.gradle

tasks.withType(Test){
    systemProperties=System.properties
    println systemProperties['param']
}

Now I can either pass parameters in the command line:

gradle test -Dparam=10

or put them in gradle.properties:

systemProp.param=15

Ideally I would like to put the defaults in the gradle.properties, and be able to overwrite them from the command line. Unfortunately if I do that, the gradle.properties has precedence, and -Dparam=10 is ignored.

Could you offer any solutions on that?

Overhear answered 30/6, 2016 at 14:25 Comment(2)
discuss.gradle.org/t/… It seems like system properties defined in gradle.properties currently cannot be overwritten by using -DMolybdous
thank you, updating gradle wrapper to the newest, solved the problem.Overhear
O
1

https://issues.gradle.org/browse/GRADLE-2122

It works since 2.12 or 2.13 "the smart way" already!

The example above is working, the command line -D option overdrives the defaults in gradle.properties

Overhear answered 1/7, 2016 at 15:40 Comment(0)
E
0

I am using gradle 2.12 and sharing how I used it:

test {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}

I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:

//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)

Doing this with gradle required that the system property provided at the time of running gradle build, shown here,

gradle build -Ddeep.test.run=true

was indeed passed through to the tests.

Hope this helps others trying out this approach for running tests conditionally.

Experimentalism answered 28/11, 2016 at 7:20 Comment(1)
I really shocked when systemProperties = System.properties works. But why?Imply

© 2022 - 2024 — McMap. All rights reserved.