Pavel Leonov
Pavel Leonov

Reputation: 1

Gradle: different system properties for different tests

I wrote an extension to the Spock. @Configuration with two parameters: environment and file - path to configuration file, parsed groovy.util.ConfigSlurper. Besides the parameters can be specified as system properties: "org.wsw.spock.cfg.file", "org.wsw.spock.cfg.environment". If the system properties are specified, they overwrite the settings specified in the annotation (not sure if that's better, but still left so).

I wrote two tests in different specifications. To run them in gradle wrote this:

task configTest ( type: Test, dependsOn: testClasses) {
    include '**/ConfigurationTest.class'
}

task configSysPropsTest ( type: Test, dependsOn: testClasses) {

    include '**/ConfigurationTestSysProps.class'

    systemProperty 'org.wsw.spock.cfg.file', 'feature_test.gconfig'
    systemProperty 'org.wsw.spock.cfg.environment', 'dev'
}

task test( overwrite: true,
    dependsOn: [ configTest, configSysPropsTest ])

It works, but overwrite task test, it seems to me not very nice. And the reports folder (build/reports/tests) contains information on only one test.

There are ways to run different tests with different system properties?

Upvotes: 0

Views: 926

Answers (2)

aitorpazos
aitorpazos

Reputation: 174

Couldn't you just add different gradle.properties to each test in the setup() method of your test class?

E.g.:

  • Test1: FileUtils.copyFile(new File(loader.getResource('test1.properties').getPath()), new File("${testProjectDir.root.absolutePath}/gradle.properties"))

  • Test2: FileUtils.copyFile(new File(loader.getResource('test2.properties').getPath()), new File("${testProjectDir.root.absolutePath}/gradle.properties"))

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123910

I don't quite understand why you are overwriting the test task. Anyway, the only way to run different tests with different system properties is to have multiple Test tasks. And in that case, you need to configure Test.reportsDir (or Test.reports.html.destination in more recent Gradle versions) explicitly.

Perhaps you could change your plugin so that each test class has its own system property. Then you can set all system properties at once.

Upvotes: 1

Related Questions