Passing system properties to Test task

Hello

I have a feature request: in the Test task, provide a convenient way to specify system properties that should be passed on to the actual JVM running the tests (if the system property is actually set).

For example:

test {

mappedSystemProperties= [‘foo’, ‘bar’]

… }

–> In this example, if the system properties foo and bar are set, they are passed on to the test JVM, otherwise they are ignored.

Regards, Etienne

Not sure if this should be a core functionality of our test tasks. But it’s easy to implement as a custom plugin. E.G:

apply plugin:'java'
apply plugin:SystemPropertiesMappingPlugin
  repositories{
 mavenCentral()
}
  dependencies{
 testCompile "junit:junit:4.10"
}
    test{
 mappedSystemProperties= ['foo', 'bar']
 }
    class SystemPropertiesMappingPlugin implements Plugin<Project>{
   public void apply(Project project){
  project.tasks.withType(Test){ testTask ->
   testTask.ext.mappedSystemProperties = []
       doFirst{
    mappedSystemProperties.each{ mappedPropertyKey ->
     def systemPropertyValue = System.getProperty(mappedPropertyKey)
     if(systemPropertyValue){
      testTask.systemProperty(mappedPropertyKey, systemPropertyValue)
     }
    }
   }
  }
 }
}

regards, René

Hi Rene

I think it should be in the core - it is yet another distinction from Maven, where such a thing is very hard to achieve.

Thanks for the plugin, I will apply it.

Regards, Etienne

Yes, this is very hard to figure out. I searched a lot to find this, and have spent quite some time to find a solution. I’m not applying a plugin, but instead setting a script variable and then using systemProperty to set the properties in the tasks. That seems like a lot of trouble to get that to hand off. Seems 1) that would be documented well as I did not see it in the manual and the search results for various search queries took a while to weed down to this one and 2) at the very minimum there would be some boolean property when defining a task that would cause this to happen. For instance, when one tries to use geb and Selenium, and they need to pass web driver properties or other system properties such as the baseUrl on the command line, then none of that works until you figure this out, and it is a time consuming debug process of a build tool that is supposed to be simpler than Maven and Ant.

1 Like