This post was most recently updated on July 31st, 2024
Annotations in TestNG
Annotation is a feature introduced in Java 5 and is used to add a piece of code to the java source code.
Annotations are easy to use, maintain and understand.
Annotations provide many useful attributes to use in the code.
It is easier to find where the error occurred in the code with the use of Annotations.
TestNG Annotations made the life of testers very easy. Based on your requirements, you can access the test methods, i.e., it has no predefined pattern or format.
Parallel, multiple and specific executions of test cases is possible with Annotations.
TestNG Annotations Groups and Hierarchy
Image: TestNG Annotations Hierarchy
In this section, we will explain the order of execution procedure in the TestNG.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package TestNG; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestNGFlow { @BeforeMethod public void BeforeMethod(){ System.out.println("Executes BeforeMethod"); } @AfterMethod public void AfterMethod(){ System.out.println("Executes AfterMethod"); } @BeforeTest public void BeforeTest(){ System.out.println("Executes BeforeTest"); } @AfterTest public void AfterTest(){ System.out.println("Executes AfterTest"); } @Test // Test Case public void TestCase(){ System.out.println("Executes Test case"); } @Test // Test Case 1 public void TestCase1(){ System.out.println("Executes Test Case1"); } @BeforeSuite public void BeforeSuite(){ System.out.println("Executes BeforeSuite"); } @AfterSuite public void AfterSuite(){ System.out.println("Executes AfterSuite"); } @BeforeClass public void BeforeClass(){ System.out.println("Executes BeforeClass"); } @AfterClass public void AfterClass(){ System.out.println("Executes AfterClass"); } } |
Now let’s create a TestNG file as “TestNG.xml” to execute the above code.
1 2 3 4 5 6 7 |
<suite name = "Suite"> <test name = "Test"> <classes> <class name = "TestNG.TestNGFlow"/> </classes> </test> </suite> |
Let’s check the output:
Let’s discuss what happens in the above code,
First BeforeSuite() method will execute as it executes before all the methods.
Eventually, AfterMethod will execute after all the methods and for each test present in the class but before executing the test case. That’s why Aftermethod is executing after each test case.
Then annotations BeforeTest(), BeforeClass(), AfterClass(), and AfterTest() methods respectively of the order mentioned are executed only once.
AfterSuite will always execute after all the methods and only once.
The following is a table containing information about all the annotations provided by TestNG and a description of them:
Annotation | Description |
@BeforeSuite |
This annotated method will be executed before any tests declared inside a TestNG suite. |
@AfterSuite | This annotated method will be executed after any tests declared inside a TestNG suite. |
@BeforeTest | This annotated method will be executed before each test section declared inside a TestNG suite. |
@AfterTest | This annotated method will be executed after each test section declared inside a TestNG suite. |
@BeforeGroup | BeforeGroups annotated method will run before the first test method run from the specified group. |
@AfterGroup | AfterGroups annotated method will run after all the test method from the specified group. |
@BeforeClass | This annotated method will execute before any of the test methods of a test class. |
@AfterClass | This annotated method will execute after the execution of every test methods of a test class is executed. |
@BeforeMethod | This annotated method is executed before the execution of each test method. |
@AfterMethod | This annotated method will execute before the execution of each test method. |
@DataProvider | DataProvider is used to provide any data for parameterization. |
@Factory | The factory is used to run a set of test cases with different values. |
@Listeners | Listeners are used in tracking the execution status and logging purpose. |
@Parameters | Parameters are used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at runtime. |
@Test | A class or method is considered as a test method. If used at class level, all public methods of a class will be considered as a test method. |
In the next topic, we will discuss TestNG Annotations in depth.