Groups in TestNG

TestNG allows us to perform sophisticated groupings of test methods.

Using TestNG we can execute only set of groups while excluding another set. This gives us the maximum flexibility in divide tests and doesn't require us to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in testng.xml file and can be used either under the or tag. Groups specified in the tag apply to all the tags underneath.

package com.example.group;
import org.testng.annotations.Test;
public class groupExamples {
	@Test(groups="Regression")
	public void testCaseOne()
	{
	System.out.println("Im in testCaseOne - And in Regression Group");
	}
	@Test(groups="Regression")
	public void testCaseTwo(){
	System.out.println("Im in testCaseTwo - And in Regression Group");
	}
	@Test(groups="Smoke Test")
	public void testCaseThree(){
	System.out.println("Im in testCaseThree - And in Smoke Test Group");
	}
	@Test(groups="Regression")
	public void testCaseFour(){
	System.out.println("Im in testCaseFour - And in Regression Group");
	}
}

The below is the XML file to execute, the test methods with group.
We will execute the group “Regression” which will execute the test methods which are defined with group as “Regression”

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Suite">
  <test name="testing">
  	<groups>
      <run>
        <include name="Regression"/>
      </run>
    </groups>
    <classes>
       <class name="com.example.group.groupExamples" />
    </classes>
  </test>
</suite>
Test Frameworks: 

Comments

can we run two groups at single xml file?

Hey, how can I update the thread-count according to the number of tests I have in the specific group (and not manually)? In the above example you have three @Test(groups="Regression") and if I run a suite that runs only the (groups="Regression"), how can the thread-count attribute update dynamically according to the number of tests having the same group name?

Thanks

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.