JBT
JBT

Reputation: 8746

Gradle: how to exclude some tests?

My src/test/ folder includes both unit and functional tests. The classpath of functional tests has the word cucumber, whereas the unit tests do not. So, how can I run the unit tests only?

Thank you very much.

P.S.: I know it is easy to use the "include" logic to select tests. For example, to only run the functional tests in my case, I can simply use this
./gradlew test -Dtest.single=cucumber/**/
However, I don't know how to exclude tests in a simple way.

BTW, I am using gradle 1.11.

Upvotes: 45

Views: 64521

Answers (5)

Inanc Cakil
Inanc Cakil

Reputation: 336

With Gradle + Kotlin in your build.gradle.kts

tasks.test {
    if (System.getProperty("test.profile") != "integration") {
        exclude("**/*IntegrationTest*")
    }
}


gradle test -Dtest.profile=integration

Upvotes: 0

Gayan Weerakutti
Gayan Weerakutti

Reputation: 13715

You can also define a custom flag in your build.gradle:

test {
    if (project.hasProperty('excludeTests')) {
        exclude project.property('excludeTests')
    }
}

Then in the command-line:

gradle test -PexcludeTests=com.test.TestToExclude

Upvotes: 6

shabinjo
shabinjo

Reputation: 1561

You can exclude this based on the external system properties.

-Dtest.profile=integration

and in build.gradle

test {
    if (System.properties['test.profile'] != 'integration') {
    exclude '**/*integrationTests*'
   }
}

Upvotes: 17

JBT
JBT

Reputation: 8746

Credit: This answer is inspired by JB Nizet's answer. It is posted because it is more direct to my question.

To run the unit tests only, create a new task like this:

task unitTest( type: Test ) {
    exclude '**/cucumber/**'
}

This way we have:
run all tests: ./gradlew test
run all unit tests: ./gradlew unitTest
run all functional tests: ./gradlew test -Dtest.single=cucumber/**/

Upvotes: 40

JB Nizet
JB Nizet

Reputation: 691625

The documentation of the task explains it, with an example and everything:

apply plugin: 'java' // adds 'test' task

test {
  // ...

  // explicitly include or exclude tests
  include 'org/foo/**'
  exclude 'org/boo/**'

  // ...
}

Upvotes: 33

Related Questions