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.
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.
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"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts - JUnit launcher is created | No tests executed yet | - | PASS |
| 2 | LauncherDiscoveryRequest built to select tests tagged with 'fast' in TagAndClassSelectionTest class | Request ready to find tests with tag 'fast' | - | PASS |
| 3 | Launcher executes tests matching the 'fast' tag | Only the test method 'fastTest' is found and executed | Verify test 'fastTest' passes | PASS |
| 4 | Summary shows 1 test found and passed for tag 'fast' | Test summary reports 1 test found and succeeded | Assert testsFoundByTag == 1 and testsSucceededByTag == 1 | PASS |
| 5 | LauncherDiscoveryRequest built to select all tests in TagAndClassSelectionTest class | Request ready to find all tests in the class | - | PASS |
| 6 | Launcher executes all tests in the class | All three test methods (fastTest, slowTest, untaggedTest) are found and executed | Verify all tests pass | PASS |
| 7 | Summary shows 3 tests found and passed for class selection | Test summary reports 3 tests found and succeeded | Assert testsFoundByClass == 3 and testsSucceededByClass == 3 | PASS |
| 8 | Test ends successfully with all assertions passing | All tests executed as expected | - | PASS |