Search code examples
gradleintegration-testing

How to run a single test that is excluded


I have integration tests named with "IT" at the end and the tests are excluded by default. My build.gradle contains:

test {
    if (!project.hasProperty('runITs')) {
        exclude '**/**IT.class'
    }
}

The check task, which is part of build, no longer runs integration tests. All tests (unit + integration) are executed with defined runITs (e.g. ./gradlew -PrunITs=1 check) if it is necessary.

It works very well. The only drawback is than I can't run single integration test with --test (used by IDE) without runITs defined. The command fails with message: No tests found for given includes: [**/**IT.class](exclude rules).

Is there any way (e.g. build variable) how to recognize the run of single test with --test and skip the exclusion?


Solution

  • I have found a solution. My build.gradle now contains:

    test {
        if (!project.hasProperty('runITs') && filter.commandLineIncludePatterns.empty) {
            exclude '**/**IT.class'
        }
    }
    

    When tests are run with --tests then the list commandLineIncludePatterns in filter is not empty and exclude is not applied.

    Details are obvious in test task source: https://github.com/gradle/gradle/blob/dc8545eb1caf7ea99b48604dcd7b4693e79b6254/subprojects/testing-base/src/main/java/org/gradle/api/tasks/testing/AbstractTestTask.java