Friday, 16 August 2019 14:46

Run Tagged Tests in Maven

Written by
Rate this item
(0 votes)

It is common to place a mix of tests in your projects. Some run fast, some slow and others are more integration testing than unit testing. This can cause problems in DevOps practices when you need to compile a project with a simple change and don't want to wait 15 minutes for all the tests to run. This is not an example of fast feedback. Here is how you call a Maven build process in a way to run just the tests you want.

The Simple Way

If you research this topic you will see developers suggesting creating properties in your project, adding profile sections to populate those properties, and possibly even configuring the Maven Surefire plugin. While those approaches will work, there is a much simpler approach.

The Surefire plugin supports two properties called groups and excludedGroups to respectively include and exclude any JUnit 5, JUnit 4 and even TestNG tags. So to execute tests tagged with slow or fast you can run the following from the command line:

mvn test -Dgroups=fast,slow

This simply sets the groups property from the command line. The Surefire plugin then uses it to select the tests it executes. It is not necessary to alter the pom.xml file to run tagged tests. The use of profiles is not necessary.

By the way; if there is nothing in that property, all tests are run.

Tagging Tests

Just a reminder, the tagging of tests is performed with annotations:

public class ClassATest
    void testCaseA(TestInfo testInfo) {

Again, this is a simple way to manage test runs with tags.

Last modified on Friday, 16 August 2019 15:17
Steve Cote

Steve has been a systems architect for the telecommunications and electric utility industries creating circuit provisioning systems and smart grid solutions. He is a hands-on architect working with teams to deliver large complex projects. More recently, Steve has been consulting agile teams from Fortune 15 organizations, start-ups, and everything in-between to be more productive with DevOps and agile practices.

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.