Showing posts with label TestNG Groups Tutorial. Show all posts
Showing posts with label TestNG Groups Tutorial. Show all posts

TestNG groups

  

What are Testng Groups?

In Testnggroups are used to categorize your test methods logically so you can run:

  • A specific set of tests (like "smoke", "regression", "sanity")

  • Multiple tests together, even if they are in different classes

Groups allow modular test execution, making large test suites more manageable and efficient.


Why Use Groups?

  • Run a subset of tests without running the entire suite

  • Reuse the same test in multiple groupings

  • Organize tests based on functionality, feature, or type (example: @smoke@regression@login)



Example: TestNG Groups using Java

Step 1: Create Java Test Class with Groups

import org.testng.annotations.Test;

public class GroupTestExample {

    @Test(groups = {"smoke", "login"})
    public void loginTest() {
        System.out.println("Smoke/Login Test: Login functionality");
    }

    @Test(groups = {"regression"})
    public void profileTest() {
        System.out.println("Regression Test: Profile page validation");
    }

    @Test(groups = {"sanity", "checkout"})
    public void paymentTest() {
        System.out.println("Sanity/Checkout Test: Payment flow");
    }

    @Test
    public void generalTest() {
        System.out.println("General Test: No group assigned");
    }
}



Step 2: Define Groups in testng.xml

It will run only the smoke group

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="GroupSuite">
  <test name="SmokeGroupOnly">
    <groups>
      <run>
        <include name="smoke"/>
      </run>
    </groups>
    <classes>
      <class name="GroupTestExample"/>
    </classes>
  </test>
</suite>



Output

Smoke/Login Test: Login functionality


We can also run multiple groups:

<include name="smoke"/>
<include name="sanity"/>



We can also exclude specific groups:

<groups>
  <run>
    <exclude name="regression"/>
  </run>
</groups>



If a test case execution depends upon successful execution of group of test cases then we can also create dependencies on test cases by using 'dependsOnGroups' attribute in @Test annotation, as shown below in code snippet.

@Test(groups = "db")
public void connectToDB() {
    System.out.println("Connected to DB");
}

@Test(groups = "sanity", dependsOnGroups = "db")
public void fetchUserData() {
    System.out.println("Fetched user data after DB connection");
}