How To Tag and Filter JUnit 5 Tests | JUnit 5 Tutorial | Part - VII | LambdaTest

:star2: New Tutorial Alert!

Dive into the world of JUnit 5 with our latest guide on Tagging and Filtering :hammer_and_wrench::white_check_mark:. Streamline your testing process by mastering how to organize and execute your tests efficiently. Don’t miss out on this essential tutorial. Watch the session now! #JUnit5 #TaggingAndFiltering #TestingExcellence

By Using @Tag Annotation: In JUnit 5, you can use the @Tag annotation to categorize your tests. This allows you to group tests by certain characteristics, such as “fast” or “slow”. You can then run tests selectively based on these tags. For instance, you can configure your build tool (e.g., Maven or Gradle) to run only tests with the “fast” tag during a typical build process, reserving the “slow” tests for more comprehensive testing phases.

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class TagExampleTest {

    @Test
    @Tag("fast")
    void fastTest() {
        System.out.println("Fast test");
    }

    @Test
    @Tag("slow")
    void slowTest() {
        System.out.println("Slow test");
    }
}

To learn more about annotation in JUnit, follow this guide and get detailed insights.

Filtering Tests with Maven Surefire Plugin: You can filter which tests to run based on tags using the Maven Surefire Plugin. In this example, the Surefire Plugin is configured to run only tests that are tagged with “fast”. This allows more control over the test suite execution directly from the Maven build lifecycle. By setting the to fast, only tests with the @Tag(“fast”) annotation will be executed, optimizing the testing process for different build environments.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <properties>
                    <property>
                        <name>groups</name>
                        <value>fast</value>
                    </property>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

Filtering Tests Programmatically: You can programmatically filter tests using JUnit 5’s extension model. In the example, a custom TagFilter extension implements TestExecutionCondition to check the tags on each test method. If the test is tagged with integration, it is skipped. This approach provides dynamic and flexible test filtering directly in the code, allowing for complex conditional test execution logic based on tags.

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.TestExecutionCondition;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExtensionContext;

public class ExampleTest {

    @Test
    @Tag("integration")
    void integrationTest() {
        System.out.println("Integration test executed");
    }

    @Test
    @Tag("unit")
    void unitTest() {
        System.out.println("Unit test executed");
    }

    @ExtendWith(TagFilter.class)
    public static class TaggedTests {
    }

    public static class TagFilter implements TestExecutionCondition {

        @Override
        public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
            boolean isIntegrationTest = context.getTags().contains("integration");
            return isIntegrationTest ? ConditionEvaluationResult.disabled("Skipping integration tests") 
                                     : ConditionEvaluationResult.enabled("Running unit tests");
        }
    }
}