Grouping Test Cases in TestNG

Grouping Test Cases in TestNG

In TestNG, grouping test cases allows you to organize and execute a specific subset of tests together. This is especially useful in large test suites where you may want to run only a particular category of tests (e.g., smoke tests, regression tests, or sanity tests).


How to Group Test Cases in TestNG

You can group test cases using the groups attribute of the @Test annotation.


Example: Grouping Test Cases

java

Copy

Edit

import org.testng.annotations.Test;


public class GroupTestExample {


    @Test(groups = {"smoke"})

    public void testLogin() {

        System.out.println("Smoke Test - Login Test");

    }


    @Test(groups = {"regression"})

    public void testAddToCart() {

        System.out.println("Regression Test - Add To Cart");

    }


    @Test(groups = {"smoke", "regression"})

    public void testSearchProduct() {

        System.out.println("Smoke & Regression Test - Search Product");

    }

}

Running Specific Groups in testng.xml

You can define which groups to include or exclude in your testng.xml file:


xml

Copy

Edit

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="MySuite">

  <test name="SmokeTests">

    <groups>

      <run>

        <include name="smoke"/>

      </run>

    </groups>

    <classes>

      <class name="GroupTestExample"/>

    </classes>

  </test>

</suite>

Using Groups with Annotations

You can also include/exclude groups programmatically or in Maven with the Surefire plugin, like:


xml

Copy

Edit

<groups>smoke</groups>

Or:


xml

Copy

Edit

<excludedGroups>regression</excludedGroups>

Benefits of Grouping Tests

Easier test management


Better control over test execution


Supports different testing needs (smoke, sanity, regression)


Saves time by avoiding unnecessary test runs

Learn Selenium JAVA Course in Hyderabad

Read More

Introduction to TestNG Annotations

How to Use Assertions in Selenium Tests

Visit Our IHUB Talent Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial