Starting with Java5 the Annotation feature was introduced. Annotations works by adding metadata to the Java source code.
There are some predefined annotations in Java like @Override, @Deprecated, @SupressWarning, etc.
Java also lets you define your own annotations and this is what TestNg does. It makes use of this feature to build its own annotations.
The annotations provided by TestNg can be seen below:
| @BeforeSuite | This annotation will be run before all tests in the suite. |
| @AfterSuite | This annotation will be run after all tests in the suite. |
| @BeforeTest | This annotation will be run before each section declared as <test> inside a TestNg suite |
| @AfterTest | This annotation will be run after each section declared as <test> inside a TestNg suite |
| @BeforeGroups | This annotation will run before any tests specified on the group provided |
| @AfterGroups | This annotation will run after any tests specified on the group provided |
| @BeforeClass | This annotation will run before any test method of the test class |
| @AfterClass | This annotation will run after any test method of the test class |
| @BeforeMethod | This annotation will run before each test method of the test class |
| @AfterMethod | This annotation will run after each test method of the test class |
| @DataProvider | This annotation marks a method as being a test data provider for another method |
| @Factory | This annotation marks a method as a factory that returns an array of class objects (Object[ ]) which will be used as test classes by TestNG |
| @Listeners | This annotation helps in tracking the execution status and logging. It defines the listeners of a test class |
| @Parameters | This annotation is passing parameters to a test method from the TestNg.xml configuration file at runtime. |
| @Test | This annotation can be used on a class or a method to mark it as a test method. |
These are the annotations provided by TestNG. Many of them also have attributes that extend their functionality and provide a greater flexibility on our tests.
We will see each one in detail in the next tutorials!
