Run one or Exclude one test with Maven

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 (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

Note: Some example require an update of the surefire plugin and JUnit 4.x. See details below.

Maven: Run specific test(s)

Maven: Exclude specific test(s)

Maven Surefire plugin

Maven usually doesn’t provide the latest version of the Surefire plugin by default.
Some examples above require the Surefire plugin to be of version 2.19 or higher.

On how to update the surefire plugin version: see my code example or: http://maven.apache.org/surefire/maven-surefire-plugin/usage.html

Example code:
https://github.com/madegroot/Run-or-Exclude-one-test-with-Maven

Maven reference:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

The Gradle equivalent
https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-gradle

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

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

Leave a Reply

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