← Back to libraryQuestion 272 of 468
🔬TestNG & JUnitIntermediate

Grouping Tests — TestNG Groups vs JUnit Tags

📌 Definition:

Grouping lets you categorize tests (smoke, regression, slow) and run subsets. TestNG uses @Test(groups=...) with include/exclude in testng.xml; JUnit 5 uses @Tag and selects tags via the build or @Suite.

📖 Detailed Explanation:

TestNG: annotate @Test(groups = {"smoke", "login"}) and in testng.xml <groups><run><include name='smoke'/>. A method can belong to multiple groups, and group-level @BeforeGroups/@AfterGroups exist. JUnit 5: @Tag("smoke") on a test/class, then filter with Maven Surefire (groups/excludedGroups) or JUnit Platform tag expressions. Both let CI run 'smoke on PR, full nightly'. Groups/tags are the standard way to slice a large suite by suite type, speed, or feature. A consistent tagging taxonomy is important; unregistered/typo'd group names silently run nothing.

🔑 Key Points:
  • TestNG @Test(groups=...) + include/exclude in testng.xml
  • JUnit5 @Tag(...) + Surefire groups/excludedGroups or tag expressions
  • A test can belong to multiple groups/tags
  • Slice suites by type/speed/feature for pipeline stages
🌍 Real-World Example:

Both worlds tag critical paths (@Test(groups='smoke') / @Tag('smoke')) so the PR pipeline runs only smoke and the nightly runs everything — the same strategy, different syntax.

🎯 Scenario-Based Interview Question:

You want the same 'smoke subset on PR' capability in TestNG and in JUnit 5. What's the mechanism in each?