@Before group and @After group


@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

Lets create java class

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
publicclass beforeaftergroup {
@BeforeGroups("group1")
publicvoid TestMethod1() {
System.out.println("before group TestMethod ()");
}
@AfterGroups("group1")
publicvoid TestMethod2() {
System.out.println("after group TestMethod()");
}
@Test(groups= "group1")
publicvoid TestMethod3() {
System.out.println("TestMethod1-group1 ()");
}
@Test(groups= " group1")
publicvoid TestMethod4() {
System.out.println("TestMethod2-group1 ()");
}
@Test(groups= "group2")
publicvoid TestMethod5() {
System.out.println("TestMethod3-group2 ()");
}
}
Here, TestMethod3 and TestMethod4 belongs to group1
TestMethod5 belongs to group2


TestNG.xml file

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test1">
<classes>
<classname="beforeaftergroup"/>
</classes>
</test><!-- Test -->
</suite><!—Suite -->

Results

image

Comments