Test groups help organize tests into categories. This makes running related tests easier and faster.
When you want to run only smoke tests before a release.
When you want to separate slow tests from fast tests.
When you want to run tests for a specific feature only.
When you want to skip tests that are not ready or broken.
When you want to run tests in parallel by group.
Syntax
Selenium Java
@Test(groups = {"groupName1", "groupName2"})
public void testMethod() {
// test code
}
Use @Test annotation with groups attribute to assign test methods to groups.
You can assign one or more groups by listing them in curly braces.
Examples
This test belongs to the 'smoke' group.
Selenium Java
@Test(groups = {"smoke"})
public void testLogin() {
// test login functionality
}
This test belongs to both 'regression' and 'slow' groups.
Selenium Java
@Test(groups = {"regression", "slow"})
public void testReportGeneration() {
// test report generation
}
Sample Program
This class has three tests assigned to different groups. You can run tests by group using TestNG configuration.
Selenium Java
import org.testng.annotations.Test;
public class SampleTestGroups {
@Test(groups = {"smoke"})
public void testHomePage() {
System.out.println("Home page test running");
assert true;
}
@Test(groups = {"regression"})
public void testLogin() {
System.out.println("Login test running");
assert true;
}
@Test(groups = {"smoke", "regression"})
public void testSearch() {
System.out.println("Search test running");
assert true;
}
}
OutputSuccess
Important Notes
You run test groups using TestNG XML or command line options.
Grouping helps run only needed tests, saving time.
Tests can belong to multiple groups for flexibility.
Summary
Test groups organize tests into categories.
Use @Test(groups = {...}) to assign groups.
Run tests by group to save time and focus testing.
Practice
(1/5)
1. What is the main purpose of using groups in Selenium Java tests?
easy
A. To change the browser used in tests
B. To organize tests into categories for selective execution
C. To automatically generate test reports
D. To increase the speed of the WebDriver
Solution
Step 1: Understand the role of test groups
Test groups help organize tests into categories, making it easier to run specific sets of tests.
Step 2: Identify the correct purpose
Groups do not affect WebDriver speed, report generation, or browser choice directly.
Final Answer:
To organize tests into categories for selective execution -> Option B
Quick Check:
Test groups = Organize tests [OK]
Hint: Groups categorize tests to run only what you need [OK]
Common Mistakes:
Thinking groups speed up WebDriver
Confusing groups with report generation
Assuming groups change browser settings
2. Which is the correct syntax to assign a test method to groups "smoke" and "regression" in TestNG?
easy
A. @Test(groups = {"smoke", "regression"})
B. @Test(group = "smoke, regression")
C. @Test(groups = "smoke, regression")
D. @Test(groups = ["smoke", "regression"])
Solution
Step 1: Recall TestNG groups syntax
The correct syntax uses groups = {"group1", "group2"} with curly braces and double quotes for each group.
Step 2: Compare options
@Test(groups = {"smoke", "regression"}) matches the correct syntax. Options A, B, and D misuse quotes or attribute name. @Test(groups = ["smoke", "regression"]) uses square brackets which is invalid in Java annotations.
Final Answer:
@Test(groups = {"smoke", "regression"}) -> Option A
Quick Check:
Groups use curly braces and quotes [OK]
Hint: Use curly braces and quotes for multiple groups [OK]
Common Mistakes:
Using 'group' instead of 'groups'
Passing groups as a single string
Using square brackets instead of curly braces
3. Given the following TestNG test methods, which tests will run if you execute only the "fast" group?
@Test(groups = {"fast"})
public void testA() {}
@Test(groups = {"slow"})
public void testB() {}
@Test(groups = {"fast", "regression"})
public void testC() {}
medium
A. testA only
B. testB only
C. testA and testC
D. testB and testC
Solution
Step 1: Identify tests with group "fast"
testA has group "fast"; testC has groups "fast" and "regression"; testB has "slow" only.
Step 2: Determine which tests run when "fast" group is executed
Only tests assigned to "fast" group run, so testA and testC run; testB does not.
Final Answer:
testA and testC -> Option C
Quick Check:
Run group "fast" = testA, testC [OK]
Hint: Run group runs all tests tagged with it [OK]
Common Mistakes:
Running tests from other groups
Ignoring tests with multiple groups
Assuming only single-group tests run
4. What is wrong with this TestNG test method if the intention is to assign it to groups "ui" and "integration"?
@Test(groups = "ui, integration")
public void testUIIntegration() {}
medium
A. Groups should be passed as an array, not a single string
B. The method name is invalid
C. The annotation @Test is missing parentheses
D. Groups attribute is not supported in @Test
Solution
Step 1: Check groups attribute format
Groups must be passed as an array of strings using curly braces, not a single comma-separated string.
Step 2: Identify the error in the code
The code uses a single string "ui, integration" instead of {"ui", "integration"}.
Final Answer:
Groups should be passed as an array, not a single string -> Option A
Quick Check:
Groups need array syntax [OK]
Hint: Use curly braces for multiple groups [OK]
Common Mistakes:
Passing groups as one string with commas
Forgetting curly braces for arrays
Misunderstanding annotation syntax
5. You have three test methods:
@Test(groups = {"sanity"})
public void testLogin() {}
@Test(groups = {"regression", "sanity"})
public void testCheckout() {}
@Test(groups = {"regression"})
public void testSearch() {}
How would you run only the tests that belong exclusively to the "sanity" group, excluding those that also belong to "regression"?
hard
A. Run tests with groups "sanity" and "regression" together
B. Run all tests with group "sanity" only
C. Run tests with group "regression" only
D. Run tests with group "sanity" and exclude group "regression" in the TestNG XML
Solution
Step 1: Identify tests with only "sanity" group
testLogin belongs only to "sanity"; testCheckout belongs to both "regression" and "sanity"; testSearch belongs only to "regression".
Step 2: Determine how to run only exclusive "sanity" tests
Use TestNG XML to include "sanity" group and exclude "regression" group to run only testLogin.
Final Answer:
Run tests with group "sanity" and exclude group "regression" in the TestNG XML -> Option D
Quick Check:
Include "sanity" exclude "regression" = exclusive tests [OK]
Hint: Include one group, exclude others in XML to filter tests [OK]
Common Mistakes:
Running all tests with "sanity" ignoring exclusions
Running "regression" group instead
Running both groups together without filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.