Have a question?
Message sent Close
View Categories

 Hierarchy In TestNG Annotations

AnnotationHierarchyExample.java
package com.example.tests;

import org.testng.annotations.*;

/**

 * Demonstrates the hierarchy and execution order of TestNG annotations.

 */

public class AnnotationHierarchyExample {

    @BeforeSuite
    public void beforeSuite() {

        System.out.println("[BeforeSuite] - runs once before all tests in the suite");

    }

    @BeforeTest
    public void beforeTest() {

        System.out.println("[BeforeTest] - runs before any @Test belonging to <test> tag");

    }

    @BeforeClass

    public void beforeClass() {

        System.out.println("[BeforeClass] - runs once before the first method in the current class");

    }

    @BeforeMethod
    public void beforeMethod() {

        System.out.println("[BeforeMethod] - runs before each @Test method");

    }

    @Test
    public void testA() {

        System.out.println("[Test] - testA execution");

    }

    @Test
    public void testB() {

        System.out.println("[Test] - testB execution");

    }

    @AfterMethod
    public void afterMethod() {

        System.out.println("[AfterMethod] - runs after each @Test method");

    }

    @AfterClass
    public void afterClass() {

        System.out.println("[AfterClass] - runs once after all methods in the current class");

    }

    @AfterTest
    public void afterTest() {

        System.out.println("[AfterTest] - runs after all @Test methods belonging to <test> tag");

    }

    @AfterSuite
    public void afterSuite() {

        System.out.println("[AfterSuite] - runs once after all tests in the suite");

    }

    /*

      Optional: annotation for group-level demonstration

      @BeforeGroups("regression")
      public void beforeGroups() {

          System.out.println("[BeforeGroups] - runs before first test in 'regression' group");

      }

      @AfterGroups("regression")
      public void afterGroups() {

          System.out.println("[AfterGroups] - runs after last test in 'regression' group");

      }

      @Test(groups = "regression")
      public void regressionTest() {

          System.out.println("[Test group=regression] - regressionTest execution");

      }

    */

}

/*

📄
testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="AnnotationHierarchySuite">

  <test name="HierarchyTest">

    <classes>

      <class name="com.example.tests.AnnotationHierarchyExample"/>

    </classes>

  </test>

</suite>

*/

[BeforeSuite]  - runs once before all tests in the suite
[BeforeTest]   - runs before any @Test belonging to <test> tag
[BeforeClass]  - runs once before the first method in the current class
[BeforeMethod] - runs before each @Test method
[Test]         - testA execution
[AfterMethod]  - runs after each @Test method
[BeforeMethod] - runs before each @Test method
[Test]         - testB execution
[AfterMethod]  - runs after each @Test method
[AfterClass]   - runs once after all methods in the current class
[AfterTest]    - runs after all @Test methods belonging to <test> tag
[AfterSuite]   - runs once after all tests in the suite