Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

TestNG is a Java testing framework whose major features are its rich lifecycle annotations, hard and soft assertions, test grouping, parameterization and data-driven testing, method and group dependencies, test priorities, parallel execution driven by testng.xml, listeners and reporting, per-method timeouts, expected exceptions, and an automatic retry analyzer. Together these capabilities let you organize, parameterize, sequence, and scale a Java automation suite from a single declarative configuration.
Here is a quick map of the major features and what each one does before we walk through them in detail.
| Feature | What it does |
|---|---|
| Annotations | Mark test and setup/teardown methods and control the suite, test, class, group, and method lifecycle. |
| Assertions | Validate results with fail-fast hard assertions or failure-collecting SoftAssert. |
| Groups | Tag tests (smoke, regression) and include or exclude them at run time. |
| Parameterization | Inject values via @Parameters and testng.xml, or drive tests with rows from @DataProvider. |
| Dependencies | Sequence tests with dependsOnMethods and dependsOnGroups; skip dependents when a prerequisite fails. |
| Parallel execution | Run methods, classes, tests, or instances concurrently via parallel and thread-count. |
| Listeners & reporting | Hook into events with ITestListener/IReporter and emit HTML and XML reports. |
| testng.xml | Define suites, tests, classes, methods, groups, listeners, and parameters declaratively. |
| Resilience | Set timeouts, assert expected exceptions, and auto-retry flaky tests via IRetryAnalyzer. |
Annotations are the backbone of TestNG. The @Test annotation marks a method as a test, while a layered set of configuration annotations runs setup and teardown logic at the right scope:
Other annotations such as @DataProvider, @Factory, @Parameters, and @Listeners extend this model into data-driven, dynamic, and event-driven testing.
TestNG ships with two assertion styles so you can choose how a test reacts to a failed check:
The groups attribute on @Test lets you tag methods, even across different classes, with labels such as smoke, regression, or sanity. At run time you include or exclude groups from testng.xml, so the same code base can power a fast smoke run on every commit and a full regression run nightly without touching the tests themselves.
TestNG separates parameter injection from the test logic in two complementary ways. @Parameters reads static values supplied through testng.xml, which is handy for things like a base URL or browser name. @DataProvider feeds a method many rows of data, returning an Object[][] so the same test runs once per row, the standard way to do data-driven testing in TestNG.
import org.testng.Assert;
import org.testng.annotations.*;
public class LoginTest {
@BeforeMethod
public void setUp() {
// initialize the WebDriver / state here
}
@Test(priority = 1, groups = {"smoke"})
public void validLogin() {
Assert.assertTrue(true, "Login should succeed");
}
@Test(dependsOnMethods = {"validLogin"}, timeOut = 5000)
public void dashboardLoads() {
// runs only if validLogin passed, fails if it exceeds 5s
}
@DataProvider(name = "creds")
public Object[][] creds() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"}
};
}
@Test(dataProvider = "creds")
public void loginWithData(String user, String pass) {
// executes once for each row supplied above
}
}TestNG gives you precise control over execution order and inclusion:
The testng.xml suite file is the control center of the framework. It declares which suites, tests, classes, methods, and groups run, wires in listeners and parameters, and turns on parallel execution. Setting parallel to methods, classes, tests, or instances together with a thread-count spreads work across threads and cuts total run time.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite" parallel="methods" thread-count="4">
<test name="SmokeTests">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="com.example.LoginTest"/>
</classes>
</test>
</suite>Parallelism multiplies in value when paired with broad browser and OS coverage. You can run the same parallel Selenium Automation Testing with TestNG across a cloud grid of real browsers and operating systems, so a four-thread run validates several environments at once instead of one local browser at a time.
The final group of features makes a suite observable and robust:
TestNG also integrates cleanly with Maven Surefire, Gradle, Selenium WebDriver, and CI pipelines, and it is designed to cover unit, functional, integration, and end-to-end testing, not just isolated unit tests. Once you understand the features, the next practical step is knowing the different List Out Various Ways in Which TestNG Can Be Invoked?.
TestNG's major features are its rich lifecycle annotations, hard and soft assertions, test grouping, parameterization with @Parameters and data-driven testing with @DataProvider, method and group dependencies, priorities, parallel execution configured through testng.xml, listeners and reporting, timeouts, expected exceptions, and a retry analyzer for flaky tests.
A hard assertion using the Assert class stops the test method immediately on the first failed check. A SoftAssert collects every failure and only reports them when you call assertAll(), so the test continues and you see all failing conditions in a single run instead of one at a time.
TestNG runs tests in parallel through the parallel attribute in testng.xml, set to methods, classes, tests, or instances, combined with a thread-count value. You can also control concurrency per method with threadPoolSize and invocationCount on the @Test annotation.
@DataProvider supplies a test method with multiple sets of input data. The provider method returns an Object[][] (or an iterator), and TestNG runs the bound @Test once for each row, which is how data-driven testing is implemented in TestNG.
dependsOnMethods makes a test run only after its named prerequisite methods have passed. If a prerequisite fails, the dependent test is skipped rather than failed, which keeps reports clean by separating genuine failures from tests that never had a chance to run.
No. TestNG is designed to cover unit, functional, integration, and end-to-end testing. It integrates with Selenium WebDriver, Maven Surefire, Gradle, and CI pipelines, which makes it a common choice for full UI and API automation suites, not just isolated unit tests.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance