Wednesday, November 12, 2025

thumbnail

Introduction to TestNG Annotations

 🧩 What is TestNG?


TestNG stands for Test Next Generation — it’s a testing framework for Java inspired by JUnit but with more powerful and flexible features.


It’s widely used with Selenium WebDriver for test automation because it supports:


Test configuration with annotations


Parallel execution


Grouping and prioritizing tests


Data-driven testing


HTML reports


🧠 Why Annotations?


In TestNG, annotations tell the framework how and when to run specific methods during the test lifecycle.

Instead of relying on method names (like testLogin), you simply annotate methods with tags like @Test, @BeforeMethod, @AfterClass, etc.


⚙️ Common TestNG Annotations (with Order of Execution)

Annotation Description Runs

@BeforeSuite Runs once before all tests in the suite Before everything

@BeforeTest Runs before any <test> tag in testng.xml Before all classes in a test

@BeforeClass Runs before the first method in the current class Before first test method in the class

@BeforeMethod Runs before each @Test method Before every test

@Test Marks a test case Actual test logic

@AfterMethod Runs after each @Test method After every test

@AfterClass Runs after all test methods in a class After all tests in the class

@AfterTest Runs after all classes in a test After <test> tag

@AfterSuite Runs once after all tests in the suite At the very end

🧪 Example: Basic TestNG Script

import org.testng.annotations.*;


public class TestNGExample {


    @BeforeSuite

    public void beforeSuite() {

        System.out.println(">> Before Suite");

    }


    @BeforeTest

    public void beforeTest() {

        System.out.println(">> Before Test");

    }


    @BeforeClass

    public void beforeClass() {

        System.out.println(">> Before Class");

    }


    @BeforeMethod

    public void beforeMethod() {

        System.out.println(">> Before Method");

    }


    @Test

    public void testLogin() {

        System.out.println("Executing Test: Login");

    }


    @Test

    public void testLogout() {

        System.out.println("Executing Test: Logout");

    }


    @AfterMethod

    public void afterMethod() {

        System.out.println(">> After Method");

    }


    @AfterClass

    public void afterClass() {

        System.out.println(">> After Class");

    }


    @AfterTest

    public void afterTest() {

        System.out.println(">> After Test");

    }


    @AfterSuite

    public void afterSuite() {

        System.out.println(">> After Suite");

    }

}


🧩 Output (Execution Flow)

>> Before Suite

>> Before Test

>> Before Class

>> Before Method

Executing Test: Login

>> After Method

>> Before Method

Executing Test: Logout

>> After Method

>> After Class

>> After Test

>> After Suite


🧭 Key Annotations Explained

🧪 @Test


Marks a method as a test case.


@Test

public void verifyLogin() {

    System.out.println("Login test executed");

}



You can also control execution with parameters:


@Test(priority = 1, description = "Verifies login with valid credentials")

public void loginTest() { ... }


@Test(priority = 2, dependsOnMethods = "loginTest")

public void logoutTest() { ... }


⚙️ @BeforeMethod / @AfterMethod


Used for setup and teardown for each test method.


@BeforeMethod

public void setUp() {

    // Launch browser, open URL

}


@AfterMethod

public void tearDown() {

    // Close browser

}


🧱 @BeforeClass / @AfterClass


Runs once before/after all tests in a class (useful for class-level setup).


@BeforeClass

public void init() {

    System.out.println("Runs before any @Test method in this class");

}


🧩 @BeforeSuite / @AfterSuite


Runs once before/after the entire test suite (good for setting up reports, database connections, etc.).


🧠 Example: Selenium with TestNG

import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.*;


public class GoogleTest {

    WebDriver driver;


    @BeforeMethod

    public void setUp() {

        driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.get("https://www.google.com");

    }


    @Test

    public void verifyTitle() {

        String title = driver.getTitle();

        System.out.println("Page Title: " + title);

        assert title.contains("Google");

    }


    @AfterMethod

    public void tearDown() {

        driver.quit();

    }

}


🧩 TestNG XML Example


You can control which tests run using a testng.xml file:


<suite name="MySuite">

  <test name="SmokeTests">

    <classes>

      <class name="tests.GoogleTest"/>

      <class name="tests.LoginTest"/>

    </classes>

  </test>

</suite>



Run using:


testng testng.xml


✅ Summary

Annotation When It Runs Typical Use

@BeforeSuite Once before all tests Setup global config (reports, DB)

@BeforeTest Before <test> in XML Setup environment

@BeforeClass Before all tests in class Class-level setup

@BeforeMethod Before each test Launch browser, open page

@Test Actual test method Test logic

@AfterMethod After each test Close browser, cleanup

@AfterClass After all tests in class Close connections

@AfterSuite Once after all tests Generate reports

Learn Selenium with JAVA Training in Hyderabad

Read More

How to Use Assertions in Selenium Tests

Working with Links and Buttons in Selenium

Handling Checkboxes and Radio Buttons in Selenium

How to Verify Page Title and URL with Selenium

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive