About TestNG Before And After Annotations And its Execution.
Posted By : Kuldeep Sidana | 23-Dec-2018
TestNG: TestNG is the unit testing automation framework which is widely used in selenium. This is very essential for all the automation engineers to understand the TestNG annotations before start to work on automation TestNG framework.
TestNG Before and After Annotations:
Different kind of before and after annotations are provided by the TestNG, which are used upon the base of the Test requirement. Following are the different annotations of the TestNG.
@BeforeMethod- Much before of the all the test methods execution the BeforeMethod annotated method will be executed.
@AfterMethod- After each the test method the AfterMethod annotation will be executed.
@BeforeClass- BeforeClass will execute prior to the before method and the Test method.
@AfterClass- AfterClass annotated method will execute later on the AFterMethod and Test Method.
@BeforeTest- BeforeTest will execute prior to BeforeClass, BeforeMethod and Test Method.
@AfterTest- AfterTest will execute prior to AfterClass and AfterMethod.
@BeforeSuite- Before all the annotations and Test the BeforeSuit will be run.
@AfterSuite- After all the annotations and the Test, the AfterSuit will be executed.
Prerequisites:
- TestNG should be installed in the Eclipse.
- JDK – Java Development Kit should also installed.
- TestNG Annotations support only with Java 1.5 version or higher
Understand the execution by code:
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class MyFirstTest
{
@Test
public void testCase() {
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite method");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite method");
}
@BeforeTest
public void beforeTest() {
System.out.println("Before Test method");
}
@AfterTest
public void afterTest() {
System.out.println("After Test method");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class method");
}
@AfterClass
public void afterClass() {
System.out.println("After Class method");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
}
Output:
Before Suite method
Before Test method
Before Class method
Before Method
After Method
After Class method
After Test method
PASSED: testCase
After Suite method
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kuldeep Sidana
Kuldeep Sidana is an orient QA Engineer having dexterity in Core Java and vouched with Automation Testing tool (Selenium Web Driver) and Manual Testing.