TestNG Annotations Used in a Project
@Test
Marks a method as a test method.
TestNG executes each @Test method as an individual test case.
@BeforeSuite
Runs before all tests in the suite.
It is used for:
- Global configuration
- Resources required by the entire test suite
@AfterSuite
Runs after all tests in the suite are completed.
It is used for:
- Cleanup
- Generating test reports
@BeforeTest
Runs before each <test> tag in the testng.xml file.
It is used for per-test configuration.
@AfterTest
Runs after each <test> tag.
It is used for:
- Cleanup
- Post-test activities
@BeforeClass
Runs before the first test method in a class.
It is used for:
- Common test data
- Shared resources
@AfterClass
Runs after all test methods in a class have finished.
It is mainly used for cleanup.
@BeforeMethod
Runs before every @Test method.
It is commonly used for per-test setup, such as:
- Opening the browser
- Initializing objects
@AfterMethod
Runs after every @Test method.
It is commonly used for per-test cleanup, such as:
- Closing the browser
- Clearing test data
TestNG Execution Hierarchy
TestNG follows the execution order:
Suite → Test → Class → Method
The hierarchy opens from the outermost level first and closes from the innermost level.
DataProvider and Its Return Type
A DataProvider is TestNG's mechanism for supplying test data to test methods.
It separates test data from test logic, allowing the same test to execute multiple times with different input values.
Test methods receive the data through method parameters.
The return type of a DataProvider is:
Object[][]
It is a two-dimensional array of objects.
- Each row represents one set of test data.
- Each column represents one parameter.
Example:
@DataProvider(name = "data-provider")
public Object[][] dpMethod() {
return new Object[][] {
{"John", 25},
{"Alice", 30},
{"Bob", 35}
};
}
The above DataProvider returns:
- 3 rows
- 2 columns
Therefore, the test method executes three times, each time receiving different values of name and age.
TestNG Priority: Rules and Edge Cases
Execution order is assigned using the priority attribute of @Test.
Example:
public class TestNGFirstTest {
@Test(priority = 2)
public void a_test() { }
@Test(priority = 3)
public void c_test() { }
@Test(priority = 1)
public void b_test() { }
}
Rules
- Lower priority values execute first.
- A method with priority = 1 executes before priority = 2.
- Larger priority values execute later.
- TestNG first follows the annotation hierarchy, then applies priority.
Edge Cases
Same Priority
If multiple methods have the same priority, TestNG executes them in alphabetical order.
Mixed Priority
If some methods have a priority and others do not:
- Methods without a priority execute first.
- They execute in alphabetical order.
- Prioritized methods execute afterward.
Test Dependencies
dependsOnMethods
Specifies that a test depends on another test method.
Example:
@Test(dependsOnMethods = "loginTest")
public void dashboardTest() {
}
Multiple dependencies:
@Test(dependsOnMethods = {"loginTest", "createPostTest"})
public void verifyPostTest() {
}
dependsOnGroups
Creates dependencies between entire groups of tests instead of individual methods.
If the depended-on test or group fails, the dependent test is automatically skipped.
This prevents cascading false failures.
Verify vs Assert
Verify
Verify checks whether a condition is true.
If verification fails:
- Test execution continues.
- The failure is reported as a warning or non-fatal error.
Its purpose is to gather information without stopping the test.
Assert
Assert also checks whether a condition is true.
If the assertion fails:
- Test execution stops immediately.
- The failure is reported.
Summary
Verify
- Validates the condition.
- Continues execution even if validation fails.
Assert
- Validates the condition.
- Stops execution immediately if validation fails.
In TestNG terminology:
- SoftAssert behaves like Verify.
- Assert behaves like Hard Assert.
SoftAssert requires calling:
assertAll();
at the end of the test.
Creating testng.xml
The testng.xml file configures the test suite.
It specifies:
- Test suites
- Test classes
- Parameters
- Groups
- Parallel execution
- Listeners
Template Structure
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Test Suite">
<test name="Test Name">
<classes>
<class name="com.example.TestClass1"/>
<class name="com.example.TestClass2"/>
<class name="com.example.TestClass3"/>
</classes>
</test>
</suite>
Elements of testng.xml
<!DOCTYPE suite>
Specifies the TestNG DTD.
<suite>
The root element representing the entire test suite.
It contains one or more <test> elements.
<test>
Defines an individual test inside the suite.
It may contain:
- Parameters
- Configurations
- Classes
- Methods
<classes> and <class>
Specify the test classes to execute.
Groups can also be defined and selected here to execute:
- Smoke Tests
- Regression Tests
from the same codebase.
Generating Reports with Listeners
Step 1
Create the testng.xml file and configure the suite.
Step 2
Configure report generation by adding a listeners section.
Example:
<listeners>
<listener class-name="org.testng.reporters.ExtentReporterNG"/>
</listeners>
Step 3
Run the TestNG suite.
Reports are generated automatically.
The default TestNG output directory is:
test-output
It contains the built-in reports such as:
- emailable-report.html
- index.html
FAQs
1. Which TestNG annotations are commonly used in a project?
Common TestNG annotations include:
- @Test
- @BeforeSuite
- @AfterSuite
- @BeforeTest
- @AfterTest
- @BeforeClass
- @AfterClass
- @BeforeMethod
- @AfterMethod
2. What is the return type of a DataProvider?
The return type of a DataProvider is:
Object[][]
It is a two-dimensional array.
- Each row represents one data set.
- Each column represents one parameter.
The test executes once for each row.
3. How does TestNG Priority work?
- Lower priority values execute first.
- TestNG follows the annotation hierarchy before applying priority.
- Same priorities execute alphabetically.
- Methods without a priority execute before prioritized methods.
4. How do you create dependencies between tests?
Test dependencies are created using:
dependsOnMethodsdependsOnGroups
If the dependency fails, the dependent test is skipped.
5. What is the difference between Verify and Assert?
Verify
- Reports failures.
- Continues test execution.
- Similar to SoftAssert.
Assert
- Stops test execution immediately upon failure.
- Similar to Hard Assert.
6. What is the structure of testng.xml?
The basic structure is:
<suite>
<test>
<classes>
<class/>
</classes>
</test>
</suite>
The TestNG DTD is declared at the top of the file.
7. How do you generate reports in TestNG?
Add a listener entry inside testng.xml.
Example:
<listeners>
<listener class-name="org.testng.reporters.ExtentReporterNG"/>
</listeners>