JUnit 5 @Tag Annotation Example

JUnit 5 @Tag can be used to filter testcases from test plans. It can help in create multiple different test plans for different environments, different use-cases or any specific requirement. You can execute set of tests by including only those tagged tests in test plan OR by excluding other tests from test plan.

1. @Tag Annotation Usage

  1. We can apply the @Tag annotation on test class or test method or both.
    @Tag("development")
    public class ClassATest
    {
    	@Test
    	@Tag("userManagement")
    	void testCaseA(TestInfo testInfo) {
    	}
    }
    
  2. We can apply multiple tags on single test case as well, so that you can include it in multiple test plans.
    public class ClassATest
    {
    	@Test
    	@Tag("development")
    	@Tag("production")
    	void testCaseA(TestInfo testInfo) {
    	}
    }
    

2. Create test plans with @IncludeTags and @ExcludeTags

We can use @IncludeTags or @ExcludeTags annotations in your testplan to filter tests or include tests.

//@IncludeTags example

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples")
@IncludeTags("production")
public class MultipleTagsExample 
{
}

//@ExcludeTags example

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples")
@ExcludeTags("production")
public class MultipleTagsExample 
{
}

To add more than one tag, pass a string array of tags in desired annotation.

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples")
@IncludeTags({"production","development"})
public class MultipleTagsExample 
{
}
We CAN NOT include both @IncludeTags and @ExcludeTags annotations in single test plan.

3. JUnit 5 @Tag Example

Let’s say we have 3 tests and we want to run all 3 in development environment; but want to run only one in production. So we will tag the tests as below:

public class ClassATest
{
	@Test
	@Tag("development")
	@Tag("production")
	void testCaseA(TestInfo testInfo) { //run in all environments
	}
}

public class ClassBTest
{
	@Test
	@Tag("development")
	void testCaseB(TestInfo testInfo) {
	}
}

public class ClassCTest
{
	@Test
	@Tag("development")
	void testCaseC(TestInfo testInfo) {
	}
}

Let’s create test plan for both environments.

Tests to run in production environment

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples")
@IncludeTags("production")
public class ProductionTests 
{
}
JUnit 5 @Tag Example - Production Tests
JUnit 5 @Tag Example – Production Tests

Tests to run in development environment

@RunWith(JUnitPlatform.class)
@SelectPackages("com.howtodoinjava.junit5.examples")
@IncludeTags("development")
public class DevelopmentTests 
{
}
JUnit 5 @Tag Example - Development Tests
JUnit 5 @Tag Example – Development Tests

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “JUnit 5 @Tag Annotation Example”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.