Before and After annotations are mainly used to execute a certain set of code before and after the execution of test methods. These are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends.
TestNG provides five different kinds of Before and After annotation options, each of which can be used depending upon the test requirements. The following are the different before and after options provided by TestNG:
@BeforeSuite
/@AfterSuite
@BeforeTest
/@AfterTest
@BeforeGroups
/@AfterGroups
@BeforeClass
/@AfterClass
@BeforeMethod
/@AfterMethod
Let's try out an example containing all the preceding annotated methods and learn about how and when they are executed.