User's blog

Learn 15 TestNG Annotations for Enhanced Selenium Testing

@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. 

Leave a Comment

Your email address will not be published. Required fields are marked *

Accelerate Your Salary with Expert-Level Selenium Training

X