TestNG is the test framework behind most Java Selenium and REST Assured projects. It runs your tests, decides the order, groups them into suites, feeds them data, runs them in parallel and produces reports. This tutorial covers what TestNG is, why teams choose it, how to add it to a Maven project, your first test class, and the exact order in which TestNG runs everything. At the end there's a step-by-step path through the rest of TestNG.
What is TestNG?
TestNG (Test Next Generation) is a powerful open-source testing framework for Java, inspired by JUnit and NUnit but designed to be more flexible and powerful. It was created by Cédric Beust and is widely used in enterprise Selenium automation, API testing, and unit testing.
TestNG = Testing, Next Generation. Created by Cédric Beust. Maintained as open-source.
Works seamlessly with Maven, Gradle, Jenkins, and all major IDEs (IntelliJ IDEA, Eclipse).
Why TestNG? — Problems it Solves
| Problem Without TestNG | Solution TestNG Provides |
|---|---|
| No built-in parallel test execution | Run tests in parallel across threads, classes, methods |
| No data-driven testing out of the box | @DataProvider — feed multiple data sets to one test |
| No grouping of tests | @Test(groups) — tag tests as smoke, regression, sanity |
| Cannot pass parameters from XML to test | @Parameters — inject values from testng.xml |
| No dependency between test methods | @Test(dependsOnMethods) — enforce test order |
| No built-in retry of failed tests | IRetryAnalyzer — auto retry flaky tests |
| Verbose setup code in JUnit 3/4 | Clean annotations — @BeforeMethod, @AfterMethod etc. |
| Cannot run a subset of tests easily | testng.xml — include/exclude classes, methods, groups |
| Limited reporting | Built-in HTML + XML reports + Extent/Allure integration |
Maven Setup: pom.xml
<!-- TestNG dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version> <!-- check Maven Central for the latest 7.x -->
<scope>test</scope>
</dependency>
<!-- Maven Surefire Plugin — runs TestNG via Maven -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
The Surefire plugin is what runs TestNG when you type mvn test; pointing it at testng.xml lets you decide which tests run without changing code.
Your First TestNG Test
import org.testng.Assert;
import org.testng.annotations.*;
public class FirstTestNGTest {
@BeforeSuite
public void beforeSuite() {
System.out.println("BEFORE SUITE — runs once before all tests");
}
@BeforeClass
public void beforeClass() {
System.out.println("BEFORE CLASS — runs once before this class");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("BEFORE METHOD — runs before each @Test");
}
@Test
public void testLogin() {
System.out.println("TEST — Login");
Assert.assertEquals(2 + 2, 4, "Math should work");
}
@Test
public void testDashboard() {
System.out.println("TEST — Dashboard");
Assert.assertTrue(true, "Should be true");
}
@AfterMethod
public void afterMethod() {
System.out.println("AFTER METHOD — runs after each @Test");
}
@AfterClass
public void afterClass() {
System.out.println("AFTER CLASS — runs once after this class");
}
@AfterSuite
public void afterSuite() {
System.out.println("AFTER SUITE — runs once after all tests");
}
}
Run it from the IDE (right-click → Run) or with mvn test. The console output shows each annotation firing in order, which is the quickest way to understand the lifecycle.
TestNG Execution Order
Execution order for 2 test classes (ClassA and ClassB) each with 2 @Test methods:
@BeforeSuite (once — very first)
@BeforeTest (once per <test> tag in testng.xml)
--- ClassA ---
@BeforeClass (once for ClassA)
@BeforeMethod → @Test(testOne) → @AfterMethod
@BeforeMethod → @Test(testTwo) → @AfterMethod
@AfterClass (once for ClassA)
--- ClassB ---
@BeforeClass (once for ClassB)
@BeforeMethod → @Test(testOne) → @AfterMethod
@BeforeMethod → @Test(testTwo) → @AfterMethod
@AfterClass (once for ClassB)
@AfterTest (once per <test> tag)
@AfterSuite (once — very last)
Learn TestNG Step by Step
- Annotations, priority, DataProvider and testng.xml: TestNG annotations, priority and DataProvider
- Assertions: hard vs soft asserts in assertions in Java and TestNG
- Parallel execution: parallel runs, ThreadLocal and Grid
- Listeners, retries and reports: TestNG listeners and RetryAnalyzer
- Advanced features: @Factory, invocationCount and execution control
- Running it in CI and doing it well: TestNG best practices, Jenkins and GitHub Actions
- Revision: the TestNG cheat sheet and TestNG interview questions
FAQs
What is TestNG used for?
TestNG is a Java test framework used to organize and run automated tests: unit, API (REST Assured) and UI (Selenium). It handles setup and teardown, grouping, data-driven tests, parallel execution, dependencies, retries and reports.
What is the full form of TestNG?
TestNG stands for Test Next Generation. It was created by Cédric Beust, inspired by JUnit and NUnit.
How do I run TestNG tests?
From the IDE with the TestNG plugin (built into IntelliJ IDEA; available for Eclipse), or with Maven using mvn test, which runs the suites listed in the Surefire plugin configuration.
Is TestNG better than JUnit?
For Selenium suites, TestNG's built-in groups, testng.xml, parallel settings and DataProviders make it the common choice; JUnit 5 is more common for developer unit tests. See TestNG vs JUnit 5.