0
0
JUnittesting~10 mins

Selecting tests by tags and classes in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test suite demonstrates how to select and run specific tests in JUnit 5 by using tags and test classes. It verifies that only tests with the specified tag or in the specified class are executed.

Test Code - JUnit 5
JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.TagFilter;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TagAndClassSelectionTest {

    @Tag("fast")
    @Test
    void fastTest() {
        assertEquals(2, 1 + 1);
    }

    @Tag("slow")
    @Test
    void slowTest() {
        assertEquals(4, 2 * 2);
    }

    @Test
    void untaggedTest() {
        assertEquals(3, 1 + 2);
    }

    public static void main(String[] args) {
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        // Build request to select tests tagged with 'fast'
        LauncherDiscoveryRequest requestByTag = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectClass(TagAndClassSelectionTest.class))
                .filters(TagFilter.includeTags("fast"))
                .build();

        Launcher launcher = LauncherFactory.create();
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(requestByTag);

        long testsFoundByTag = listener.getSummary().getTestsFoundCount();
        long testsSucceededByTag = listener.getSummary().getTestsSucceededCount();

        System.out.println("Tests found with tag 'fast': " + testsFoundByTag);
        System.out.println("Tests succeeded with tag 'fast': " + testsSucceededByTag);

        // Reset listener for next run
        listener = new SummaryGeneratingListener();

        // Build request to select tests by class only (all tests in this class)
        LauncherDiscoveryRequest requestByClass = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectClass(TagAndClassSelectionTest.class))
                .build();

        launcher.registerTestExecutionListeners(listener);
        launcher.execute(requestByClass);

        long testsFoundByClass = listener.getSummary().getTestsFoundCount();
        long testsSucceededByClass = listener.getSummary().getTestsSucceededCount();

        System.out.println("Tests found by class: " + testsFoundByClass);
        System.out.println("Tests succeeded by class: " + testsSucceededByClass);

        // Assertions to verify behavior
        assertEquals(1, testsFoundByTag, "Should find exactly 1 test with tag 'fast'");
        assertEquals(1, testsSucceededByTag, "The tagged test should pass");
        assertEquals(3, testsFoundByClass, "Should find all 3 tests in the class");
        assertEquals(3, testsSucceededByClass, "All tests in the class should pass");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts - JUnit launcher is createdNo tests executed yet-PASS
2LauncherDiscoveryRequest built to select tests tagged with 'fast' in TagAndClassSelectionTest classRequest ready to find tests with tag 'fast'-PASS
3Launcher executes tests matching the 'fast' tagOnly the test method 'fastTest' is found and executedVerify test 'fastTest' passesPASS
4Summary shows 1 test found and passed for tag 'fast'Test summary reports 1 test found and succeededAssert testsFoundByTag == 1 and testsSucceededByTag == 1PASS
5LauncherDiscoveryRequest built to select all tests in TagAndClassSelectionTest classRequest ready to find all tests in the class-PASS
6Launcher executes all tests in the classAll three test methods (fastTest, slowTest, untaggedTest) are found and executedVerify all tests passPASS
7Summary shows 3 tests found and passed for class selectionTest summary reports 3 tests found and succeededAssert testsFoundByClass == 3 and testsSucceededByClass == 3PASS
8Test ends successfully with all assertions passingAll tests executed as expected-PASS
Failure Scenario
Failing Condition: No tests found with the specified tag due to typo or missing tag annotation
Execution Trace Quiz - 3 Questions
Test your understanding
Which test method is executed when selecting tests by tag 'fast'?
AfastTest
BslowTest
CuntaggedTest
DAll tests in the class
Key Result
Using tags and class selectors in JUnit allows you to run focused subsets of tests, which helps speed up testing and organize tests by categories.