Groups


TestNGroup

TestNG allows you to perform groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This will gives you maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath. Note that groups are accumulative in these tags: if you specify group "group1" in <suite> and "group2" in <test>, then both "group1" and "group2" will be included.

Lets Create java class as example groupexample.java

import org.testng.annotations.Test;
publicclass groupexample {
@Test(groups = { "functest", "vssintest" })
publicvoid testMethod1() {
System.out.println("This is testMethod1");
}
@Test(groups = {"vssintest "} )
publicvoid testMethod2() {
System.out.println("This is testMethod2");
}
@Test(groups = { "functest" })
publicvoid testMethod3() {
System.out.println("This is testMethod3");
}
}

TestNG allows you to specify this in a very intuitive way with test groups.

For example, you could structure your test by saying that your entire test class belongs to the "functest" group, and additionally that a couple of methods belong to the group "vssintest"

TestNG.xml file

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<groups>
<run>
<includename="functest"/>
</run>
</groups>
<classes>
<classname="groupexample"/>
</classes>
</test><!-- Test -->
</suite><!—Suite -->
Results
image
will run only test methods (testMethod1and testMethod3) belongs to functest group , and it will not run testmethod2 because it belongs to vssintest group.
Here is another example, using regular expressions.
import org.testng.annotations.Test;
publicclass groupwithregexpression {
@Test(groups = { "windows.vssintest" })
publicvoid testWindowsOnly() {
System.out.println("This is testWindowsOnly");
}
@Test(groups = {"linux.checkintest"} )
publicvoid testLinuxOnly() {
System.out.println("This is testLinuxOnly");
}
@Test(groups = { "windows.functest"})
publicvoid testWindowsToo() {
System.out.println("This is testWindowsToo");
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<groups>
<run>
<includename="windows.*"/>
</run>
</groups>
<classes>
<classname="groupwithregexpression"/>
</classes>
</test><!-- Test -->
</suite><!—Suite –>
Results
image
will run only test methods (testWindowsOnly and testWindowsToo) belongs to windows group

Comments