TestNG Annotations

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:

@BeforeSuiteThis annotation will be run before all tests in the suite.
@AfterSuiteThis annotation will be run after all tests in the suite.
@BeforeTestThis annotation will be run before each section declared as <test> inside a TestNg suite
@AfterTestThis annotation will be run after each section declared as <test> inside a TestNg suite
@BeforeGroupsThis annotation will run before any tests specified on the group provided
@AfterGroupsThis annotation will run after any tests specified on the group provided
@BeforeClassThis annotation will run before any test method of the test class
@AfterClassThis annotation will run after any test method of the test class
@BeforeMethodThis annotation will run before each test method of the test class
@AfterMethodThis annotation will run after each test method of the test class
@DataProviderThis annotation marks a method as being a test data provider for another method
@FactoryThis annotation marks a method as a factory that returns an array of class objects (Object[ ]) which will be used as test classes by TestNG
@ListenersThis annotation helps in tracking the execution status and logging. It defines the listeners of a test class
@ParametersThis annotation is passing parameters to a test method from the TestNg.xml configuration file at runtime.
@TestThis 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!