Getting Started with TestNG Framework for Selenium Automation
Automation testing with Selenium is a popular approach for verifying web applications, but when it comes to structuring test cases, generating reports, or managing test execution, Selenium alone isn't enough. This is where TestNG, a powerful testing framework inspired by JUnit and NUnit, becomes invaluable.
In this blog, we'll explore the essentials of TestNG and how to integrate it with Selenium to build efficient, scalable, and maintainable automation test suites.
Overview of TestNG Framework
What is TestNG?
Why Use TestNG with Selenium?
Setting Up TestNG with Selenium
Organizing Tests with TestNG
Reporting and Logging
What is TestNG?
TestNG (Test Next Generation) is a testing framework designed to cover all types of test scenarios: unit, functional, integration, and end-to-end. It provides powerful features such as:
Annotations for structuring tests
Test prioritization and grouping
Parameterization
Parallel test execution
Built-in HTML and XML reporting
Why Use TestNG with Selenium?
Combining Selenium with TestNG brings structure and flexibility to your test scripts. Here’s why this combination is so effective:
Feature | Selenium | Selenium + TestNG |
Test structure | Manual | Organized using annotations |
Reporting | None | Built-in HTML reports |
Test execution | Linear | Grouped, prioritized, and parallel |
Data-driven testing | Manual | Easy with @DataProvider |
Setting Up TestNG with Selenium
1. Add TestNG to Your Project
If you're using Maven, add this dependency to your pom.xml:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
Or if you're using Eclipse/IntelliJ, install TestNG via plugin or project libraries.
2.Sample TestNG Test with Selenium
import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;public class GoogleSearchTest { WebDriver driver; @BeforeClass public void setup() { System.setProperty("webdriver. chrome. driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testGoogleHomePage() { driver.get("https://www.google.com"); assert driver.getTitle().contains("Google"); } @AfterClass public void teardown() { driver.quit(); }}3. Running the Test
· Right-click → Run as → TestNG Test (in IDE)
· Using a testng.xml suite file
· Via Maven command: mvn test
TestNG Annotations Overview
Here are some key annotations you'll use:
Annotation | Description |
@Test | Marks a method as a test case |
@BeforeClass / @AfterClass | Run once before/after all tests in the class |
@BeforeMethod / @AfterMethod | Run before/after each test method |
@DataProvider | Supplies test data |
@Parameters | Supports parameterized tests via XML |
Organizing Tests with TestNG
You can group and prioritize tests easily:
@Test (priority = 1, groups = {"smoke"})
public void loginTest () {...}
@Test (priority = 2, groups = {"regression"}
public void searchTest () {...}
And run specific groups via your testng.xml:
<test name="Smoke Tests">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
</test>
Reporting and Logging
TestNG automatically generates:
1.test-output/index.html — test results in HTML format
2.test-output/emailable-report.html — a concise, shareable report
You can also integrate ExtentReports or Allure for more detailed, interactive reports.
Parallel Execution
Speed up your test execution using:
Xml:
<suite name="Parallel Suite" parallel="tests" thread-count="3">
Or annotate with:
Java:
@Test (threadPoolSize = 3, invocationCount = 10)
Conclusion
TestNG significantly enhances Selenium automation by enabling:
· Clean test structuring
· Grouping and prioritization
· Parameterization and parallelism
· Professional reporting
Whether you’re building a CI/CD pipeline or running local tests, TestNG helps you scale automation quickly and cleanly.


