Get tests to run from gradle commandline

Viewed 383

My tests are run via gradle like

./gradlew -Denv=remote clean test --tests '*com.examle.Test*'

and before tests executing I need to get argument that specified tests to run (*com.examle.Test*).

Is it possible to extract this argument in build.gradle file?

I assume something like

tasks.withType(Test) {
    def testsToExecute = ??? //need to get "*com.examle.Test*"
}

Thank you in advance.

1 Answers

If you’d like to get what is set on the command line, then there doesn’t seem to be any official way of getting that information. Even if you use internal APIs, then the patterns are only accesible at task execution time (i.e., not yet in the build configuration phase). The following works with Gradle 7.1 at least:

tasks.withType(Test) {
    // only at task execution time:
    doFirst {
        // "getCommandLineIncludePatterns()" is part of the internal API only
        def testsToExecute = filter.commandLineIncludePatterns
        // "filter.includePatterns" would use the public API but doesn't contain
        // the patterns from the command line, unfortunately
    }
}

If you’d like to hardcode the tests to run, then you’re probably looking for the filter method of the Test task. Something like the following should work:

test.filter {
    includeTestsMatching '*com.examle.Test*'
}
Related