After you have finished this lesson, you:
- Can select the invoked test cases by using tag expressions.
- Know how you can filter tests with Maven.
- Can filter tests with Gradle.
Let’s begin.
- You can run your tests with Maven or Gradle.
- You are familiar with JUnit 5 tags.
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 |
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 tests with Maven.
Filtering 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 & B</groups> </configuration> </plugin>
&
.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) & !integrationTest</groups> </configuration> </plugin>
Let’s move on and find out how you can filter tests with Gradle.
Filtering 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.
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
andexcludedGroups
configuration options. - If you are using Gradle, you can include and exclude test methods by using the
includeTags
andexcludeTags
configuration options.
Get the sample code from Github
These steps are the same for all exercises of the Filtering Tests With Maven and Gradle lesson. In other words, before you start an exercise, make sure that you have finished these steps:
1. Clone the Git repository that contains the sample applications of this course. If you have already cloned that repository, you can ignore this step.
2. Open the project found from the introduction-to-junit5/selecting-invoked-test-methods/exercises/test-filtering-exercise directory with your favorite IDE.
Remember to revert your changes after you have finished an exercise!
This exercise helps you to understand how you can configure Gradle to run only the tests which have the specified tag. You can finish this exercise by following these steps:
1. Configure Gradle to run the tests which have the tag: ‘apiTest’.
2. Run your tests with Gradle and ensure that only the apiTest()
method of the TodoItemApiTest
class is run.
This exercise helps you to understand how you can configure Gradle to run only the tests which have all specified tags. You can finish this exercise by following these steps:
1. Configure Gradle to run the tests which have the tags: ‘unitTest’ and ‘apiTest’.
2. Run your tests with Gradle and ensure that only the apiTest()
method of the TodoItemApiTest
class is run.
This exercise helps you to understand how you can configure Gradle to run only the tests which have at least one of specified tags. You can finish this exercise by following these steps:
1. Configure Gradle to run the tests which have the tags: ‘unitTest’ or ‘serviceTest’.
2. Run your tests with Gradle and ensure that all test methods are run.
This exercise helps you to understand how you can configure Gradle to run only the tests which have the tag A and don’t have the tag B. You can finish this exercise by following these steps:
1. Configure Gradle to run the tests have the tag: ‘unitTest’ and don’t have the tag: ‘apiTest’.
2. Run your tests with Gradle and ensure that only the serviceTest()
method of the TodoItemServiceTest
class is run.
This exercise helps you to understand how you can configure Maven to run only the tests which have the specified tag. You can finish this exercise by following these steps:
1. Configure Maven to run the tests which have the tag: ‘apiTest’.
2. Run your tests with Maven and ensure that only the apiTest()
method of the TodoItemApiTest
class is run.
This exercise helps you to understand how you can configure Maven to run only the tests which have all specified tags. You can finish this exercise by following these steps:
1. Configure Maven to run the tests which have the tags: ‘unitTest’ and ‘apiTest’.
2. Run your tests with Maven and ensure that only the apiTest()
method of the TodoItemApiTest
class is run.
This exercise helps you to understand how you can configure Maven to run only the tests which have at least one of specified tags. You can finish this exercise by following these steps:
1. Configure Maven to run the tests which have the tags: ‘unitTest’ or ‘serviceTest’.
2. Run your tests with Maven and ensure that all test methods are run.
This exercise helps you to understand how you can configure Maven to run only the tests which have the tag A and don’t have the tag B. You can finish this exercise by following these steps:
1. Configure Maven to run the tests have the tag: ‘unitTest’ and don’t have the tag: ‘apiTest’.
2. Run your tests with Maven and ensure that only the serviceTest()
method of the TodoItemServiceTest
class is run.