We can use JUnit 5 @Tag to filter test cases from test plans. It helps us to create multiple different test plans for different environments or different use-cases or any specific requirements. we may execute a set of tests by including/excluding only those tagged tests in the test plan.
@Tag Annotation Usage
We may use @Tag annotation either at test class or test method-level
@Tag(“dev”)
public class MyTest
{
@Test
@Tag(“userManagement”)
void testCaseX(TestInfo testInfo) {
}
}
We can use multiple tags on a single test case so that it can be included it in multiple test plans.
public class MyTest
{
@Test
@Tag(“dev”)
@Tag(“prod”)
void testCaseX(TestInfo testInfo) {
}
}
Create test plans with @IncludeTags and @ExcludeTags
We can use @IncludeTags or @ExcludeTags annotations in your test plan to filter tests or include tests.
@RunWith(JUnitPlatform.class)
@SelectPackages(“com.kkjavatutorials.packageA”)
@IncludeTags(“prod”)
public class MultipleTagsExample
{
}
@RunWith(JUnitPlatform.class)
@SelectPackages(“com.kkjavatutorials.packageA”)
@ExcludeTags(“prod”)
public class MultipleTagsExample
{
}
To add more than one tag we have to pass a string array of tags in the desired annotation.
@RunWith(JUnitPlatform.class)
@SelectPackages(“com.kkjavatutorials.packageA”)
@IncludeTags({“prod”,“dev”})
public class MultipleTagsExample
{
}
Point to be noted here We can’t include both @IncludeTags and @ExcludeTags annotations in single test plan.
JUnit 5 @Tag Example
Let’s say we have 3 tests and we want to run all 3 in dev environment but want to run only one in prod. So, we can tag the tests as below:
public class ClassXTest
{
@Test
@Tag(“dev”)
@Tag(“prod”)
void testCaseX(TestInfo testInfo) {
}
}
public class ClassYTest
{
@Test
@Tag(“dev”)
void testCaseY(TestInfo testInfo) {
}
}
public class ClassCTest
{
@Test
@Tag(“dev”)
void testCaseC(TestInfo testInfo) {
}
}
Let’s create a test plan for both environments.
Tests to run in a production environment
@RunWith(JUnitPlatform.class)
@SelectPackages(“com.kkjavatutorials.package1”)
@IncludeTags(“prod”)
public class ProdTests
{
}
Tests to run in dev environment
@RunWith(JUnitPlatform.class)
@SelectPackages(“com.kkjavatutorials.package1”)
@IncludeTags(“dev”)
public class DevTests
{
}
JUnit 5 @Tag Annotation complete example
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5TagAnnotationExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> <junit.platform.version>1.6.2</junit.platform.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project> |
MyUtils.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
MyUtilsTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); } @Test @Tag(value = "dev") void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @Tag(value = "dev") void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @Tag(value = "prod") @Tag(value = "dev") void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterAll static void tearDown() { myUtils = null; } } |
DevTests.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkjavatutorials.runner; import org.junit.platform.runner.JUnitPlatform; import org.junit.platform.suite.api.IncludeTags; import org.junit.platform.suite.api.SelectPackages; import org.junit.runner.RunWith; @RunWith(value = JUnitPlatform.class) @SelectPackages(value = { "com.kkjavatutorials" }) @IncludeTags(value = {"dev"}) public class DevTests { } |
ProdTests.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkjavatutorials.runner; import org.junit.platform.runner.JUnitPlatform; import org.junit.platform.suite.api.IncludeTags; import org.junit.platform.suite.api.SelectPackages; import org.junit.runner.RunWith; @RunWith(value = JUnitPlatform.class) @SelectPackages(value = { "com.kkjavatutorials" }) @IncludeTags(value = {"prod"}) public class ProdTests { } |
ExcludeTests.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.kkjavatutorials.runner; import org.junit.platform.runner.JUnitPlatform; import org.junit.platform.suite.api.ExcludeTags; import org.junit.platform.suite.api.SelectPackages; import org.junit.runner.RunWith; @RunWith(value = JUnitPlatform.class) @SelectPackages(value = { "com.kkjavatutorials" }) @ExcludeTags(value = {"prod"}) public class ExcludeTests { } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
That’s all about Conditional Test Execution in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.