{"id":2129,"date":"2024-04-17T11:16:14","date_gmt":"2024-04-17T05:46:14","guid":{"rendered":"https:\/\/www.testleaf.com\/blog\/?p=2129"},"modified":"2025-08-28T16:28:43","modified_gmt":"2025-08-28T10:58:43","slug":"learn-15-testng-annotations-for-enhanced-selenium-testing","status":"publish","type":"post","link":"https:\/\/www.testleaf.com\/blog\/learn-15-testng-annotations-for-enhanced-selenium-testing\/","title":{"rendered":"Learn 15 TestNG Annotations for Enhanced Selenium Testing"},"content":{"rendered":"<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div><!--[if lt IE 9]><script>document.createElement('audio');<\/script><![endif]-->\n<audio class=\"wp-audio-shortcode\" id=\"audio-2129-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2024\/04\/Learn-15-TestNG-Annotations-for-Enhanced-Selenium-Testing.mp3?_=1\" \/><a href=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2024\/04\/Learn-15-TestNG-Annotations-for-Enhanced-Selenium-Testing.mp3\">https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2024\/04\/Learn-15-TestNG-Annotations-for-Enhanced-Selenium-Testing.mp3<\/a><\/audio>\n<p>&nbsp;<\/p>\n<h3><span data-contrast=\"auto\">@Test:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage<\/strong>: Marks a method as a test method.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: The methods annotated with @Test are executed when TestNG runs the test suite.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.Test;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @Test<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void myTestMethod() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@BeforeSuite:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed before the TestNG suite starts, runs only once.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: This annotation is used to set up global preconditions for the entire test suite.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.BeforeSuite;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @BeforeSuite<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void setUpBeforeSuite() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Global setup logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@AfterSuite:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed after the TestNG suite ends, runs only once.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: This annotation is used for cleanup or teardown tasks after the entire test suite has finished executing.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.AfterSuite;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @AfterSuite<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void tearDownAfterSuite() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Global teardown logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\"> <a href=\"https:\/\/www.testleaf.com\/course\/selenium-automation-certification-training-course.html\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-4555 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium.jpg\" alt=\"Selenium training in chennai\" width=\"2048\" height=\"512\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium.jpg 2048w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium-300x75.jpg 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium-1024x256.jpg 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium-768x192.jpg 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium-1536x384.jpg 1536w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Selenium-150x38.jpg 150w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/span><\/p>\n<h3><span data-contrast=\"auto\">@BeforeTest:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed before each &lt;test&gt; tag in the testng.xml file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: Methods annotated with @BeforeTest are executed before each &lt;test&gt; tag defined in the testng.xml file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.BeforeTest;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @BeforeTest<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void setUpBeforeTest() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test-specific setup logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@AfterTest:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage<\/strong>: Executed after each &lt;test&gt; tag in the testng.xml file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @AfterTest are executed after each &lt;test&gt; tag defined in the testng.xml file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.AfterTest;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @AfterTest<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void tearDownAfterTest() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test-specific teardown logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@BeforeClass:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed before the first test method in the current class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @BeforeClass are executed once before any test methods in the current class are executed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.BeforeClass;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @BeforeClass<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void setUpBeforeClass() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Class-specific setup logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@AfterClass:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed after all the test methods in the current class have been run.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @AfterClass are executed once after all test methods in the current class have been executed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.AfterClass;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @AfterClass<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void tearDownAfterClass() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Class-specific teardown logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@BeforeMethod:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed before each test method.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @BeforeMethod are executed before each test method in the class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">import org.testng.annotations.BeforeMethod;<\/span><\/b><\/p>\n<p><b><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @BeforeMethod<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void setUpBeforeMethod() {<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Method-specific setup logic goes here<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">}<\/span><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@AfterMethod:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage<\/strong>: Executed after each test method.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: Methods annotated with @AfterMethod are executed after each test method in the class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">import org.testng.annotations.AfterMethod;<\/span><\/b><\/p>\n<p><b><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @AfterMethod<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void tearDownAfterMethod() {<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Method-specific teardown logic goes here<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/b><br \/>\n<b><span data-contrast=\"auto\">}<\/span><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\"> <a href=\"https:\/\/www.testleaf.com\/all-courses.html\"><img decoding=\"async\" class=\"aligncenter wp-image-4599 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes.png\" alt=\"Online Classes\" width=\"2048\" height=\"512\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes.png 2048w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes-300x75.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes-1024x256.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes-768x192.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes-1536x384.png 1536w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/Online-Classes-150x38.png 150w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/span><\/p>\n<h3><span data-contrast=\"auto\">@DataProvider:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Supplies data to test methods, allowing them to be executed multiple times with different data sets.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @DataProvider provide the data required for test methods.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.DataProvider;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyDataProvider {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @DataProvider(name = &#8220;myData&#8221;)<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public Object[][] provideData() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Data generation logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@Parameters:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Provides values to test methods from the testng.xml file.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> TestNG injects parameter values from the testng.xml file into <a href=\"https:\/\/www.testleaf.com\/blog\/5-types-of-software-testing-models-in-2025\/\">test methods<\/a> annotated with @Parameters.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.Parameters;<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">import org.testng.annotations.Test;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @Test<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @Parameters(&#8220;myParameter&#8221;)<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void myTestMethod(String parameterValue) {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test logic using parameterValue goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@Listeners:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Defines custom listeners to <a href=\"https:\/\/www.testleaf.com\/blog\/top-10-performance-testing-tools-in-2025\/\">perform additional actions during test<\/a> execution, such as logging or reporting.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: Methods annotated with @Listeners are invoked during various stages of test execution to perform custom actions.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Java Code<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.Listeners;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">@Listeners(MyCustomListener.class)<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 \/\/ Test methods and other annotations go here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3>@Factory:<\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Allows the creation of test instances dynamically, enabling data-driven testing and parallel execution.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> The @Factory annotation is used to create multiple instances of a test class, each with different data sets or configurations.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><strong>Java Code\u00a0<\/strong><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.Factory;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyFactory {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @Factory<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public Object[] createInstances() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test instances creation logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h3><span data-contrast=\"auto\">@BeforeGroups:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage:<\/strong> Executed before the first <a href=\"https:\/\/www.testleaf.com\/blog\/different-software-testing-methods-every-qa-should-know\/\">test method<\/a> that belongs to any of the specified groups.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution:<\/strong> Methods annotated with @BeforeGroups are executed before any test methods belonging to the specified groups are executed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><strong>Java Code\u00a0<\/strong><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.BeforeGroups;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @BeforeGroups(&#8220;group1&#8221;)<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void setUpBeforeGroup() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Group-specific setup logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\"> <a href=\"https:\/\/www.testleaf.com\/about-us.html\"><img decoding=\"async\" class=\"aligncenter wp-image-4556 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship.png\" alt=\"One to one mentorship\" width=\"2048\" height=\"512\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship.png 2048w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship-300x75.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship-1024x256.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship-768x192.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship-1536x384.png 1536w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/04\/One-to-one-mentorship-150x38.png 150w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/span><\/p>\n<h3><span data-contrast=\"auto\">@AfterGroups:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\"><strong>Usage<\/strong>: Executed after all the test methods that belong to any of the specified groups have been run.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\"><strong>Execution<\/strong>: Methods annotated with @AfterGroups are executed after all test methods belonging to the specified groups have been executed.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<p><strong>Java Code\u00a0<\/strong><\/p>\n<p><b><i><span data-contrast=\"auto\">import org.testng.annotations.AfterGroups;<\/span><\/i><\/b><\/p>\n<p><b><i><span data-contrast=\"auto\">public class MyTestClass {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 @AfterGroups(&#8220;group1&#8221;)<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 public void tearDownAfterGroup() {<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Group-specific teardown logic goes here<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">\u00a0\u00a0\u00a0 }<\/span><\/i><\/b><br \/>\n<b><i><span data-contrast=\"auto\">}<\/span><\/i><\/b><br \/>\n<span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><span class=\"TextRun SCXW224075645 BCX8\" lang=\"EN-US\" style=\"font-size: 16px;\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW224075645 BCX8\">These examples illustrate how each annotation is used and executed in TestNG-based <a href=\"https:\/\/www.testleaf.com\/blog\/future-career-opportunities-for-selenium-testing-in-india\/\">Selenium tests<\/a>.<\/span><\/span><span class=\"EOP SCXW224075645 BCX8\" style=\"font-size: 16px;\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:279}\">\u00a0<\/span><\/p>\n<h6><strong>Author&#8217;s Bio<\/strong>:<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2404 alignleft\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design.png\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design.png 250w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design-150x150.png 150w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design-96x96.png 96w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>As CEO of TestLeaf, I\u2019m 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.<\/p>\n<p><strong>Babu Manickam<\/strong><\/p>\n<p>CEO &#8211; Testleaf<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"https:\/\/in.linkedin.com\/in\/babu-manickam\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/07\/linkedin.png\" alt=\"LinkedIn Logo\" width=\"28\" height=\"28\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; @Test:\u00a0 Usage: Marks a method as a test method.\u00a0 Execution: The methods annotated with @Test are executed when TestNG runs the test suite.\u00a0 Java Code\u00a0 import org.testng.annotations.Test; public class MyTestClass { \u00a0\u00a0\u00a0 @Test \u00a0\u00a0\u00a0 public void myTestMethod() { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Test logic goes here \u00a0\u00a0\u00a0 } } \u00a0 @BeforeSuite:\u00a0 Usage: Executed before the TestNG &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.testleaf.com\/blog\/learn-15-testng-annotations-for-enhanced-selenium-testing\/\"> <span class=\"screen-reader-text\">Learn 15 TestNG Annotations for Enhanced Selenium Testing<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[5,120,115,114,55,108,67,16,42,61],"tags":[147,29,146],"class_list":["post-2129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-automation-framework-interview-questions","category-automation-interview-questions","category-automation-questions","category-automation-tester","category-automation-testing","category-automation-testing-courses-in-chennai","category-selenium","category-software-testing","category-test-automation","tag-annotations","tag-selenium","tag-testng"],"acf":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/2129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/comments?post=2129"}],"version-history":[{"count":12,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/2129\/revisions"}],"predecessor-version":[{"id":2421,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/2129\/revisions\/2421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/media\/2132"}],"wp:attachment":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/media?parent=2129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/categories?post=2129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/tags?post=2129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}