Gradle - Chaining test tasks with `--tests` filter

I have been at this for a few days now, let’s say I have a build.gradle as follows:

ext.createRunIntegrationTestsTask = { String taskName ,  String buildPlatform ->
	return project.tasks.create(taskName, Test.class) {
		dependsOn buildPlatform
        [...]
	}
}

def Platforms = ["A", "B"]
for (platform in Platforms) {
	println "Creating task: runIntegrationTestsPlatform$platform"
	createRunIntegrationTestsTask("runIntegrationTestsPlatform$platform", "buildPlatform$platform")
}
    
task runIntegrationTest(type: Test) {
	dependsOn 'runIntegrationTestPlatformA'
	dependsOn 'runIntegrationTestPlatformB'
}

I want to be able to run the following command with the tests filter:

gradlew :runIntegrationTest --tests "com.somestuff.methodTest"

For some reason when the above command runs, it does not pass the tests filter to runIntegrationTestPlatformA and runIntegrationTestPlatformB task. Both the runIntegrationTestPlatform... tasks run the whole suite of integration tests.

The current behavior I get is for runIntegrationTestPlatform... tasks to run the whole integration test suite for the specified platform and runIntegrationTest to run the integration test suite for all platforms.

But, when I pass the --tests filter to runIntegrationTest task, it does not get propagated to the chained tasks, i.e. runIntegrationTestPlatformA and runIntegrationTestPlatformB. Instead, it runs the whole integration test suite for both platforms. I want runIntegrationTestPlatformA and runIntegrationTestPlatformB to use the tests filter passed to runIntegrationTest to only run the specified integration tests.

Is there a way to get runIntegrationTestPlatformA and runIntegrationTestPlatformB to run from runIntegrationTest with the --tests filter.

Any help would be appreciated. Thank you.

Additional Sources:

  1. groovy - Refactor duplicated code in gradle task "type: Copy" - Stack Overflow

--tests is not a magically global option.
It is an option to a task of type Test.
So with your command you tell the runIntegrationTest task to only run this test.
But that task does not have any tests anyway.
You either need to forward the filter property of that task into the concrete tasks or use a different strategy like not having multiple tasks, or having a task running all and filtering there or whatever.