Skip to main content

Test scope configuration

Once many tests are present in your repository it may be necessary to have the capability to run only a certain subset of tests each time.

There are many ways how to select specific tests to be run:

  • The Surefire Maven plugin allows you specifying a complex selector of test to be run using the argument -Dtest=[pattern]
  • The JUnit framework allows specifying test suites or even writing your own test discovery routines
  • IDEs usually allow you to run all tests in one class or even all tests in one package

Surefire test discovery

By default, the Surefire plugin will run any methods annotated with @Test present under the path src/test/java/* given the class names follow a naming convention. Have a look in the Surefire documentation. We have disabled the convention in our library hence all methods having the annotations will be run.

Surefire allows then to configure inclusions and exclusions either in the pom.xml file or using the commandline argument -Dtest=[pattern]. Have a look in the Surefire documentation on what is syntax of the pattern.

A good idea is to structure your tests in a tree structure that corresponds to test suites that you will be susceptible to run separately. Options are:

  • By business areas / By systems / By types of tests
  • By types of tests / By business area / By systems
  • By types of tests / By systems
  • etc...

Then it will be very easy to pass the root of a sub-tree (or a couple of those) as argument -Dtest=.

JUnit test suites

JUnit allows defining test suites using annotations like @RunWith, @SelectClasses and @SelectPackages.

Simply create a class as follows:

@RunWith(JUnitPlatform.class)
@SelectClasses({
MyTest1.class,
MyTest2.class
})
public class TestSuite {
}

By running tests in this class tests from all mentioned classes will be run.

As you may guess the main difference with the Surefire test discovery is that here the test suites are defined in the code while when using the Surefire test discovery based on patterns the scope of tests to be run can be passed as a runtime argument.

IDE based test discovery

When running tests from your IDE, you may want to use the basic test discovery it allows. Three options are available in all IDEs:

  • Running a single test
  • Running all tests in a class
  • Running all tests in a package - since packages are constructed as a tree, you may be able to run many tests by running tests for a top level package

Of course test discover using your IDE is there only for your convenience when testing tests. Once tests are run from a pipeline unattended, you will need to work with the previous test discovery options.