What is TestNG?
TestNG (Next Generation) is a powerful testing framework for Java, inspired by JUnit and NUnit. It is designed to cover all categories of tests — unit, functional, end-to-end, and integration tests — and integrates seamlessly with Selenium.
Key capabilities:
- ✓Assertions – Validate expected vs actual results (
Assert.assertEquals,Assert.assertTrue) - ✓Reporting – Generates HTML and XML test reports automatically
- ✓Parallel Execution – Run tests simultaneously across threads or browsers
- ✓Data-Driven Testing –
@DataProviderfeeds multiple data sets into one test - ✓Grouping – Tag tests into groups (sanity, regression) and selectively run them
- ✓Dependency – Control execution order with
dependsOnMethods - ✓Parameterization – Pass external values via
testng.xmlusing@Parameters
Minimal example:
Java
import org.testng.annotations.Test;
import org.testng.Assert;
public class LoginTest {
@Test
public void verifyLogin() {
String actual = "Welcome";
Assert.assertEquals(actual, "Welcome", "Login failed!");
}
}
Why TestNG over JUnit?
| Feature | TestNG | JUnit |
|---|---|---|
| Parallel execution | Built-in | Limited |
| Data provider | @DataProvider | @ParameterizedTest |
| Grouping | Yes | Categories |
| Dependencies | Yes | No |
| Suite configuration | testng.xml | Suites runner |
| Reporting | Built-in HTML | Requires plugin |
TestNG is the most popular choice for Selenium automation projects in Java.
