@BeforeClass and @AfterClass


@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
publicclass TestNGExample {
@Test
publicvoid TestSample(){
System.out.println("This Method will Whenever TestMethod Calls");
}
@BeforeSuite
publicvoid BeforeSuiteSample(){
System.out.println("This Method will Execute before suite");
}
@AfterSuite
publicvoid AfterSuiteSample(){
System.out.println("This Method will Execute After Suite");
}
@BeforeClass
publicvoid BeforeclassSample(){
System.out.println("This Method will Execute Before Class");
}
@AfterClass
publicvoid AfterClassSample(){
System.out.println("This Method will Execute After Class");
}
@BeforeTest
publicvoid BeforeTestSample(){
System.out.println("This Method will Execute Before Test");
}
@AfterTest
publicvoid AfterTestSample(){
System.out.println("This Method will Execute After Test");
}
}

TestNG.xml file

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

Results

image

Note:

1) @before Class Annotation will execute just before the @Test Annotation executes
2) @ After Class Annotation will execute after run all @Test annotations.

Comments