TestNG Annotations in Selenium Java – Complete Guide
By Bhau Automation • Master TestNG Annotations & Execution Flow
🎯 What You Will Learn in This Video
- What is a TestNG annotation?
- Exact use and purpose of TestNG annotations
- Complete list of all TestNG annotations
- How to use TestNG annotations in your test scripts
- TestNG annotations execution flow and hierarchy
- Real-world examples and best practices
💡 Pro Tip: Understanding the execution flow of TestNG annotations is crucial for writing efficient and maintainable test automation scripts.
📌 What is a TestNG Annotation?
TestNG Annotations are special markers (starting with @) that provide metadata about test methods and control the test execution flow. They tell TestNG when and how to execute specific methods in your test suite.
Annotations help you:
- Define setup and teardown methods
- Control test execution order
- Group related tests together
- Manage test dependencies
- Configure parallel execution
🔧 Exact Use of TestNG Annotations
TestNG annotations serve multiple critical purposes in test automation:
- ✅ Pre-Configuration: Set up test environment before execution
- ✅ Test Execution: Mark methods as executable test cases
- ✅ Post-Execution Cleanup: Clean up resources after tests
- ✅ Test Organization: Group and prioritize tests logically
- ✅ Data Management: Supply test data through DataProvider
📋 Complete List of TestNG Annotations
TestNG provides 10 main annotations for comprehensive test management:
| Annotation | Execution Level | Description |
|---|---|---|
@BeforeSuite | Suite Level | Runs once before all tests in the suite |
@AfterSuite | Suite Level | Runs once after all tests in the suite |
@BeforeTest | Test Level | Runs before all test methods in a test tag |
@AfterTest | Test Level | Runs after all test methods in a test tag |
@BeforeClass | Class Level | Runs once before the first test method in the class |
@AfterClass | Class Level | Runs once after all test methods in the class |
@BeforeMethod | Method Level | Runs before each test method |
@AfterMethod | Method Level | Runs after each test method |
@Test | Method Level | Marks a method as a test case |
@DataProvider | Method Level | Supplies data for parameterized tests |
🔄 TestNG Annotations Execution Flow
Understanding the execution order is critical for writing effective tests. Here is the complete hierarchy:
1. @BeforeSuite → Executes first (once per suite)
2. @BeforeTest → Before test tag execution
3. @BeforeClass → Before first method of class
4. @BeforeMethod → Before each @Test method
5. @Test → Actual test execution
6. @AfterMethod → After each @Test method
7. @AfterClass → After all methods in class
8. @AfterTest → After test tag execution
9. @AfterSuite → Executes last (once per suite)
💻 How to Use TestNG Annotations
Here is a practical example demonstrating all major annotations:
import org.testng.annotations.*;
public class TestNGAnnotationsDemo {
@BeforeSuite
public void beforeSuite() {
System.out.println("1. @BeforeSuite - Setup database connection");
}
@BeforeTest
public void beforeTest() {
System.out.println("2. @BeforeTest - Initialize test environment");
}
@BeforeClass
public void beforeClass() {
System.out.println("3. @BeforeClass - Launch browser");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("4. @BeforeMethod - Navigate to homepage");
}
@Test(priority = 1)
public void loginTest() {
System.out.println("5. @Test - Executing Login Test");
}
@Test(priority = 2)
public void searchTest() {
System.out.println("5. @Test - Executing Search Test");
}
@AfterMethod
public void afterMethod() {
System.out.println("6. @AfterMethod - Clear cookies");
}
@AfterClass
public void afterClass() {
System.out.println("7. @AfterClass - Close browser");
}
@AfterTest
public void afterTest() {
System.out.println("8. @AfterTest - Cleanup test data");
}
@AfterSuite
public void afterSuite() {
System.out.println("9. @AfterSuite - Close database connection");
}
}
🎯 Real-World Use Cases
- @BeforeSuite: Database connections, global configurations
- @BeforeClass: WebDriver initialization, browser launch
- @BeforeMethod: Login, navigation to test page
- @Test: Actual test case execution
- @AfterMethod: Logout, clear session data
- @AfterClass: Close browser, quit driver
- @AfterSuite: Generate reports, cleanup resources
❓ Common Interview Questions
Q: How many annotations are in TestNG?
A: TestNG provides 10 main annotations for test configuration and execution.
Q: What is the order of annotations in TestNG?
A: BeforeSuite → BeforeTest → BeforeClass → BeforeMethod → Test → AfterMethod → AfterClass → AfterTest → AfterSuite
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: TestNG Annotations in Selenium Java
🎓 Key Takeaways
- TestNG annotations provide complete control over test execution flow
- Understanding the annotation hierarchy prevents configuration errors
- Use @BeforeMethod and @AfterMethod for repetitive setup/teardown tasks
- @BeforeSuite and @AfterSuite are ideal for expensive one-time operations
- Proper annotation usage leads to maintainable and scalable test frameworks
⚡ Next Steps: Practice implementing all annotations in your test framework and observe the execution flow using print statements.
🚀 Created with ❤️ by Bhau Automation