TestNG Annotations
Annotation is a feature introduced in Java 5 and is used to add metadata to Java source code. This will allow you to add information to an existing data object in your source code. It can be applied for Classes, Methods, Variables and Parameters. Java allows users to define their own annotations too. TestNG made use of the same feature to define its own annotations. TestNg Annotations will direct the execution flow of the program.
The following is a table containing information about all the annotations in the TestNG and a brief description of them:
Annotation | Description |
---|---|
@BeforeSuite | The annotated method will be executed Before any tests declared inside the TestNG suite |
@AfterSuite | The annotated method will be executed After any tests declared inside the TestNG suite |
@BeforeTest | The annotated method will be executed Before test section declared inside the TestNG suite |
@AfterTest | The annotated method will be executed After test section declared inside the TestNG suite |
@BeforeGroups | The annotated method will be executed Before any of the test method belongs to specified group |
@AfterGroups | The annotated method will be executed After any of the test method belongs to specified group |
@BeforeClass | The annotated method will be executed Before any of the test method of a test class. |
@afterClass | The annotated method will be executed After any of the test method of a test class. |
@BeforeMethod | The annotated method will be executed Before each test method |
@AfterMethod | The annotated method will be executed After each test method |
@DataProvider | Marks a method as a data providing method for a test method. The said method has to return an Object double array (Object[ ][ ]) as data |
@Factory | Marks a annotated method as a factory that returns an array of class objects (Object[ ]). These class objects will then be used as test classes by TestNG. This is used to run a set of test cases with different values |
@Listeners | Applied on a test class. Defines an array of test listeners classes extending org.testng.ITestNGListener. Helps in tracking the execution status and logging purpose. |
@Parameters | This annotation is used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at run time |
@Test | Marks a class or a method as a test method. If used at class level, all the public methods of a class will be considered as a test method |