What is TestNG
Overview
TestNG is an open-source testing framework for Java, built to overcome limitations of older frameworks like JUnit, with richer annotations and configuration options.
Core Features
- Annotation-based test lifecycle control (setup, execution, teardown).
- Support for grouping related tests together.
- A configuration file (
testng.xml) to control which tests run and how. - Built-in assertions and detailed pass/fail reporting.
Typical Use
Testers write test methods annotated with @Test, organize them into suites via testng.xml, and run the suite to get a structured report of results.
Annotations and the Order They Run In
TestNG decides when each method runs based on its annotation. Knowing the order helps you place setup and cleanup code at the right level:
@BeforeSuite: once, before anything in the suite, for example starting a report.@BeforeTest: before each<test>block intestng.xml.@BeforeClass: once per test class, for example launching a browser.@BeforeMethod: before every single test method, for example opening the login page.@Test: the test itself.@AfterMethod,@AfterClass,@AfterTestand@AfterSuitemirror the setup steps in reverse.
A test class using groups, priorities and a data provider
import org.testng.Assert;
import org.testng.annotations.*;
public class LoginTest {
@BeforeClass
public void openBrowser() {
System.out.println("Launch browser");
}
@Test(priority = 1, groups = "smoke")
public void validLoginShowsDashboard() {
String title = login("admin", "correct-password");
Assert.assertEquals(title, "Dashboard", "Page title after login");
}
@Test(priority = 2, groups = "regression", dataProvider = "badLogins")
public void invalidLoginShowsError(String user, String pass) {
String title = login(user, pass);
Assert.assertEquals(title, "Login", "User should stay on the login page");
}
@DataProvider(name = "badLogins")
public Object[][] badLogins() {
return new Object[][] {
{"admin", "wrong"},
{"", "secret"},
{"unknown", "admin"}
};
}
@AfterClass
public void closeBrowser() {
System.out.println("Close browser");
}
private String login(String user, String pass) {
// In a real project this would drive the UI through a page object
return "admin".equals(user) && "correct-password".equals(pass) ? "Dashboard" : "Login";
}
}
The data provider feeds three rows into one test method, so TestNG reports three separate results. Adding a new invalid case is just one more line of data.
Controlling Runs with testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Web Suite" parallel="classes" thread-count="2">
<test name="Smoke checks">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="tests.LoginTest"/>
<class name="tests.SearchTest"/>
</classes>
</test>
</suite>
This file runs only tests in the smoke group and runs two classes at the same time. Switching the include to regression gives a full run without touching any Java code. With Maven, the Surefire plugin can point to this file so mvn test runs the same suite on a CI server.
Hard Asserts, Soft Asserts and Common Pitfalls
A normal Assert stops the test at the first failure. A SoftAssert records failures and keeps going, which is handy for checking many fields on one page. You must call softAssert.assertAll() at the end; without it, the test passes even when checks failed.
- Using priority to express dependency. Priority only sets order. If test B needs test A to succeed, use
dependsOnMethodsso B is skipped, not failed, when A breaks. - Sharing one WebDriver in parallel runs. Threads will fight over the same browser. Keep a driver per thread, often with
ThreadLocal. - Vague assertion messages. Add the third message argument so reports explain what was expected.
- Heavy setup in @BeforeMethod. Logging in before every test is slow; move shared setup to class level when tests do not change that state.
- Ignoring the reports. TestNG writes HTML and XML results into the output folder after each run. Check failed and skipped counts there, and rerun only the failures using the generated
testng-failed.xmlfile.
JUnit 5 now covers many of the same needs. TestNG remains popular in Selenium projects because of its suite XML, groups, dependencies and built-in parallel options.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.