TestNG Annotations

Here is the list of all TestNG annotations.

  1. @Test – used to mark a test method.
  2. @BeforeTest – method to be executed before executing set of tests (test tag in XML suite)
  3. @AfterTest – method to be executed after executing set of tests (test tag in XML suite)
  4. @BeforeMethod – method to be executed before every test method
  5. @AfterMethod – method to be executed after every test method
  6. @BeforeClass – method to be executed before executing all test methods in a test class
  7. @AfterClass – method to be executed after executing all test methods in a test class
  8. @BeforeSuite – method to be executed before all tests in the suite
  9. @AfterSuite – method to be executed after all tests in the suite
  10. @BeforeGroups – method to be executed before a test belonging to specific group
  11. @AfterGroups – method to be executed after a test belonging to specific group
  12. @DataProvider – data generation for tests
  13. @Parameters – pass parameters to tests
  14. @Factory –
  15. @Listeners –

Below example will be useful in understanding how to use above annotations.

package org.softpost;

import org.testng.annotations.*;

public class Annotations {

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("This method is invoked before suite");
    }

    @AfterSuite
    public void afterSuite(){
        System.out.println("This method is invoked after suite");
    }

    @BeforeTest
    public void beforeTest(){
        System.out.println("This method is invoked before test tag");
    }

    @AfterTest
    public void afterTest(){
        System.out.println("This method is invoked after test tag");
    }

    @BeforeMethod
    public void beforeTestMethod(){
        System.out.println("This method is invoked before test method");
    }

    @AfterMethod
    public void afterTestMethod(){
        System.out.println("This method is invoked after test method");
    }


    @BeforeClass
    public void beforeClassMethod(){
        System.out.println("This method is invoked before class");
    }

    @AfterClass
    public void afterClassMethod(){
        System.out.println("This method is invoked after class");
    }

    @BeforeGroups(groups = {"sanity"})
    public void beforeGroupsMethod(){
        System.out.println("This method is invoked before sanity group");
    }

    @AfterGroups(groups = {"sanity"})
    public void afterGroupsMethod(){
        System.out.println("This method is invoked after sanity group");
    }

    @Test(groups = "sanity")
    public void test1(){
        System.out.println("This is a test1");
    }

}

Here is the output of above code.

[TestNG] Running:
C:\Users\Sagar\.IdeaIC15\system\temp-testng-customsuite.xml
This method is invoked before suite
This method is invoked before test tag
This method is invoked before class
This method is invoked before sanity group
This method is invoked before test method
This is a test1
This method is invoked after test method
This method is invoked after sanity group
This method is invoked after class
This method is invoked after test tag
This method is invoked after suite

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

You may also like...