Grouping Test cases

  • We can group multiple test methods in a class.
  • You can easily classify your test cases based on requirement.
  • You can perform a specific group of test methods related to a group or multiple groups.
  • You can create a group of test methods based on the testing type, depending on modules or based on testing types like functional testing, sanity testing etc…
  • Differentiate specific group test methods from all test methods.

Syntax:

@Test(groups={"groupname"})

Lets assume we are automating any application and there are two groups Smoke and Sanity test cases-

package testNGFrameWork;

import org.testng.annotations.Test;

public class testngGrouping {

	@Test(groups = { "Sanity", "Smoke" }, priority = 1)
	public void launchBrowser() {
		System.out.println("Launching the browser");
	}
	
	@Test(groups = { "Sanity","Smoke" }, priority = 5)
	public void closeBrowser() {
		System.out.println("Closing the browser");
	}
	
	@Test(groups = { "Sanity","Smoke" }, priority = 2)
	public void loginPage() {
		System.out.println("Navigate to application login page");
	}
	
	@Test(groups = { "Sanity"}, priority = 3)
	public void clicklogin() {
		System.out.println("Error message shown without entering un and pw");
	}
	
	@Test(groups = {"Smoke" }, priority = 4)
	public void loginToApplication() {
		System.out.println("Sucessfully login to application");
	}	
}

Execute single group using testng.xml file

If we want to execute only one group from class using testng.xml file then add group name into testng.xml file and run as shown as below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
          <groups>
            <run>
                <include name ="Smoke"/>
            </run>
        </groups>
    <classes>
      <class name="testNGFrameWork.testngGrouping"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Similarly we can add multiple groups in our testng.xml file and execute using it. Try with yourself and if found any difficulty then comment here we will figure it out.

Next
TestNG
Home Page

Leave a comment

Design a site like this with WordPress.com
Get started