What are TestNG Annotations? | LambdaTest

Curious about TestNG annotations? :thinking: Watch this video to learn all you need to know! :video_camera:

#TestNG #Testing #QA #Automation

TestNG annotations are like special tags or markers in the TestNG framework that define how methods should be treated during test execution. These annotations help manage the order and behavior of test methods, making the test execution process more efficient and flexible.

Basic Overview of Annotations in TestNG

  • @Test: Marks a method as a test method.
  • @BeforeSuite: Runs before all tests in the suite.
  • @AfterSuite: Runs after all tests in the suite.
  • @BeforeTest: Runs before any test method in the tag.
  • @AfterTest: Runs after all test methods in the tag.
  • @BeforeClass: Runs before the first method of the current class.
  • @AfterClass: Runs after all methods of the current class.
  • @BeforeMethod: Runs before each test method.
  • @AfterMethod: Runs after each test method.
  • @DataProvider: Provides data to a test method.
  • @Factory: Marks a method as a factory for creating test instances

Follow this guide to learn more about Annotation in detail:

Hey, here’s my take on the essential TestNG annotations:

Functional Roles of TestNG Annotations

  • @Test: This is the primary annotation that marks a method as a test method to be run.
  • @BeforeSuite / @AfterSuite: Used for setting up or cleaning resources before or after the entire suite runs. For example, opening or closing a database connection.
  • @BeforeTest / @AfterTest: These annotations handle setup or teardown operations that need to occur before or after any test methods within a specified test group or tag.
  • @BeforeClass / @AfterClass: These manage operations at the class level, such as initializing or releasing class-level resources.
  • @BeforeMethod / @AfterMethod: Used to perform operations before and after each test method, such as resetting variables or configurations.
  • @DataProvider: Facilitates the parameterization of test methods, allowing them to run multiple times with different sets of data.
  • @Factory: Enables dynamic creation of test instances, supporting parameterized test classes.

Here’s how I’ve applied TestNG annotations in my real-world projects:

  • @Test: Marks the test methods that will be executed. For example:
@Test
public void testLogin() { 
    // test code
}

This is the core of any test suite, defining what should be tested.

  • @BeforeSuite / @AfterSuite: Often used for configuration files setup and teardown.

For instance:

@BeforeSuite
public void setupSuite() { 
    // setup code
}
@AfterSuite
public void teardownSuite() { 
    // cleanup code
}

This ensures the environment is properly prepared and cleaned up before and after the entire suite runs.

  • @BeforeTest / @AfterTest: Ideal for initializing and cleaning up test environments.

For example:

@BeforeTest
public void setupTest() { 
    // test setup code
}
@AfterTest
public void cleanupTest() { 
    // test cleanup code
}

This helps in setting up prerequisites and cleaning up resources for test groups.

  • @BeforeClass / @AfterClass: Useful for web driver initialization and termination in Selenium tests. For example:
@BeforeClass
public void setupClass() { 
    // WebDriver initialization code
}
@AfterClass
public void teardownClass() { 
    // WebDriver termination code
}

This ensures that the WebDriver is ready before any test methods in the class run and properly closed afterward.

  • @BeforeMethod / @AfterMethod: Used for setting up test data or state before each test, and cleaning up after each test. For instance:
@BeforeMethod
public void setupMethod() { 
    // setup code
}
@AfterMethod
public void cleanupMethod() { 
    // cleanup code
}

This is crucial for ensuring that each test runs in a consistent state.

  • @DataProvider: Supplies multiple sets of data to a test method. For example:
@DataProvider(name = "userData")
public Object[][] getData() {
    return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} };
}
@Test(dataProvider = "userData")
public void testLogin(String username, String password) { 
    // test code using username and password
}

This enables testing with different sets of input data efficiently.

  • @Factory: Allows creating multiple instances of a test class dynamically.

For example:

@Factory
public Object[] createInstances() {
    return new Object[] { new TestClass(1), new TestClass(2) };
}

This is useful for running the same tests with different configurations.

These annotations have been invaluable in organizing and managing my test cases, ensuring each test runs smoothly and effectively.