TestNG Annotations Cheat Sheet

Introduction

TestNG is a powerful testing framework inspired by JUnit and NUnit. It is designed to cover all categories of tests, including unit, functional, end-to-end, integration, etc. TestNG offers various annotations that help to configure and execute test methods in a flexible manner. This cheat sheet provides a comprehensive list of TestNG annotations along with their descriptions and examples to help you effectively use TestNG in your testing process.

TestNG Annotations Cheat Sheet

Annotation Description
@Test Marks a method as a test method.
@BeforeSuite The annotated method will run before all tests in the suite.
@AfterSuite The annotated method will run after all tests in the suite have run.
@BeforeTest The annotated method will run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest The annotated method will run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups The annotated method will run before the first test method that belongs to any of these groups is invoked.
@AfterGroups The annotated method will run after all the test methods that belong to any of these groups have run.
@BeforeClass The annotated method will run before the first test method in the current class is invoked.
@AfterClass The annotated method will run after all the test methods in the current class have been run.
@BeforeMethod The annotated method will run before each test method.
@AfterMethod The annotated method will run after each test method.
@DataProvider Marks a method as a data provider for a test method.
@Factory Marks a method as a factory that returns objects to be used as test classes.
@Listeners Defines listeners on a test class.
@Parameters Passes parameters to test methods.
@BeforeSuite Runs once before all tests in the suite.
@AfterSuite Runs once after all tests in the suite have run.
@BeforeTest Runs before any test method belonging to the classes inside the <test> tag is run.
@AfterTest Runs after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups Runs before the first test method that belongs to any of these groups is invoked.
@AfterGroups Runs after all the test methods that belong to any of these groups have run.
@BeforeClass Runs before the first test method in the current class is invoked.
@AfterClass Runs after all the test methods in the current class have been run.
@BeforeMethod Runs before each test method.
@AfterMethod Runs after each test method.
@DataProvider Marks a method as a data provider for a test method.
@Factory Marks a method as a factory that returns objects to be used as test classes.
@Listeners Defines listeners on a test class.
@Parameters Passes parameters to test methods.

Explanation and Examples of TestNG Annotations

@Test

Description: Marks a method as a test method. The method with this annotation will be executed as a part of the test.
Example:

@Test
public void testMethod() {
    System.out.println("This is a test method.");
}

Explanation: The testMethod will be executed as a test case when running the test suite.

@BeforeSuite

Description: The annotated method will run before all tests in the suite.
Example:

@BeforeSuite
public void setupSuite() {
    System.out.println("Setup before the test suite.");
}

Explanation: This method will be executed once before any of the test methods in the suite.

@AfterSuite

Description: The annotated method will run after all tests in the suite have run.
Example:

@AfterSuite
public void teardownSuite() {
    System.out.println("Teardown after the test suite.");
}

Explanation: This method will be executed once after all test methods in the suite have completed.

@BeforeTest

Description: The annotated method will run before any test method belonging to the classes inside the <test> tag is run.
Example:

@BeforeTest
public void setupTest() {
    System.out.println("Setup before the test methods.");
}

Explanation: This method will be executed before any test method in the <test> tag.

@AfterTest

Description: The annotated method will run after all the test methods belonging to the classes inside the <test> tag have run.
Example:

@AfterTest
public void teardownTest() {
    System.out.println("Teardown after the test methods.");
}

Explanation: This method will be executed after all test methods in the <test> tag.

@BeforeGroups

Description: The annotated method will run before the first test method that belongs to any of these groups is invoked.
Example:

@BeforeGroups("group1")
public void setupGroup() {
    System.out.println("Setup before the group tests.");
}

Explanation: This method will be executed before the first test method belonging to the "group1" group.

@AfterGroups

Description: The annotated method will run after all the test methods that belong to any of these groups have run.
Example:

@AfterGroups("group1")
public void teardownGroup() {
    System.out.println("Teardown after the group tests.");
}

Explanation: This method will be executed after all test methods belonging to the "group1" group.

@BeforeClass

Description: The annotated method will run before the first test method in the current class is invoked.
Example:

@BeforeClass
public void setupClass() {
    System.out.println("Setup before the class methods.");
}

Explanation: This method will be executed once before any test method in the current class.

@AfterClass

Description: The annotated method will run after all the test methods in the current class have been run.
Example:

@AfterClass
public void teardownClass() {
    System.out.println("Teardown after the class methods.");
}

Explanation: This method will be executed once after all test methods in the current class.

@BeforeMethod

Description: The annotated method will run before each test method.
Example:

@BeforeMethod
public void setupMethod() {
    System.out.println("Setup before each test method.");
}

Explanation: This method will be executed before each test method in the current class.

@AfterMethod

Description: The annotated method will run after each test method.
Example:

@AfterMethod
public void teardownMethod() {
    System.out.println("Teardown after each test method.");
}

Explanation: This method will be executed after each test method in the current class.

@DataProvider

Description: Marks a method as a data provider for a test method. It is used to supply data to a test method.
Example:

@DataProvider(name = "data")
public Object[][] dataProvider() {
    return new Object[][] { {1, "a"}, {2, "b"} };
}

@Test(dataProvider = "data")
public void testMethod(int num, String str) {
    System.out.println("Data: " + num + ", " + str);
}

Explanation: The dataProvider method supplies data to the testMethod.

@Factory

Description: Marks a method as a factory that returns objects to be used as test classes.
Example:

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

Explanation: The createInstances method returns an array of TestClass instances.

@Listeners

Description: Defines listeners on a test class, which can be used to modify the behavior of the tests.
Example:

@Listeners(MyListener.class)
public class TestClass {
    @Test
    public void testMethod() {
        System.out.println("Test method.");
    }
}

Explanation: The @Listeners annotation registers the MyListener class to listen to test events.

@Parameters

Description: Passes parameters to test methods from the testng.xml file.
Example:

@Test
@Parameters({"param1", "param2"})
public void testMethod(String param1, String param2) {
    System.out.println("Parameter values: " + param1 + ", " + param2);
}

Explanation

Explanation: The @Parameters annotation allows you to pass parameters to the test method from the testng.xml configuration file.

Conclusion

TestNG’s powerful annotations provide flexibility and control over the execution of test methods, enabling you to set up and manage your test suites efficiently. This cheat sheet offers a quick reference to the most commonly used TestNG annotations, helping you streamline your testing process. Keep this guide handy to make the most of TestNG in your automated testing efforts. Happy testing!

Leave a Comment

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

Scroll to Top