TestNG Annotations - Deep Dive
TestNG (Test Next Generation) is a
powerful testing framework in Java that provides advanced features like
parallel execution, data-driven testing, and test configuration using
annotations. TestNG annotations control test execution order, dependencies, setup/teardown
operations, and parameterization.
1. TestNG Annotations Overview
TestNG uses annotations to define
test methods and lifecycle hooks. These annotations replace JUnit's @BeforeClass,
@AfterClass, etc., providing greater flexibility.
Basic TestNG Annotations:
Annotation |
Description |
@Test |
Marks a method as a test case |
@BeforeSuite |
Runs before the test suite |
@AfterSuite |
Runs after the test suite |
@BeforeTest |
Runs before any test in <test>
tag |
@AfterTest |
Runs after all tests in <test>
tag |
@BeforeClass |
Runs before the first method of
the class |
@AfterClass |
Runs after the last method of
the class |
@BeforeMethod |
Runs before each test method |
@AfterMethod |
Runs after each test method |
@DataProvider |
Supplies test data |
@Parameters |
Passes parameters from testng.xml |
@Factory |
Creates test instances
dynamically |
@Listeners |
Implements listeners for test
execution tracking |
@Ignore |
Ignores a test method |
2. Test Execution Flow
TestNG executes tests in the
following order:
- @BeforeSuite
- @BeforeTest
- @BeforeClass
- @BeforeMethod
- @Test
- @AfterMethod
- @AfterClass
- @AfterTest
- @AfterSuite
3. Core TestNG Annotations -
In-Depth
3.1 @Test
- Marks a method as a test case.
- Supports priority, timeouts, and expected
exceptions.
java
import org.testng.annotations.Test;
public class SampleTest {
@Test(priority = 1)
public void loginTest() {
System.out.println("Login
Test");
}
@Test(priority = 2)
public void dashboardTest() {
System.out.println("Dashboard
Test");
}
}
Attributes of @Test:
Attribute |
Description |
Example |
priority |
Defines execution order |
@Test(priority = 1) |
enabled |
Enables/disables a test |
@Test(enabled = false) |
dependsOnMethods |
Runs a test after dependent
tests |
@Test(dependsOnMethods =
{"loginTest"}) |
groups |
Groups tests for selective
execution |
@Test(groups =
"regression") |
expectedExceptions |
Handles expected exceptions |
@Test(expectedExceptions =
ArithmeticException.class) |
timeOut |
Fails if execution exceeds time |
@Test(timeOut = 2000) |
3.2 @BeforeSuite &
@AfterSuite
- @BeforeSuite runs once before all test cases
in a suite.
- @AfterSuite runs once after all test cases.
java
import org.testng.annotations.*;
public class SuiteConfig {
@BeforeSuite
public void setup() {
System.out.println("Before Suite:
Setting up test environment");
}
@AfterSuite
public void tearDown() {
System.out.println("After Suite:
Cleanup test environment");
}
}
3.3 @BeforeTest &
@AfterTest
- @BeforeTest runs before the first test method
in the <test> tag.
- @AfterTest runs after all methods in the <test>
tag.
java
public class TestConfig {
@BeforeTest
public void setup() {
System.out.println("Before Test: Open
Database Connection");
}
@AfterTest
public void cleanup() {
System.out.println("After Test:
Close Database Connection");
}
}
3.4 @BeforeClass &
@AfterClass
- @BeforeClass runs once before any test
method in a class.
- @AfterClass runs once after all methods in a
class.
java
public class ClassConfig {
@BeforeClass
public void beforeClass() {
System.out.println("Before Class:
Initialize Test Data");
}
@AfterClass
public void afterClass() {
System.out.println("After Class:
Cleanup Test Data");
}
}
3.5 @BeforeMethod &
@AfterMethod
- @BeforeMethod runs before each test method.
- @AfterMethod runs after each test method.
java
public class MethodConfig {
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method:
Open Browser");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method:
Close Browser");
}
@Test
public void testA() {
System.out.println("Executing Test
A");
}
@Test
public void testB() {
System.out.println("Executing Test
B");
}
}
Execution Order:
Before Method
Executing Test A
After Method
Before Method
Executing Test B
After Method
4. Parameterization with
@DataProvider & @Parameters
4.1 @DataProvider (Dynamic Data
Injection)
- Supplies data dynamically for test methods.
java
import
org.testng.annotations.DataProvider;
import
org.testng.annotations.Test;
public class DataProviderExample {
@DataProvider(name = "loginData")
public Object[][] getData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
System.out.println("Logging in
with: " + username + ", " + password);
}
}
4.2 @Parameters (XML-based Data
Injection)
- Injects parameters from testng.xml.
testng.xml:
xml
<suite name="Suite">
<test name="Test">
<parameter name="browser" value="Chrome"/>
<classes>
<class name="ParameterExample"/>
</classes>
</test>
</suite>
Java Class:
java
import org.testng.annotations.*;
public class ParameterExample {
@Test
@Parameters("browser")
public void setupTest(String browser) {
System.out.println("Running test
on: " + browser);
}
}
5. Dependency Management with dependsOnMethods
- Ensures a method executes only if another
method passes.
java
public class DependencyTest {
@Test
public void login() {
System.out.println("Login
Successful");
}
@Test(dependsOnMethods = {"login"})
public void dashboard() {
System.out.println("Dashboard
Loaded");
}
}
6. TestNG Listeners
(@Listeners)
- Listeners track test execution and generate
reports.
java
import org.testng.ITestListener;
import org.testng.ITestResult;
public class CustomListener implements
ITestListener {
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed:
" + result.getName());
}
}
Usage:
java
import
org.testng.annotations.Listeners;
@Listeners(CustomListener.class)
public class ListenerTest {
@Test
public void sampleTest() {
System.out.println("Executing
Sample Test");
}
}
7. Conclusion
TestNG annotations provide a
powerful way to structure and execute test cases efficiently. They allow:
- Flexible test execution
- Data-driven testing
- Parallel execution
- Dependency management
Would you like a practical example with a complete TestNG framework? 🚀