@Test:Â
Usage: Marks a method as a test method.Â
Execution: The methods annotated with @Test are executed when TestNG runs the test suite.Â
Java CodeÂ
import org.testng.annotations.Test;
public class MyTestClass {
   @Test
   public void myTestMethod() {
       // Test logic goes here
   }
}
Â
@BeforeSuite:Â
Usage: Executed before the TestNG suite starts, runs only once.Â
Execution: This annotation is used to set up global preconditions for the entire test suite.Â
Java CodeÂ
import org.testng.annotations.BeforeSuite;
public class MyTestClass {
   @BeforeSuite
   public void setUpBeforeSuite() {
       // Global setup logic goes here
   }
}
Â
@AfterSuite:Â
Usage: Executed after the TestNG suite ends, runs only once.Â
Execution: This annotation is used for cleanup or teardown tasks after the entire test suite has finished executing.Â
Java CodeÂ
import org.testng.annotations.AfterSuite;
public class MyTestClass {
   @AfterSuite
   public void tearDownAfterSuite() {
       // Global teardown logic goes here
   }
}

@BeforeTest:Â
Usage: Executed before each <test> tag in the testng.xml file.Â
Execution: Methods annotated with @BeforeTest are executed before each <test> tag defined in the testng.xml file.Â
Java CodeÂ
import org.testng.annotations.BeforeTest;
public class MyTestClass {
   @BeforeTest
   public void setUpBeforeTest() {
       // Test-specific setup logic goes here
   }
}
Â
@AfterTest:Â
Usage: Executed after each <test> tag in the testng.xml file.Â
Execution: Methods annotated with @AfterTest are executed after each <test> tag defined in the testng.xml file.Â
Java CodeÂ
import org.testng.annotations.AfterTest;
public class MyTestClass {
   @AfterTest
   public void tearDownAfterTest() {
       // Test-specific teardown logic goes here
   }
}
Â
@BeforeClass:Â
Usage: Executed before the first test method in the current class.Â
Execution: Methods annotated with @BeforeClass are executed once before any test methods in the current class are executed.Â
Java CodeÂ
import org.testng.annotations.BeforeClass;
public class MyTestClass {
   @BeforeClass
   public void setUpBeforeClass() {
       // Class-specific setup logic goes here
   }
}
Â
@AfterClass:Â
Usage: Executed after all the test methods in the current class have been run.Â
Execution: Methods annotated with @AfterClass are executed once after all test methods in the current class have been executed.Â
Java CodeÂ
import org.testng.annotations.AfterClass;
public class MyTestClass {
   @AfterClass
   public void tearDownAfterClass() {
       // Class-specific teardown logic goes here
   }
}
Â
@BeforeMethod:Â
Usage: Executed before each test method.Â
Execution: Methods annotated with @BeforeMethod are executed before each test method in the class.Â
Java CodeÂ
import org.testng.annotations.BeforeMethod;
public class MyTestClass {
   @BeforeMethod
   public void setUpBeforeMethod() {
       // Method-specific setup logic goes here
   }
}
Â
@AfterMethod:Â
Usage: Executed after each test method.Â
Execution: Methods annotated with @AfterMethod are executed after each test method in the class.Â
Java CodeÂ
import org.testng.annotations.AfterMethod;
public class MyTestClass {
   @AfterMethod
   public void tearDownAfterMethod() {
       // Method-specific teardown logic goes here
   }
}

@DataProvider:Â
Usage: Supplies data to test methods, allowing them to be executed multiple times with different data sets.Â
Execution: Methods annotated with @DataProvider provide the data required for test methods.Â
Java CodeÂ
import org.testng.annotations.DataProvider;
public class MyDataProvider {
   @DataProvider(name = “myData”)
   public Object[][] provideData() {
       // Data generation logic goes here
   }
}Â
Â
@Parameters:Â
Usage: Provides values to test methods from the testng.xml file.Â
Execution: TestNG injects parameter values from the testng.xml file into test methods annotated with @Parameters.Â
Java CodeÂ
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MyTestClass {
   @Test
   @Parameters(“myParameter”)
   public void myTestMethod(String parameterValue) {
       // Test logic using parameterValue goes here
   }
}
Â
@Listeners:Â
Usage: Defines custom listeners to perform additional actions during test execution, such as logging or reporting.Â
Execution: Methods annotated with @Listeners are invoked during various stages of test execution to perform custom actions.Â
Java CodeÂ
import org.testng.annotations.Listeners;
@Listeners(MyCustomListener.class)
public class MyTestClass {
   // Test methods and other annotations go here
}
Â
@Factory:
Usage: Allows the creation of test instances dynamically, enabling data-driven testing and parallel execution.Â
Execution: The @Factory annotation is used to create multiple instances of a test class, each with different data sets or configurations.Â
Java CodeÂ
import org.testng.annotations.Factory;
public class MyFactory {
   @Factory
   public Object[] createInstances() {
       // Test instances creation logic goes here
   }
}
Â
@BeforeGroups:Â
Usage: Executed before the first test method that belongs to any of the specified groups.Â
Execution: Methods annotated with @BeforeGroups are executed before any test methods belonging to the specified groups are executed.Â
Java CodeÂ
import org.testng.annotations.BeforeGroups;
public class MyTestClass {
   @BeforeGroups(“group1”)
   public void setUpBeforeGroup() {
       // Group-specific setup logic goes here
   }
}

@AfterGroups:Â
Usage: Executed after all the test methods that belong to any of the specified groups have been run.Â
Execution: Methods annotated with @AfterGroups are executed after all test methods belonging to the specified groups have been executed.Â
Java CodeÂ
import org.testng.annotations.AfterGroups;
public class MyTestClass {
   @AfterGroups(“group1”)
   public void tearDownAfterGroup() {
       // Group-specific teardown logic goes here
   }
}
 These examples illustrate how each annotation is used and executed in TestNG-based Selenium tests.Â
Author’s Bio:

As CEO of TestLeaf, I’m dedicated to transforming software testing by empowering individuals with real-world skills and advanced technology. With 24+ years in software engineering, I lead our mission to shape local talent into global software professionals. Join us in redefining the future of test engineering and making a lasting impact in the tech world.
Babu Manickam
CEO – Testleaf






