TestNG Annotations

 HOME

In the previous tutorial, we have discussed How to Download and Install TestNG in Eclipse. In the this tutorial, we are going to discuss about the various annotations available in TestNG. Below is the list of annotations:-

  • @Test – This marks a class or a method as a part of the test.
  • @BeforeSuite – The @BeforeSuite method in TestNG runs before the execution of all other test methods. It will run only once before all tests in the suite have to run
  • @AfterSuite – The @AfterSuite method in TestNG runs after the execution of all other test methods. It will run only once after all tests in the suite have run
  • @BeforeTest – The @BeforeTest method in TestNG runs before the execution of first @Test method. It runs only once per class.
  • @AfterTest – The @AfterTest method in TestNG executes after the execution of all the test methods that are inside that folder.
  • @BeforeClass – The @BeforeClass method in TestNG will run before the first method invokes of the current class.
  • @AfterClass – The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
  • @BeforeMethod – The @BeforeMethod method in TestNG will execute before each test method. 
  • @AfterMethod – The @AfterMethod method in TestNG will run after each test method is executed.
  • @BeforeGroups – The @BeforeGroups method in TestNG run before the test cases of that group execute. It executes just once.
  • @AfterGroups – The @AfterGroups method in TestNG run after the test cases of that group execute. It executes only once. 
  • @Parameters –This annotation is used to pass parameters to test methods.
  • @DataProvider – If we use @DataProvider annotation for any method that means you are using that method as a data supplier. The configuration of @DataProvider annotated method must be like it always return Object[][] which we can use in @Test annotated method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
  • @Factory – Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
  • @Listeners – This annotation is used with test class. It helps in writing logs and results.

Lets see a program to understand the sequence in which these annotations run in the program.

import org.testng.annotations.*;

public class AnnotationsExample {

    @BeforeTest
    public void init(){
        System.out.println("---------------- Before Test ------------- ");
        //open the browser
    }

    @AfterTest
    public void quit(){
        System.out.println("---------------- After Test ------------- ");
        //close the browser
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("############### Before Method ############### ");
        //Initialize the Report
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("############## After Method ################");
        //Finalize the Report
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("*************** Before Class ***************");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("*************** After Class *****************");
    }

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("This will execute Before Suite");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("This will execute After Suite");
    }

    @Test
    public void loginTest(){
        System.out.println("Login the application ");

    }

    @Test
    public void logout(){
        System.out.println("Logout the application ");

    }

}

Execution process is as follows:

  1. First of all, beforeSuite() method is executed only once.

2. Lastly, the afterSuite() method executes only once.

3. @BeforeMethod and @AfterMethod are executed twice, one for Test Case 1 and again for Test Case 2.

4. Methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.

5. To execute this program, we need to Right click and select Run as – TestNG or Run testng.xml

 The test execution result will look like something shown below

TestNG generates various type of reports under test-output folder.

Open ‘emailable-report.html‘, as this is a html report open it with browser. It will look like something below.

TestNG also produce “index.html” report and it resides under test-output folder.

Congratulations. We are done. Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s