Sharing test configs between different test tasks


(Phil Swenson) #1

I need to set some test configuration like a set of System Properties and JVM settings between my unit tests (plain test task) and my integration tests(tests that end in “IntegrationTest”) I tried this:

test {
    systemProperty 'opt.terracotta', 'true'
    systemProperty 'opt.dev.home', '/Users/pswenson/dev/sag/optimize/trunk'
    systemProperty 'base.config.path', '/Users/pswenson/dev/sag/opt/trunk/modules/ae/src/main/resources/conf'
    //tuning the included/excluded tests
    include '**/*UnitTest.*'
    jvmArgs '-Xms128m', '-Xmx512m', '-XX:MaxPermSize=128m'
}
  task integrationTests(type: Test) {
    println ("tcsytemprop = ${System.getProperty('opt.terracotta')}")
    include '**/*IntegrationTest.*'
}

the tcsystemprop was output as null, so the test configuration isn’t shared just by declaring a task type “Test”

What should I do?

thanks!


(René Groeschke) #2

Hello phil, a system property defined in a test task is passed to the JVM that executes the tests. So when you do

test{
    systemProperty 'myProp', 'somePath'
}

the systemProperty myProp is not available in other sections of your build script. Its only available in the context of your executed tests, as they use a JVM, that is configured with this systemProperty.

to share a configuration between your integrationTests task and your test task you can do:

[test, integrationTests].each{
    systemProperty 'opt.terracotta', 'true'
    systemProperty 'opt.dev.home', '/Users/pswenson/dev/sag/optimize/trunk'
    systemProperty 'base.config.path', '/Users/pswenson/dev/sag/opt/trunk/modules/ae/src/main/resources/conf'
      }

If you want configure ALL your tasks of type ‘Test’ in your build you can do

tasks.withType(Test){
    systemProperty 'opt.terracotta', 'true'
    systemProperty 'opt.dev.home', '/Users/pswenson/dev/sag/optimize/trunk'
    systemProperty 'base.config.path', '/Users/pswenson/dev/sag/opt/trunk/modules/ae/src/main/resources/conf'
    ...
}

regards, René


(Phil Swenson) #3

I like your answer, but for some reason it’s not working:

tasks.withType(Test) {

systemProperty ‘opt.terracotta’, ‘true’

systemProperty ‘opt.dev.home’, ‘/Users/pswenson/dev/sag/optimize/trunk’

systemProperty ‘base.config.path’, ‘/Users/pswenson/dev/sag/opt/trunk/modules/ae/src/main/resources/conf’

jvmArgs ‘-Xms128m’, ‘-Xmx512m’, ‘-XX:MaxPermSize=128m’

beforeTest { descriptor ->

logger.lifecycle("Running test: " + descriptor)

}

onOutput { descriptor, event ->

logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message)

} }

test {

println(“testsytemprop = ${System.getProperty(‘opt.terracotta’)}”)

//tuning the included/excluded tests //

include ‘**/UnitTest.

include ‘**/ProcessEventStageStorageUnitTest.’ }

I get output:

testsytemprop = null


(Phil Swenson) #4

maybe it’s because the test configuration happens before the tasks.withType?


(Peter Niederwieser) #5

‘test.systemProperty()’ sets the system property in the test JVM, not in the Gradle JVM. Hence you can’t print it like that.


(René Groeschke) #6

what do you mean with “its not working”? Again, the line

println("testsytemprop = ${System.getProperty('opt.terracotta')}")

in your the configuration of your task ‘test’ will allways print “testsystemprop = NULL”, as you try to load a SystemProperty on the java instance the gradle build uses.

All tasks of type ‘Test’ are executed in a seperate jvm instance. The systemProperty method of the Test task is used to pass system properties that you need in your tests to this newly created JVM instance. what you can do to check that your configuration is correctly, you can write a simple unit test that verifies, that your needed systemproperties are set correctly.


(Phil Swenson) #7

This makes perfect sense, all good now.