---
title:  Hierarchy In TestNG Annotations
description: "** Hierarchy In TestNG Annotations** **The full hierarchy of TestNG annotations—from suite-level to method-level:** 1. *@BeforeSuite / @AfterSuite* – once before/after the entire suite 2. *@BeforeTest / @AfterTest*– before/after each in your XML 3. *@BeforeClass / @AfterClass*– once before/after all methods…"
url: "https://vinothqaacademy.com/docs/hierarchy-in-testng-annotations/"
updated: "2025-09-13"
category: TestNG Framework
tags:
  - Testng Framework
---

#  Hierarchy In TestNG Annotations

** Hierarchy In TestNG Annotations**

**The full hierarchy of TestNG annotations—from suite-level to method-level:**

1. *@BeforeSuite / @AfterSuite* – once before/after the entire suite

2. *@BeforeTest / @AfterTest*– before/after each <test> in your XML

3. *@BeforeClass / @AfterClass*– once before/after all methods in the class

*4. @BeforeMethod / @AfterMethod* – before/after each individual @Test method

Run it via the included testng.xml snippet to see the execution order printed in the console.

```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");

      }

    */

}

/*
```

**Sample testng.xml to execute this class:**

```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>

*/
```

**Execution Result**

```html

                [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
```
