Interview #73: Different annotations in TestNG and their sequence of order?

Interview #73: Different annotations in TestNG and their sequence of order?

TestNG (Test Next Generation) is a popular testing framework for Java that provides various annotations to control the flow of test execution. These annotations help define preconditions, test methods, and postconditions while ensuring a structured and organized test suite. Below is a detailed explanation of different TestNG annotations and their sequence of execution.

Disclaimer: For QA-Testing Jobs/Training, WhatsApp us @ 91-9606623245

1. List of TestNG Annotations and Their Purpose

TestNG provides the following annotations, which execute in a specific sequence:

Precondition Annotations:

@BeforeSuite

  • Runs once before all tests in the suite.
  • Typically used for setting up configurations such as initializing reports, database connections, or global settings.

@BeforeTest

  • Runs before any test method inside a <test> tag in the TestNG XML file.
  • Used for test-level setup like opening a browser or setting up the test environment.

@BeforeGroups

  • Runs before any test method belonging to a specific group.
  • Useful for setting up configurations for grouped tests.

@BeforeClass

  • Runs once before the first test method in the current class.
  • Used to set up class-level prerequisites.

@BeforeMethod

  • Runs before each test method.
  • Often used for setting up test data, launching browsers, or resetting configurations.


Test Execution Annotation:

@Test

  • Marks a method as a test case.
  • Can be customized with attributes like priority, invocation count, groups, expected exceptions, timeout, etc.


Postcondition Annotations:

@AfterMethod

  • Runs after each test method.
  • Typically used for cleaning up, logging out, or capturing test results.

@AfterClass

  • Runs once after all test methods in the current class.
  • Used for closing resources like browser sessions.

@AfterGroups

  • Runs after all test methods belonging to a specific group have executed.
  • Helps in group-level cleanup.

@AfterTest

  • Runs after all test methods inside a <test> tag in the TestNG XML file.
  • Used for post-test activities like clearing caches or closing database connections.

@AfterSuite

  • Runs once after all tests in the suite.
  • Generally used for reporting, database disconnection, or final cleanup.


2. Execution Order of TestNG Annotations

TestNG executes annotations in the following sequence:

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeGroups (if applicable)
  4. @BeforeClass
  5. @BeforeMethod
  6. @Test (Actual test execution)
  7. @AfterMethod
  8. @AfterClass
  9. @AfterGroups (if applicable)
  10. @AfterTest
  11. @AfterSuite

Each of these annotations follows a hierarchical execution, ensuring smooth test execution with proper setup and cleanup.


3. Example Code Demonstrating TestNG Annotations

Here’s an example to demonstrate the execution order of these annotations:

import org.testng.annotations.*;

public class TestNGExample {

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

    @BeforeTest
    public void beforeTest() {
        System.out.println("Before Test - Initialize test environment");
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("Before Class - Initialize class-level resources");
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before Method - Setup test data");
    }

    @Test
    public void testMethod1() {
        System.out.println("Executing Test Method 1");
    }

    @Test
    public void testMethod2() {
        System.out.println("Executing Test Method 2");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("After Method - Cleanup test data");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("After Class - Release class-level resources");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("After Test - Cleanup test environment");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("After Suite - Final cleanup and reporting");
    }
}        

Expected Console Output:

Before Suite - Setup global configurations
Before Test - Initialize test environment
Before Class - Initialize class-level resources
Before Method - Setup test data
Executing Test Method 1
After Method - Cleanup test data
Before Method - Setup test data
Executing Test Method 2
After Method - Cleanup test data
After Class - Release class-level resources
After Test - Cleanup test environment
After Suite - Final cleanup and reporting        

4. Key Points to Remember

  • @BeforeSuite and @AfterSuite execute only once per suite.
  • @BeforeTest and @AfterTest execute once per <test> block in the XML file.
  • @BeforeClass and @AfterClass execute once per class.
  • @BeforeMethod and @AfterMethod execute before and after each test method.
  • @BeforeGroups and @AfterGroups execute only for specific test groups.


5. Conclusion

Understanding TestNG annotations and their sequence is crucial for effective test automation. Proper use of these annotations ensures efficient test setup, execution, and teardown, leading to well-structured and maintainable test cases.


要查看或添加评论,请登录

Software Testing Studio | WhatsApp 91-9606623245的更多文章

社区洞察

其他会员也浏览了