Execution Order of TestNG annotations

In this blogpost, we will go through execution order of TestNG annotations, with the simple working code example.

The hierarchical representation of execution order of annotations used in the below code snippet is shown as below.

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

Create a new TestNG class, with the name TestClass1.java 

package com;

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 TestClass1 {

@BeforeSuite
public void beforeSuite() {
System.out.println(“Inside Before Suite Method.”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“Inside Before Class of TestClass1.”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“Inside Before Test Method of TestClass1.”);
}

@BeforeMethod
public void beforeMethod1() {
System.out.println(“Inside Before Method 1 of TestClass1.”);
}

@BeforeMethod
public void beforeMethod2() {
System.out.println(“Inside Before Method 2 of TestClass1.”);
}

@Test
public void testMethod1() {
System.out.println(“Inside Test Method 1 of TestClass1.”);
}

@Test
public void testMethod2() {
System.out.println(“Inside Test Method 2 of TestClass1.”);
}

@AfterTest
public void afterTest() {
System.out.println(“Inside After Test Method of TestClass1.”);
}

@AfterMethod
public void afterMethod1() {
System.out.println(“Inside After Method 1 of TestClass1.”);
}

@AfterMethod
public void afterMethod2() {
System.out.println(“Inside After Method 2 of TestClass1.”);
}

@AfterClass
public void afterClass() {
System.out.println(“Inside After Class Method of TestClass1.”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“Inside After Suite Method.”);
}

}

Now, execute the TestNG class as TestNG Test, and you will see the output as below.

Output:

Inside Before Suite Method.
Inside Before Test Method of TestClass1.
Inside Before Class of TestClass1.
Inside Before Method 1 of TestClass1.
Inside Before Method 2 of TestClass1.
Inside Test Method 1 of TestClass1.
Inside After Method 1 of TestClass1.
Inside After Method 2 of TestClass1.
Inside Before Method 1 of TestClass1.
Inside Before Method 2 of TestClass1.
Inside Test Method 2 of TestClass1.
Inside After Method 1 of TestClass1.
Inside After Method 2 of TestClass1.
Inside After Class Method of TestClass1.
Inside After Test Method of TestClass1.
PASSED: testMethod1
PASSED: testMethod2

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

Inside After Suite Method.

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Let us jot down the key observations from the above execution result.

  • BeforeSuite and AfterSuite methods would be executed only once.
  • BeforeTest and AfterTest methods would be executed only once per test class.
  • BeforeClass and AfterClass methods would be executed only once per class.
  • BeforeMethod and AfterMethod methods would be executed for all @Test methods.
  • Test method contains actual business logic of making a test pass/ fail.
  • If the TestNG class contains more than one methods marked with @BeforeMethod and @AfterMethod, then all the methods would be executed before and after every @Test method.

That is all from this blogpost, hope it helps you!

Cheers 🙂

 

 

Advertisements