Annotations In TestNG

Basically we need TestNG because WebDriver can not generate reports but TestNG generates reports based on the test results of Selenium. Not only this but there are many advantages of TestNG like :

– It makes parallel testing possible.

– Grouping of test cases are really. You can customize and run your test cases the way you want.

– TestNG are very easy to understand.

In short, you can say that TestNG guides you what to do next i.e; which method you should execute next. Here we have TestNG annotation list :

1) @Test 

It describes a class or method as part of the test.

2) @BeforeMethod

The methods which are marked with @BeforeMethod annotation will come in execution before each and every @Test annotated method.

3) @AfterMethod

The methods which are marked with @AfterMethod annotation will come in execution after each and every @Test annotated method.

4) @BeforeClass

The methods marked under @BeforeClass annotation will be executed only once before first @Test annotation of the current class.

5) @AfterClass

The methods in the @AfterClass annotations will run only once when the execution of all the test methods of the current class is done.

6) @BeforeTest

The @BeforeTest annotated methods are executed before any @Test annotated methods are executed of all those classes which comes inside <test> tag in testng.xml file.

7) @AfterTest

The @AfterTest annotated methods are executed after all @Test annotated methods are executed of all those classes which comes inside <test> tag in testng.xml file.

8) @BeforeSuite

All the methods marked under @BeforeSuite annotation will be executed before all suites are run from the test.

9) @AfterSuite

The methods of @AfterSuite annotation will start running once the execution of all the tests are done from the current test Suite.

10) @DataProvider

When we want to use a method as data supplier, we write it under @DataProvider annotation.

11) @BeforeGroups

The methods of @BeforeGroups annotation will run before the first test run of that particular test group.

12)  @AfterGroups

The methods of @AfterGroups annotation will run after all the test run of that particular test group.

13) @Parameters

For passing parameters in your test method, you need to define @Parameters Annotation.

14) @Factory

When there is a need to execute specific group of test cases by passing different values, then we use @Factory annotation. The method under it must return Object[].

15) @Listeners

This annotation defines listeners on a test class. It is very helpful for logging purpose.

Add Comment