TestNG Before and After Annotations
TestNG Before and After Annotations
There are many Before and After annotations in TestNG Framework. They are used to execute code before and after the execution of @Test annotated methods, Group of test methods, Test classes, etc.
Before and After Annotations
The following are the different Before and After annotations:
- @BeforeSuite/@AfterSuite
- @BeforeTest/@AfterTest
- @BeforeGroups/@AfterGroups
- @BeforeClass/@AfterClass
- @BeforeMethod/@AfterMethod
@BeforeSuite/@AfterSuite
This method will be executed before and after any tests declared in the TestNG <suite> tag in the testng.xml file.
@BeforeTest/@AfterTest
This method will be executed before and after the tests in the <test> tag in the TestNG testng.xml file.
@BeforeGroups /@AfterGroups
@BeforeGroups annotated method will run before any of the test methods specified with the groups attribute. @AfterGroups annotated method will run after all the test methods specified with the groups attribute have run.
@BeforeClass/@AfterClass
The @BeforeClass annotated test method is executed before any of the test methods in the test class. @AfterClass annotated method is executed after the execution of all the test methods in a test class.
@BeforeMethod/@AfterMethod
These annotated methods are executed before and after the execution of each @Test annotated method.
To know the Annotations Execution Sequence Order,detailed example can be found here:
https://www.testingdocs.com/testng-annotations-order-example/