JUnit 5 @Tag Annotation example

By | October 3, 2020

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

MyUtils.java

MyUtilsTest.java

DevTests.java

ProdTests.java

ExcludeTests.java

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.

Leave a Reply

Your email address will not be published. Required fields are marked *