TestNG - Execution order of annotations

By xngo on February 24, 2019

TestNG does provide a lot of annotations but it is not very clear what is the execution order of these annotations. Here is the execution order results from the Java code below of the most commonly used annotations.

Ran Constructor.(Work Time = 642 ms)
Ran @BeforeTest method.(Work Time = 1181 ms)
Ran @BeforeClass method.(Work Time = 674 ms)
Ran @BeforeMethod method.(Work Time = 78 ms)
Ran @Test method.(Work Time = 341 ms)
Ran @AfterMethod method.(Work Time = 1441 ms)
Ran @AfterClass method.(Work Time = 1206 ms)
Ran @AfterTest method.(Work Time = 81 ms)

In the code below, I add a random sleep time for each annotation. This will ensure that the output will show the exact execution order of annotations.

import org.testng.annotations.BeforeTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
 
import java.util.Random;
 
public class AnnotationsExecutionOrder
{
  // Variables
  Random oRandom = new Random();
  final int MAX = 2000;
 
  // Constructor
  public AnnotationsExecutionOrder()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran Constructor.(Work Time = " + iRnd + " ms)");
  }
 
  @BeforeClass
  public void BeforeClass()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @BeforeClass method.(Work Time = " + iRnd + " ms)");
  }
 
  @BeforeTest
  public void BeforeTest()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @BeforeTest method.(Work Time = " + iRnd + " ms)");
  }
 
  @BeforeMethod
  public void BeforeMethod()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @BeforeMethod method.(Work Time = " + iRnd + " ms)");
  }
 
  @Test
  public void Test()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @Test method.(Work Time = " + iRnd + " ms)");
  }
 
  @AfterMethod
  public void AfterMethod()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @AfterMethod method.(Work Time = " + iRnd + " ms)");
  }
 
  @AfterTest
  public void AfterTest()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @AfterTest method.(Work Time = " + iRnd + " ms)");
  }
 
  @AfterClass
  public void AfterClass()
  {
    final int iRnd = oRandom.nextInt(MAX);
    this.pause(iRnd);
    System.out.println("Ran @AfterClass method.(Work Time = " + iRnd + " ms)");
  }
 
  // Simulate some processing time by pausing.
  private void pause(long lPauseInMillisSec)
  {
    try
    {
      Thread.sleep(lPauseInMillisSec);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
 
  }
}

Github

  • https://github.com/xuanngo2001/java-testng/blob/master/src/net/openwritings/testng/AnnotationsExecutionOrder.java

Reference

  • http://testng.org/doc/documentation-main.html#annotations

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.