Table of contents
1.
Introduction
2.
Tagging Tests with @Tag
3.
@Tag Example
3.1.
testClass1 with A and B tag
3.2.
testClass2 with B and C tag
3.3.
testClass3 with C and A tag
3.4.
testSuite1 with A tags
3.5.
testSuite2 with B tags
3.6.
testSuite3 with C tags
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Tagging and Filtering JUnit Tests

Author Aditya Anand
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn about Tagging and Filtering JUnit Tests.
Sometimes we want to filter our tests with a particular test, which is beneficial when we have a large number of tests. JUnit 5 @Tag can be used to filter test cases from test suites. 
We can exclude a set of tests by including and excluding only those tagged tests in the test plan.

Let's get started, and I hope you learn a lot from this tutorial.

Tagging Tests with @Tag

Test classes and methods can be tagged in the JUnit 5 using @Tag annotation. Those tags can later be used to filter test discovery and execution.

Rules to create a tag are following:

  • A tag must not be null or blank.
  • A trimmed tag must not contain whitespace.
  • A trimmed tag must not contain ISO control characters.
  • A trimmed tag must not have any of the following reserved characters.

@Tag Example

Let’s create example classes with A, B, C tags and group them using tags.

testClass1 with A and B tag

package com.aditya04848.junit.helper;
import  org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;
public class testClass1
{
    @Test
    @Tag("A")
    @Tag("B")
    public void test(){
        System.out.println("Test Class 1");
        assertTrue(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

testClass2 with B and C tag

package com.aditya04848.junit.helper;
import  org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;
public class testClass2
{
    @Test
    @Tag("B")
    @Tag("C")
    public void test() {
        System.out.println("Test Class 2");
        assertTrue(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

testClass3 with C and A tag

package com.aditya04848.junit.helper;
import  org.junit.jupiter.api.Test;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Tag;

public class testClass3
{
    @Test
    @Tag("C")
    @Tag("A")
    public void test(){
        System.out.println("Test Class 3");
        assertTrue(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

testSuite1 with A tags

We will create a test suite that will group all classes with an A tag.

package com.aditya04848.junit.helper;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("A")
public class testSuite1{
}
You can also try this code with Online Java Compiler
Run Code

testSuite2 with B tags

This test suite class groups all classes with a B tag.

package com.aditya04848.junit.helper;

import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("B")
public class testSuite2{
}
You can also try this code with Online Java Compiler
Run Code

testSuite3 with C tags

This test suite class groups all classes with a C tag.

package com.aditya04848.junit.helper;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages("com.aditya04848.junit.helper")
@IncludeTags("C")
public class testSuite3{
}
You can also try this code with Online Java Compiler
Run Code

FAQs

  1. How to use @tag In JUnit 5?
    JUnit 5 @Tag can filter test cases from test suites. We can provide tags to different classes and tests and filter them as per our needs. 
     
  2. What is JUnit?
    JUnit is a simple, open-source framework to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
     
  3. What is @Test annotation in JUnit?
    The annotated methods represent the test cases in the class. Each annotated with @Test in a JUnit class, there could be multiple methods. This implies that a class may have numerous test cases.
     
  4. When to use @before/@beforeeach annotation in JUnit?
    This annotation can be used before each test’s initiation, so the code under this annotation will execute before each test.

Key Takeaways

In this article, we have extensively discussed Tagging and Filtering JUnit Tests. We have learned how to use the @Tag to tag a class or a test. We have cerated example classes with different tags and created test suites corresponding to those tags.

We hope that this blog has helped you enhance your knowledge regarding Tagging and Filtering JUnit Tests and if you would like to learn more, check out our articles on JUnit exception TestJUnit Time Test. Do upvote our blog to help other ninjas grow.

Check out JUnit Interview Questions here.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

Live masterclass