What is TestNG Annotation Order?

A TestNG class can have various TestNG methods such as @BeforeTest, @AfterTest, @BeforeSuite, @BeforeClass, @BeforeMethod, @test, etc. In this article, we will explain the order of execution of different TestNG methods.

TestNG consists of the following methods to support the main @Test method. The order of execution should be as follows −


Key points in this order are:

  • First of all, beforeSuite() method is executed only once.

  • The afterSuite() method executes only once.

  • Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.

  • beforeMethod() method executes for each test case (every time for a new @Test), but before executing the test case.

  • afterMethod() method executes for each test case (every time for a new @Test), but after executing the test case.

  • In between beforeMethod() and afterMethod(), each test case (@Test annotation’s method) executes.

Approach/Algorithm to solve this problem:

  • Step 1 − import org.testng.annotations.* for TestNG.

  • Step 2 − Write an annotation as @test

  • Step 3 − Create a method for the @test annotation as test1.

  • Step 4 − Repeat the steps for test2 and test3.

  • Step 5 − Write different annotations and their respective methods. For example, @beforeSuite, @afterSuite, @beforeTest, @afterTest, @beforeClass, @afterClass, @beforeMethod, @afterMethod

  • Step 6 − Now create the testNG.xml as given below.

  • Step 7 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.


Use the following code to show the order of different TestNG methods −

import org.testng.annotations.*;
import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   public void testCase1() {
      System.out.println("in test case 1");
   // test case 2
   public void testCase2() {
      System.out.println("in test case 2");
   public void beforeMethod() {
      System.out.println("in beforeMethod");
   public void afterMethod() {
      System.out.println("in afterMethod");
   public void beforeClass() {
      System.out.println("in beforeClass");
   public void afterClass() {
      System.out.println("in afterClass");
   public void beforeTest() {
      System.out.println("in beforeTest");
   public void afterTest() {
      System.out.println("in afterTest");
   public void beforeSuite() {
      System.out.println("in beforeSuite");
   public void afterSuite() {
      System.out.println("in afterSuite");


This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
         <class name = "OrderofTestExecutionInTestNG"/>


in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite