Gradle: how to exclude some tests?
Asked Answered
F

5

45

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.

Flopeared answered 5/3, 2014 at 21:2 Comment(0)
E
33

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/**'

  // ...
}
Eckert answered 5/3, 2014 at 21:5 Comment(7)
--@JB Nizet: This will exclude some tests permanently. It is not flexible. What if I want to run all tests sometimes? Is it possible to create a task, say, unitTest that extends the test task but with some filtering?Flopeared
Create another task called cucumberTest, of type Test, including only the cucumber tests, and a third task called allTests depending on test and on cucumberTest. You can then run test to run the unit tests, cucumberTest to run the cucumber tests, and allTests to run everything.Eckert
@user447607 what? I literally copied and pasted this example from the documentation. Have you at clicked the link?Eckert
OK I found it but it still isn't working for me. Naturally, I tried it first.Forbidding
Scroll down to see JB Nizet's suggestion in code: https://mcmap.net/q/367359/-gradle-how-to-exclude-some-testsSafeguard
org/foo/** are paths or packages?Hyperostosis
paths. But the directory structure is supposed to match the package structure, so it doesn't change much.Eckert
F
40

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/**/

Flopeared answered 5/3, 2014 at 22:50 Comment(1)
If you are running tests with ./gradle build, I think you need to add the -x test on ./gradlew unitTest to avoid running both test and unitTest when doing a build as well.Hess
E
33

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/**'

  // ...
}
Eckert answered 5/3, 2014 at 21:5 Comment(7)
--@JB Nizet: This will exclude some tests permanently. It is not flexible. What if I want to run all tests sometimes? Is it possible to create a task, say, unitTest that extends the test task but with some filtering?Flopeared
Create another task called cucumberTest, of type Test, including only the cucumber tests, and a third task called allTests depending on test and on cucumberTest. You can then run test to run the unit tests, cucumberTest to run the cucumber tests, and allTests to run everything.Eckert
@user447607 what? I literally copied and pasted this example from the documentation. Have you at clicked the link?Eckert
OK I found it but it still isn't working for me. Naturally, I tried it first.Forbidding
Scroll down to see JB Nizet's suggestion in code: https://mcmap.net/q/367359/-gradle-how-to-exclude-some-testsSafeguard
org/foo/** are paths or packages?Hyperostosis
paths. But the directory structure is supposed to match the package structure, so it doesn't change much.Eckert
S
17

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*'
   }
}
Stratification answered 3/11, 2017 at 13:47 Comment(0)
P
6

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
Puttee answered 13/5, 2020 at 15:50 Comment(1)
reference Mathijs de Groot: blog.jdriven.com/2017/10/…Begorra
S
0

With Gradle + Kotlin in your build.gradle.kts

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


gradle test -Dtest.profile=integration
Spirit answered 2/2 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.