Run one or Exclude one test with Gradle

From time to time you only want to run one test, one test method, one class or one package from the command-line.

Or on the contrary: you want to exclude / ignore one specific test or group of tests during the build cycle.

Excluding tests from the build cycle by the command line usually occurs when the following scenarios meet:

  • A test requires significant amount of resources (time, memory, disk space, etc.)
  • The run needs to be independent from the IDE (to reenact the Continuous Integration / Continuous Delivery pipeline) as some IDEs load test-dependencies on the compile-time class-path.
  • You have no or limited ability to change the code-base

Run specific test(s)

Re-run tests

Gradle will not re-run a task if the input and output hasn’t changed.
To rerun the tests, provide an extra argument or task if the test task is UP-TO-DATE

 Exclude specific test(s)

An update to the build file is required to exclude specific tests from running.
There is no command line flag to exclude one or one group of tests, like with Maven:

Exclude a fixed set of tests

To exclude a fixed set of tests, update the test task in the build file with an exclusion pattern.

Dynamically exclude tests from the command-line

Defining the exclusions during build time provides more flexibility.
To accomplish this, introduce a custom property that can be used at execution time from the command-line.

Now use the custom property when running tests from the command-line.

Code Example:

Gradle reference

Maven equivalent

One thought on “Run one or Exclude one test with Gradle

  1. Pingback: Run one or Exclude one test with Maven - JDriven

Leave a Reply

Your email address will not be published. Required fields are marked *