'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 1:[1]

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

Solution 2:[2]

try invoking the test task to the specific sub-project. if the test is root try

gradle -PrunITs=1 :test --tests 'MServiceTest'

else

gradle -PrunITs=1 :sub-prj:test --tests 'MServiceTest'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jirkabs
Solution 2 PrasadU