0
0
Selenium Javatesting~5 mins

Test groups in Selenium Java

Choose your learning style9 modes available
Introduction

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.