TestNG Annotations Order With Examples

TestNG Annotation: We discussed various basic things about the TestNG framework in our previous articles. As we mentioned earlier, TestNG supports various annotations to handle multiple operations.

TestNG uses these annotations to provide several features to the user so they can build a robust testing framework. So, in this post, we will discuss different annotations present in TestNG in detail.

What is an Annotation?

Annotations are nothing but a piece of instruction for the compiler that you apply to classes, methods, or variables in your Java code. It is a predominant feature of the TestNG framework.

In TestNG, there are multiple annotations available that can be used for different tasks. So before starting, you should be aware of those annotations and their use so that you can use those in your Selenium webDriver project.

TestNG Annotations

TestNG uses annotations to define & control the flow of test methods and allow you to define the order in which your methods execute. Annotations allow you to customise your test case’s behaviour, set up preconditions, manage resources, and define data sources for your test methods. Using TestNG, you can use them to create well-organized, controlled, well-documented test suites. Some of the commonly used TestNG annotations are.

As per the TestNG official website, in TestNG we have 15 different annotations present, those are:

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass
@BeforeMethod
@DataProvider
@Listeners
@Test
@AfterSuite
@AfterTest
@AfterGroups
@AfterClass
@AfterMethod
@Factory
@Parameters

Let us try to understand all the above annotations one by one.

TestNG Annotation With Description

@BeforeSuite: The annotated method will be executed before any tests are declared inside a TestNG suite. We can use this tag to print what tests will run, the browser name, and the project name, which is common for all the tests. [ Indicates that a method should run before all test within a test suit XML file. ]

@AfterSuite: The annotated method will be executed after any tests declared inside a TestNG suite. [ Specifies that a method should run after all test suites within a test suit XML file. ]

@BeforeTest: A Suite can have several tests. The @BeforeTest annotated methods will be executed before running the <test> tag. We use this annotation type to set up any specific test configuration. [ Denotes a method that should run before all test methods belonging to a tag in the test suite XML. ]

@AfterTest: The annotated methods will execute after each <test> tag, declared inside a TestNG suite. [ Indicates that a method should run after all test methods belonging to a tag in the test suite XML. ]

Note: @BeforeTest and @AfterTest are unique to test levels. These annotated methods will not apply to all tests if a suite contains multiple tests.

@BeforeGroups: The BeforeGroups annotated method will run before any of the test methods of the specified group are executed.

@AfterGroups: The afterGroups annotated method will run after any of the test methods of the specified group gets executed.

@BeforeClass: A class can have multiple classes, each with various methods. So @BeforeClass annotated methods are executed before executing any of the methods of that class. [ Marks a method to run once before any test methods in the same class. Useful for setting up class-level resources. ]

@AfterClass: These annotated methods are executed after the execution of all test methods of a test class is executed. [ Specifies that a method should run once after all test methods in the same class. Useful for performing cleanup or finalization task for class-level resources. ]

Note: @BeforeClass and @AfterClass methods are unique to a particular class.

@BeforeMethod: If you declare a test case or method with this annotation, the annotated method will be executed before executing each @test annotated method. [ Denotes a method that should run before each test method within the class. Useful for setting up common preconditions for multiple test methods. ]

@AfterMethod: Those methods are annotated by the @AfterMethod method executed after executing each @test annotated method. [ Specifies that a method should run after each test method within the class. Useful for performing cleanup or resetting state between test methods. ]

@Test: You can use this annotation for the test method in class or method level. If you declare this annotation at the class level, all the methods in the class will be considered test methods. This is one of the important annotations in TestNG because we need to write the business logic inside that. [ Marks a method as a test method. Test methods are the actual test cases you want to execute. ]

@Parameters: This annotation is used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at runtime. [ Provides parameters to test methods from the test suite XML. ]

@Parameters({"username", "password"})
@Test
public void loginTest(String username, String password) {
//Test logic using the provided parametes.
}

@DataProvider: If any of your methods require input data, then we can pass the data to that method. So, we can use @DataProvider annotation to make that method a data supplier. The annotated method must return an object[][]. The method that wants input data from the data provider needs to use the data provider name you mentioned when creating the data provider.

Supplies data to test methods.

@DataProvider
public Object[][] testData() {
// provide test data as two dimentional array.
}

@Factory: Marks an annotated method as a factory that returns an array of class objects (Object[ ]). These class objects will then be used as test classes by TestNG. This is used to run a set of test cases with different values.

Used to create instances of test classes. Useful for creating multiple instances of a test class with different configurations. Example:

@Factory
public Object[] createInstances(){
//create and return instance of test class
}

@Listeners: This is applied to a test class. It defines an array of test listeners classes extending org.testng.ITestNGListener. It helps in tracking the execution status and logging purposes. Listeners can monitor and report events during test execution.
Example:

@Listners(MyTestListeners.class)
public class MyTestClass {
//Test methods and annotations 
}

TestNG Annotations Order

When you execute the test class, the Sequence of TestNG Annotations order is like below:

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite

TestNG Annotation With Example

package com.softwaretestingo.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 OrderOfAnnotation 
{
  @Test
  public void testMethod() 
  {
	  System.out.println("@Test");
  }
  @BeforeMethod
  public void beforeMethod() 
  {
	  System.out.println("@BeforeMethod");
  }

  @AfterMethod
  public void afterMethod() 
  {
	  System.out.println("@AfterMethod");
  }

  @BeforeClass
  public void beforeClass() 
  {
	  System.out.println("@BeforeClass");
  }

  @AfterClass
  public void afterClass() 
  {
	  System.out.println("@AfterClass");
  }

  @BeforeTest
  public void beforeTest() 
  {
	  System.out.println("@BeforeTest");
  }

  @AfterTest
  public void afterTest() 
  {
	  System.out.println("@AfterTest");
  }

  @BeforeSuite
  public void beforeSuite() 
  {
	  System.out.println("@BeforeSuite");
  }

  @AfterSuite
  public void afterSuite() 
  {
	  System.out.println("@AfterSuite");
  }
}

Output:

@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

The annotations, as mentioned earlier in TestNG, are to provide a better structure and readability to code. Also, it has some richer functionality; using those can save lots of time. After each script execution, it provides us with a detailed report to share those reports during status reporting.

But using the above annotations depends on your business requirement; hence, Choosing the right ones per your requirement is important. Therefore, choosing the right ones for proper use is important.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment