Gradle: how to exclude some tests?

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

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

  // ...
}

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

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