Filtering tests with Maven

As we already know, we need to use  maven-surefire-plugin in a Maven project to execute Jupiter test. Moreover, this plugin allows us to filter the test execution in several ways: filtering by JUnit 5 tags and also using the regular inclusion/exclusion support of maven-surefire-plugin.

In order to filter by tags, the properties includeTags and excludeTags of the maven-surefire-plugin configuration should be used. Let's see an example to demonstrate how. Consider the following tests contained in the same Maven project. On the one hand, all tests in this class are tagged with the  functional word.

package io.github.bonigarcia;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("functional")
class FunctionalTest {

@Test
void testOne() {
System.out.println("Functional Test 1");
}

@Test
void testTwo() {
System.out.println("Functional Test 2");
}

}

On the other hand, all tests in the second class are tagged as non-functional and each individual test is also labeled with more tags (performance, security, usability, and so on):

package io.github.bonigarcia;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("non-functional")
class NonFunctionalTest {

@Test
@Tag("performance")
@Tag("load")
void testOne() {
System.out.println("Non-Functional Test 1 (Performance/Load)");
}

@Test
@Tag("performance")
@Tag("stress")
void testTwo() {
System.out.println("Non-Functional Test 2 (Performance/Stress)");
}

@Test
@Tag("security")
void testThree() {
System.out.println("Non-Functional Test 3 (Security)");
}

@Test
@Tag("usability")
void testFour() {
System.out.println("Non-Functional Test 4 (Usability)");
}

}

As described before, we use the configuration keywords includeTags and excludeTags in the Maven pom.xml file. In this example, we include the test with the tag functional and exclude non-functional:

    <build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<properties>
<includeTags>functional</includeTags>
<excludeTags>non-functional</excludeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

As a result, when we try to execute all the tests within the project, only two will be executed (those with the tag functional), and the rest are not recognized as tests:

Maven execution of test filtering by tags
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43