Annotations in testng

Here is the list of all TestNG annotations.
  • @Test – used to mark a test method.
  • @BeforeTest – method to be executed before executing set of tests (test tag in XML suite)
  • @AfterTest – method to be executed after executing set of tests (test tag in XML suite)
  • @BeforeMethod – method to be executed before every test method
  • @AfterMethod – method to be executed after every test method
  • @BeforeClass – method to be executed before executing all test methods in a test class
  • @AfterClass – method to be executed after executing all test methods in a test class
  • @BeforeSuite – method to be executed before all tests in the suite
  • @AfterSuite – method to be executed after all tests in the suite
  • @BeforeGroups – method to be executed before a test belonging to specific group
  • @AfterGroups – method to be executed after a test belonging to specific group
  • @DataProvider – data generation for tests
  • @Parameters – pass parameters to tests
  • @Factory –
  • @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:UsersSagar.IdeaIC15system	emp-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
===============================================      

Web development and Automation testing

solutions delivered!!