Introduction
The @Tag annotation helps group tests by categories. It makes running specific groups of tests easier.
The @Tag annotation helps group tests by categories. It makes running specific groups of tests easier.
@Tag("tagName")
@Test
void testMethod() {
// test code
}Place @Tag above test methods or test classes.
You can use multiple tags by repeating @Tag annotations.
@Tag("fast")
@Test
void quickTest() {
// test code
}@Tag("integration")
@Test
void databaseTest() {
// test code
}@Tag("slow") @Tag("ui") @Test void uiTest() { // test code }
This class has two tests, each tagged differently. You can run only "fast" tests or only "slow" tests using these tags.
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class SampleTagTest { @Tag("fast") @Test void fastTest() { assertTrue(1 + 1 == 2); } @Tag("slow") @Test void slowTest() { assertTrue(2 * 2 == 4); } }
You can filter tests by tags in your build tool or IDE.
Tags help keep your test suite organized and efficient.
@Tag groups tests for easier selection.
Use tags to run or skip specific test categories.
Tags can be added to methods or whole classes.
@Tag annotation in JUnit?@Tag@Tag is used to label tests with categories or groups.@Tag groups tests = A [OK]@Tag("integration")
class DatabaseTests {
@Test void testConnection() {}
@Test @Tag("slow") void testQueryPerformance() {}
}
@Tag("unit")
class ServiceTests {
@Test void testServiceLogic() {}
}@Test
@Tag(fast)
void quickTest() {}Pass the test and this pattern moves unknown