Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

TestNG Framework

  • What is TestNG Framework?
  • Advantages and Disadvantages of TestNG:
  • Difference between TestNG And Junit framework
  • What is TestNG Annotations?
  • Install TestNG In Eclipse & IntelliJ?
  •  Hierarchy In TestNG Annotations
  • TestNG’s prioritization
  • TestNG Dependent
  • Reporter Class in TestNG
  • TestNG Reports
  • Assertions in TestNG
  • TestNG Groups
  • TestNG Parameters
  •  Cross-Browser Testing in TestNG
  •  Parallel Testing in TestNG
  • Data Providers
  • TestNG Listeners
  • Rerunning failed tests in TestNG
View Categories
  • Home
  • Tutorials
  • TestNG
  • TestNG Framework
  •  Hierarchy In TestNG Annotations

 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.

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:

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

[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
testng framework
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
Install TestNG In Eclipse & IntelliJ?TestNG’s prioritization
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105