Usage of annotations in TestNG

Usage of annotations in TestNG

TestNG annotations are defined before methods in the Test class to control the next batch of code/method to be executed.

If in case any of the annotations are NOT defined in front of a test method, the compiler just ignores and executes it as a part of the Test class.

We've them defined in three ways :

Let’s check on the usage :

AnnotationExplanation
@BeforeSuiteExecutes once Before the execution the tests in the suite are executed.
@AfterSuiteExecutes once After the execution of all the tests in a particular suite is completed.
@BeforeTestExecutes Before the first @Test annotated method. This can be executed multiple times before the test case.
@AfterTestExecutes After the first @Test annotated method. This can be executed multiple times before the test case.
@BeforeClassExecutes Before first @Test method execution. This is done only once throughout the test case.
@AfterClassExecutes After first @Test method execution. This is done only once throughout the test case.
@BeforeMethodExecutes Before every @Test method execution.
@AfterMethodExecutes After every @Test method execution.
@BeforeGroupsExecutes Before the first test run of a specific particular group.
@AfterGroupsExecutes After all the test methods of a particular group.
@TestIt is a method that is part of the Test.
@ParametersUsed to pass parameters to the Test method.
@FactoryUsed to represent a method as a Factory that returns Object[] for further usage in test classes.
@DataProviderActs as a data supplier for the @Test method, the respective annotated method must always return Object[][] and the name of the dataProvider during the definition must match with the one annotated in the @Test method.
@ListenersUsed for logging and reporting.

Code Example :

import org.testng.annotations.*;
public class TestNGAnnotations {
    @BeforeTest
    public void beforeTest() {
        System.out.println("-- Before Test -- ");
    }
    @AfterTest
    public void afterTest() {
        System.out.println("-- After Test --");
    }
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("-- Before Method --");
    }
    @AfterMethod
    public void afterMethod() {
        System.out.println("-- After Method --");
    }
    @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 test() {
        System.out.println("Test method");
    }
}

Output :

Sequence of Execution

@BeforeSuite

@BeforeClass

@BeforeMethod

@BeforeTest

@Test

@AfterTest

@AfterMethod

I hope this TestNG tutorial, helped you to learn in detail about the TestNG annotations.

If you have any questions related to the article, let me know.

I look forward to your replies. Happy testing! :)