Execution order of Testng annotations

In this section, We are Going to Understand What is TestNG and What are TestNG Annotations? and How we can Define TestNG Annotations in Our Java Automation code.

We need a basic configuration, In order to use TestNG annotations in Selenium, Install and configure TestNg in Eclipse IDE .

What is TestNG?

Testng is Testing Framework For Java Programming Language and it covers a wider range of test categories: unit, functional, end-to-end, integration, etc. with more powerful and easy-to-use functionalities.

Types of Annotations in TestNg

Annotations are Pieces of Code inserted into the Java Program to Identify and Execute the Program in a Control Flow.

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

Let’s Understand the Above Annotations and Execution order of TestNG annotations With An Example.

Execution order of TestNG annotations

Test Scenario

  • Execute all TestNG annotations to Demonstrate the Execution Order

Code

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
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;

public class Execution {
    
      @Test
      public void Test() {
          System.out.println("This is Test ");
      }
      @BeforeMethod
      public void beforeMethod() {
          System.out.println("This is BeforeMethod ");
      }

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

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

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

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

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

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

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

      @BeforeSuite
      public void beforeSuiteMethod() {
          System.out.println("This Will be Executed in Before Suite");
      }
      @AfterSuite
      public void afterSuiteMethod() {
          System.out.println("This Will be Executed in After Suite");
    }
}

Code Explanation

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

@BeforeSuite public void beforeSuiteMethod() { System.out.println("This Will be Executed in Before Suite"); }

@BeforeSuite Annotation method will run before the execution of all the test methods in the suite.

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

@BeforeTest Annotation Method Can be Executed Before the First @Test Method and it can be executed Multiple Times Before Test Annotation.

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

@BeforeClass Annotation will be executed before the first method of the current class.

@BeforeMethod public void beforeMethod() { System.out.println("This is BeforeMethod "); }

@BeforeMethod Annotation will be executed before every @TEST Annotation method.

@Test public void Test() { System.out.println("This is Test "); }

@TEST Method will be Executed between the Before and After Method.

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

@AfterMethod Annotation will be executed After every @TEST Annotation method.

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

@AfterClass Annotation will be executed After the Execution of all methods in the current class.

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

@AfterTest Annotation will be executed after the execution of all the test methods of available classes.

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

@AfterSuite public void afterSuiteMethod() { System.out.println("This Will be Executed in After Suite"); }

@AfterSuite Annotation will run once after the execution of all tests in the suite is complete.

Output

To Run the TestNG Test Right Click on the Project and then click on TestNg Test under the Run As Option

We Have Successfully Demonstrated the Execution order of Testng annotations in Java.