Configure test task with different system properties

I have a custom Test task that sets the systemProperties based on a system property passed to the gradle runner.

task integrationTests(type: Test) {
    description = 'Runs integration tests'
    group = 'verification'
    systemProperties = [
            'itRun': System.getProperty('itRun')
    ]
} 

This works just fine when I pass use -DitRun=xxx

What I would like to do however is set up some additional tasks that would set set a specific value for itRun and then run my Test task.

task myIT {
  System.setProperty 'itRun', 'theseTests'
   finalizedBy integrationTests
}

However, I’m struggling to see how to set the system property from other tasks because my Test task is configured before my other task and so the value is not being configured for the test VM.

Is there a way to defer configuring system properties for the Test task to execution time or a better way of doing this?

btw I perhaps should say I define the integrationTests task through a custom plugin that I apply to various sub projects, so I’m trying to create the custom tasks within a particular sub project.

TIA
Craig

It is usually a bad idea trying to configure one task from another.
Especially like you showed it and also correctly said, you change the itRun property whenever the myIT task is configured, whether it is executed or not, besides that you don’t know in which order tasks are configured.

If you really want to do such a construct - I really discourage you - add a jvmArgumentProvider to the test task that then returns a -D parameter directly instead of using systemProperties.

But it would most probably be a much better idea to have several tasks pointing at the same tests that are configured accordingly directly.

Thanks.
I did get a version working using project ext properties but have now gone down configuring independent test tasks with filters.

1 Like

Using ext / extra properties is practically always a work-around for doing something not properly :smiley: