Your Corporate Life

The Annotation Hierarchy in TestNG: A Complete Guide for Test Automation

In the world of test automation, TestNG is a powerful testing framework that provides extensive functionality to create and manage test cases. One of the reasons for its popularity is its rich set of annotations, which enables you to define the execution flow of your tests and specify setup and teardown methods. Understanding the annotation hierarchy is crucial to mastering TestNG and optimizing the test execution process.

In this article, we’ll dive into the annotation hierarchy in TestNG, exploring each annotation and its role in the test lifecycle. Let’s get started.


Overview of TestNG Annotations

Annotations in TestNG serve as markers for different points in the test execution lifecycle. They control when methods are executed, making your code organized, modular, and reusable. The TestNG framework provides a specific order or hierarchy of annotations, ensuring that each one is executed in a defined sequence.

Here’s the hierarchy from the highest to lowest level of control:

  1. @BeforeSuite / @AfterSuite
  2. @BeforeTest / @AfterTest
  3. @BeforeGroups / @AfterGroups
  4. @BeforeClass / @AfterClass
  5. @BeforeMethod / @AfterMethod
  6. @Test

Let’s look at each of these in detail.


1. @BeforeSuite and @AfterSuite

  • @BeforeSuite: Executes only once, before any tests within the suite are run.
  • @AfterSuite: Executes only once, after all tests within the suite have finished.

This is useful for performing actions such as setting up global configurations, connecting to a database, or initializing resources needed across the entire test suite.

Example:

@BeforeSuite
public void beforeSuite() {
    System.out.println("Before Suite - Global Setup");
}

@AfterSuite
public void afterSuite() {
    System.out.println("After Suite - Global Teardown");
}

2. @BeforeTest and @AfterTest

  • @BeforeTest: Runs before each <test> tag in the XML configuration file.
  • @AfterTest: Runs after each <test> tag in the XML configuration file.

These annotations help configure settings or initialize test data for specific test configurations. They’re often used for test-specific configurations, such as preparing test data sets or resetting environment settings.

Example:

@BeforeTest
public void beforeTest() {
    System.out.println("Before Test - Test Configuration Setup");
}

@AfterTest
public void afterTest() {
    System.out.println("After Test - Test Configuration Teardown");
}

3. @BeforeGroups and @AfterGroups

  • @BeforeGroups: Runs before each group of test methods that are grouped together.
  • @AfterGroups: Runs after each group of test methods.

These are particularly useful when you want to perform setup or cleanup operations for specific groups of tests, such as UI tests, API tests, or integration tests.

Example:

@BeforeGroups("api")
public void setupAPIGroup() {
    System.out.println("Before Groups - Setup for API Tests");
}

@AfterGroups("api")
public void teardownAPIGroup() {
    System.out.println("After Groups - Teardown for API Tests");
}

4. @BeforeClass and @AfterClass

  • @BeforeClass: Runs before the first test method in the current class is invoked.
  • @AfterClass: Runs after all test methods in the current class have been executed.

Use these annotations for class-level setups like initializing WebDriver for UI tests or establishing database connections relevant to a specific test class.

Example:

@BeforeClass
public void beforeClass() {
    System.out.println("Before Class - Setup for Test Class");
}

@AfterClass
public void afterClass() {
    System.out.println("After Class - Teardown for Test Class");
}

5. @BeforeMethod and @AfterMethod

  • @BeforeMethod: Executes before each test method in the current class.
  • @AfterMethod: Executes after each test method in the current class.

These are often used for setup and cleanup tasks that need to happen before and after each individual test method, such as resetting application state or logging test results.

Example:

@BeforeMethod
public void beforeMethod() {
    System.out.println("Before Method - Setup for each Test Method");
}

@AfterMethod
public void afterMethod() {
    System.out.println("After Method - Teardown for each Test Method");
}

6. @Test

This is the primary annotation in TestNG, representing a test method. Each method annotated with @Test is treated as a test case, and TestNG will run them based on their priority, dependencies, or parameters.

Example:

@Test
public void testMethod() {
    System.out.println("Test Method Execution");
}

You can add parameters to @Test to customize its behavior:

  • priority: To define the order of execution.
  • dependsOnMethods: To create dependencies between tests.
  • enabled: To include or exclude specific tests.

Understanding the Execution Flow

In a test class, TestNG will execute annotations in the defined hierarchy, ensuring that higher-level annotations (@BeforeSuite, @BeforeTest) execute before lower-level ones (@BeforeMethod, @Test).

A typical execution flow for a TestNG test suite could look like this:

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

This hierarchy helps organize the testing lifecycle, ensuring that setup and teardown methods are executed at the appropriate stages, thus making test cases more efficient and manageable.


Conclusion

Mastering TestNG annotations and their hierarchy is essential for creating clean, modular, and efficient test automation scripts. By understanding this structure, you can better organize and control the execution flow of your tests, leading to improved testing reliability and performance.


Looking to Boost Your Test Automation Career?

At Your Corporate Life, we offer a Job Update Service designed to keep you ahead of the curve with daily job referrals and updates on Manual Testing, Automation Testing, ETL Testing, Fresher IT, Developer, and DevOps roles. Here’s how we can help:

  1. Job Update Service – Get daily job updates on WhatsApp for just ₹499/year.
  2. Naukri Profile Optimization – Enhance your profile visibility for ₹999.
  3. LinkedIn Profile Optimization – Build a professional LinkedIn presence for ₹999.
  4. Mock Interviews – Sharpen your interview skills for ₹499 per session.
  5. Placement Assistance – Comprehensive job placement support for ₹2999.
  6. Resume and Cover Letter Writing – Tailored to your experience level.

Take your career to the next level with our tailored services, ensuring you’re well-prepared and well-connected in your job search. Reach out to us via WhatsApp and let’s discuss how we can help you succeed in your testing career!

Leave a Comment

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

Scroll to Top