---Advertisement---

TestNG Tutorial: What Are Annotations & Framework in Selenium? Best 2025

By Manisha

Updated On:

---Advertisement---
TestNG Tutorial

TestNG Tutorial: TestNG Tutorial – Annotations & Framework in Selenium: TestNG (Test Next Generation) is a powerful automation testing framework inspired by JUnit. It provides advanced functionalities like parallel execution, test grouping, and detailed reporting, making it a preferred choice for Selenium testers.

In this tutorial, you will learn:
What is TestNG?
How to use TestNG in Selenium?
Annotations in TestNG
Advantages of TestNG over JUnit
How to create and run TestNG test cases


TestNG Tutorial: TestNG is a testing framework designed to overcome JUnit’s limitations. It provides:
Better test reporting
Parallel execution of test cases
Support for data-driven testing
Grouping and prioritizing test cases

Unlike traditional Java programs that use the main() method, TestNG allows structured testing with annotations.

Example: Why Use TestNG?

TestNG Tutorial: Imagine you have five test cases. If one fails, you may want to rerun only the failed test. In JUnit, you must rerun all test cases. But with TestNG, you can run only failed tests using the testng-failed.xml file.


Key Features of TestNG in Selenium:

Detailed Test Reports – View test case execution details, including passed, failed, and skipped tests.
Grouping Test Cases – Organize multiple test cases in testng.xml.
Parallel Execution – Run multiple tests without loops using invocationCount.
Cross-Browser Testing – Execute tests on different browsers.
Integration with Tools – Works with Maven, Jenkins, TestNG Maven, etc.


1️⃣ TestNG Tutorial: Easier to Use: Annotations like @BeforeMethod, @AfterMethod are simpler and more intuitive.
2️⃣ Flexible Test Execution: Run selected test cases instead of all.
3️⃣ Parallel Testing: Execute tests simultaneously on multiple browsers.


TestNG Tutorial: Annotations in TestNG help control the execution flow of test methods. They start with the @ symbol and define when and how a test method runs.

Common TestNG Annotations:

AnnotationDescription
@BeforeSuiteRuns before all tests in the suite
@AfterSuiteRuns after all tests in the suite
@BeforeTestRuns before any test method in the class
@AfterTestRuns after all test methods in the class
@BeforeClassRuns before the first test method in the current class
@AfterClassRuns after all test methods in the current class
@BeforeMethodRuns before each test method
@AfterMethodRuns after each test method
@TestDefines a test case

TestNG Tutorial: Follow these steps to create and run TestNG test cases in Selenium.

Step 1: Set Up TestNG Project in Eclipse

1️⃣ Open Eclipse and create a new Java project.
2️⃣ Add TestNG Library:

  • Click on LibrariesAdd Library → Select TestNG → Click NextFinish.
    3️⃣ Add Selenium JAR files from selenium.dev.

1️⃣ Right-click on src → NewOther → Select TestNG Class → Click Next.
2️⃣ Enter the test class name (e.g., FirstTestNGFile) → Click Finish.
3️⃣ Eclipse will generate a TestNG template.


TestNG Tutorial: Below is a simple TestNG test case that verifies the homepage title of a website:

java

import org.testng.Assert;

import org.testng.annotations.Test;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class FirstTestNGFile {

    @Test

    public void verifyHomepageTitle() {

        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver”);

        WebDriver driver = new ChromeDriver();

        driver.get(“https://www.example.com”);

        String expectedTitle = “Example Domain”;

        String actualTitle = driver.getTitle();

        Assert.assertEquals(actualTitle, expectedTitle);

        driver.quit();

    }

}

Key Takeaways from the Code:

✔ @Test tells TestNG that the method is a test case.
✔ Assert.assertEquals() is used for validation.
✔ No need for main() method – TestNG handles execution.


1️⃣TestNG Tutorial: Right-click on the test file → Click Run As → TestNG Test.
2️⃣ View test results in Eclipse Console & TestNG Results Window.

✅ TestNG generates a detailed report showing passed, failed, and skipped test cases.


TestNG can create an HTML-based report for better visualization of test results.

Steps to View HTML Report:

1️⃣ Run the test case in Eclipse.
2️⃣ Refresh the project → Look for test-output folder.
3️⃣ Open index.html to view the test report in a web browser.


1. Running Multiple Test Cases in a Single File

TestNG allows multiple test methods in a single file.

java

@Test

public void testA() { System.out.println(“Test A”); }

@Test

public void testB() { System.out.println(“Test B”); }

TestNG executes them alphabetically unless specified using priority:

java

@Test(priority=1)

public void testB() { System.out.println(“Test B”); }

@Test(priority=2)

public void testA() { System.out.println(“Test A”); }


2. Running Tests in Parallel

Parallel testing improves execution speed.

Modify testng.xml:

xml

<suite name=”ParallelTests” parallel=”tests” thread-count=”2″>

    <test name=”Test1″>

        <classes>

            <class name=”TestPackage.TestClass1″/>

        </classes>

    </test>

    <test name=”Test2″>

        <classes>

            <class name=”TestPackage.TestClass2″/>

        </classes>

    </test>

</suite>


TestNG is a powerful framework that enhances Selenium testing with annotations, parallel execution, and advanced reporting.

Simplifies test execution with annotations
Supports cross-browser and parallel testing
Generates detailed test reports
Easier to group and prioritize test cases

Now that you understand TestNG, start writing your own Selenium TestNG scripts and optimize your automation testing!
TestNG in Eclipse for Selenium
Testing Selenium Download


Leave a Comment

Index