Filtering JUnit 5 Tests With Maven and Gradle

This is a free sample lesson of my Introduction to JUnit 5 course. My JUnit 5 course has 24 lessons, 47 exercises, and 13 quizzes which help you to work smarter and save time when you are writing tests with JUnit 5.

After you have finished this lesson, you:

  • Can select the invoked test cases by using tag expressions.
  • Know how you can filter JUnit 5 tests with Maven.
  • Can filter JUnit 5 tests with Gradle.

Let's begin.

This lesson assumes that:

Introduction to Tag Expressions

Tag expressions are boolean expressions which allow you to specify the test methods which are run when you run your tests. When you specify tag expressions, you can combine tag names with the following operators:

Operator Description Associativity
! not right
& and left
| or left
The previous table lists the operators in the descending order of precedence. Also, you can adjust the operator precedence by using parentheses (aka round brackets).

Let's take a look at some examples which demonstrate how you can use tag expressions for selecting the invoked test methods.

First, if you want to run all tests which have the tag 'A', you have to use a tag expression that looks as follows:

A

Second, if you want to run all tests which have the tag 'A' or the tag 'B', you have to use a tag expression that looks as follows:

A | B

Third, if you want to run all tests which have the tag 'A' and the tag 'B', you have to use a tag expression that looks as follows:

 
A & B

Fourth, if you want to run all tests which have the tag 'A' and don't have the tag 'B', you have to use a tag expression that looks as follows:

A & !B

Fifth, if you want to run all tests which have the tag 'unitTest' or the tag 'integrationTest' and have the tag 'A' or the tag 'B', you have to use a tag expression that looks as follows:

(unitTest | integrationTest) & (A | B)

Next, you will learn to filter JUnit 5 tests with Maven.

Filtering JUnit 5 Tests With Maven

When you are running your tests by using either the Maven Surefire or the Maven Failsafe plugin and you want to configure the test methods which are run when you run your tests, you can use these two configuration options:

  • The groups configuration option allows you to configure the tag expression which specifies the test methods which are run when you run your tests.
  • The excludedGroups configuration option allows you to configure the tag expression which specifies the test methods which aren't run when you run your tests.

Let's take a look at some examples which demonstrate how you can use these configuration options.

First, let's assume that you want to run all tests which have the tag 'A'. After you have made the required changes to the configuration of the Maven Surefire plugin, its configuration looks as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>A</groups>
    </configuration>
</plugin>

Second, let's assume that you want to run all tests which have the tags 'A' and 'B'. After you have made the required changes to the configuration of the Maven Surefire plugin, its configuration looks as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>A &amp; B</groups>
    </configuration>
</plugin>
If you want to use the ampersand character ('&') in an XML document, you must use a CDATA section or use the entity &amp;.

Third, let's assume that you want to run all tests which have the tag 'A' or the tag 'B' and don't have the tag 'integrationTest'. After you have made the required changes to the configuration of the Maven Surefire plugin, its configuration looks as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>A | B</groups>
        <excludedGroups>integrationTest</excludedGroups>
    </configuration>
</plugin>

On the other hand, if you don't want to use two different configuration options, you can remove the excludedGroups configuration option and use a tag expression that excludes test methods which have the tag 'integrationTest'. After you have made the required changes to the configuration of the Maven Surefire plugin, its configuration looks as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <groups>(A | B) &amp; !integrationTest</groups>
    </configuration>
</plugin>

Let's move on and find out how you can filter JUnit 5 tests with Gradle.

Filtering JUnit 5 Tests With Gradle

When you are running your tests with Gradle and you want to configure the test methods which are run when you run your tests, you have to change the configuration of the Test task which runs your tests. To be more specific, you can use these two configuration options:

  • The includeTags configuration option allows you to configure the tag expression which specifies the test methods which are run when you run your tests.
  • The excludeTags configuration option allows you to configure the tag expression which specifies the test methods which aren't run when you run your tests.

Let's take a look at some examples which demonstrate how you can use these configuration options.

First, let's assume that you want to run all tests which have the tag 'A'. After you have made the required changes to the configuration of the Test task, its configuration looks as follows:

test {
    useJUnitPlatform {
        includeTags 'A'
    }
}

Second, let's assume that you want to run all tests which have the tags 'A' and 'B'. After you have made the required changes to the configuration of the Test task, its configuration looks as follows:

test {
    useJUnitPlatform {
        includeTags 'A & B'
    }
}

Third, let's assume that you want to run all tests which have the tag 'A' or the tag 'B' and don't have the tag 'integrationTest'. After you have made the required changes to the configuration of the Test task, its configuration looks as follows:

test {
    useJUnitPlatform {
        includeTags 'A | B'
        excludeTags 'integrationTest'
    }
}

On the other hand, if you don't want to use two different configuration options, you can remove the excludeTags configuration option and use a tag expression that excludes test methods which have the tag 'integrationTest'. After you have made the required changes to the configuration of the Test task, its configuration looks as follows:

test {
    useJUnitPlatform {
        includeTags '(A | B) & !integrationTest'
    }
}

You can now use tag expressions and filter tests with Maven and Gradle. Let's summarize what you learned from this lesson.

This is a free sample lesson of my Introduction to JUnit 5 course. My JUnit 5 course has 24 lessons, 47 exercises, and 13 quizzes which help you to work smarter and save time when you are writing tests with JUnit 5.

Summary

This lesson has taught you four things:

  • Tag expressions are boolean expressions which allow you to specify the test methods which are run when you run your tests.
  • When you want to create a new tag expression, you have to combine tag names with logical operators.
  • If you are using Maven, you can include and exclude test methods by using the groups and excludedGroups configuration options.
  • If you are using Gradle, you can include and exclude test methods by using the includeTags and excludeTags configuration options.

Get the sample code from Github

0 comments… add one

Leave a Reply