Multi module build fails with --tests filter

With a gradle multi-module project, when you run a task from the root project folder, it applies that task to all projects that have that task, skipping subprojects that don’t.

When I do the same with the --tests parameter, I get a failure if a subproject doesn’t have a matching test. My expectation would be that gradle would not run any tests in that subproject in this case - but not error and fail.

Is there a way to use --tests and not get this failure?

A little more digging and I came across the TestFilter interface which allows you to invoke the setFailOnNoMatchingTests boolean:

https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFilter.html#setFailOnNoMatchingTests-boolean-

I have configured my task as

           task('integTest', type: Test, overwrite: true) {
	            testClassesDirs = sourceSets.integTest.output.classesDirs
	            classpath = sourceSets.integTest.runtimeClasspath

                filter {
                    setFailOnNoMatchingTests(false)
                }
            }