Open In App

JUnit 5 – @Tag

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JUnit 5 testing framework provides a lot of features for testing the application. In those features, Tagging is one of them. This tagging is created using @Tag Annotation JUnit 5. JUnit 5 @Tag is used for filtering the test cases based on the given criteria like characteristics, purpose, and features. @Tag is working like a label in the Test Suite. This @Tag Annotation is used in two different levels. The first one is class level and the other one is method level. This means we can apply this Annotation for both test methods and test classes in the test suite of an application.

  • @Tag with class level
  • @Tag with method level

Syntax for Test class

@Tag("classTag")
public class MyTestClass {
@Test @Tag("methodTag") void testMethod()
{
// Test logic
}
}

Syntax for Test method

public class MyTestClass {
@Test @Tag("methodTag") void testMethod()
{
// Test logic here
}
}

Now we will discuss both of them with examples.

Class-Level @Tag Example

Java




import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
  
@Tag("classTag")
public class MyTest {
  
    @Test
    @Tag("unitTest")
    void exampleOne() {
        System.out.println("Executing unit test @Tag");
    }
  
    @Test
    @Tag("integrationTest")
    void exampleTwo() {
        System.out.println("Executing integration test @Tag");
    }
}


In this above example we have created one class that is MyTest. In above class we have created one tag by @Tag with label classTag. In that class we have created two methods by using @Tag. This Annotation can filter the class as well as methods based on tag names.

Method-Level @Tag Example

Java




import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
  
public class MathTest {
  
    @Test
    @Tag("addition")
    void testAddition() {
        System.out.println("Executing test testAddition");
    }
  
    @Test
    @Tag("subtraction")
    void testSubtraction() {
        System.out.println("Executing test testSubtraction");
    }
}


Here we have created another class that is MathTest, in that class we have created two methods for those classes we have created addition and subtraction tags by using @Tag annotation.

Advantages of JUnit 5 – @Tag

  • Categorization and Organization is possible by using this @Tag Annotation
  • We can be able to perform Selective Execution in the Test Suite
  • Filtering in IDEs and Build Tools
  • Parallel Execution is achieved by using this annotation.
  • Documentation is possible means this Documentation provides information about characteristics, purpose and features.
  • Tags can be used in conjunction with conditional test execution.
  • Integration with Test Reports.

Conclusion

@Tag annotation is provides a flexible and powerful mechanism for manage and controlling test execution of the test cases in JUnit 5. Mostly this Annotation is used for filtering the test cases in the test suite. And it is used in parallel execution while testing an application due to parallel execution time also saved for testing.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads