Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Can you describe the major features of TestNG?

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.

TestNG Features at a Glance

Here is a quick map of the major features and what each one does before we walk through them in detail.

FeatureWhat it does
AnnotationsMark test and setup/teardown methods and control the suite, test, class, group, and method lifecycle.
AssertionsValidate results with fail-fast hard assertions or failure-collecting SoftAssert.
GroupsTag tests (smoke, regression) and include or exclude them at run time.
ParameterizationInject values via @Parameters and testng.xml, or drive tests with rows from @DataProvider.
DependenciesSequence tests with dependsOnMethods and dependsOnGroups; skip dependents when a prerequisite fails.
Parallel executionRun methods, classes, tests, or instances concurrently via parallel and thread-count.
Listeners & reportingHook into events with ITestListener/IReporter and emit HTML and XML reports.
testng.xmlDefine suites, tests, classes, methods, groups, listeners, and parameters declaratively.
ResilienceSet timeouts, assert expected exceptions, and auto-retry flaky tests via IRetryAnalyzer.

Rich Annotations and Lifecycle Control

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:

  • @BeforeSuite / @AfterSuite: run once before and after the entire suite, ideal for global setup such as starting a server or driver pool.
  • @BeforeTest / @AfterTest: run around each <test> tag declared in testng.xml.
  • @BeforeGroups / @AfterGroups: run before the first and after the last method of a named group.
  • @BeforeClass / @AfterClass: run once per test class.
  • @BeforeMethod / @AfterMethod: run before and after every test method, perfect for fresh state such as a clean browser session.

Other annotations such as @DataProvider, @Factory, @Parameters, and @Listeners extend this model into data-driven, dynamic, and event-driven testing.

Flexible Assertions: Hard and Soft

TestNG ships with two assertion styles so you can choose how a test reacts to a failed check:

  • Hard assertions: the static Assert class (assertEquals, assertTrue, assertNotNull, and so on) throws immediately on the first failure, halting the rest of the method.
  • Soft assertions: a SoftAssert instance records each failed check and only raises them when you call assertAll(), letting a single run surface every problem at once instead of stopping at the first.

Grouping Tests

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.

Parameterization and Data-Driven Testing

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
    }
}

Dependencies, Priority, and Enabled Flag

TestNG gives you precise control over execution order and inclusion:

  • dependsOnMethods / dependsOnGroups: a test runs only after its prerequisites pass; if a prerequisite fails, the dependent test is skipped rather than failed, keeping reports honest.
  • priority: orders methods within a class, with lower numbers running first, so you can stage a flow without writing explicit dependencies.
  • enabled = false: temporarily disables a test without deleting it, useful for quarantining a broken or in-progress case.

Parallel Execution and testng.xml

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.

Listeners, Reporting, and Resilience

The final group of features makes a suite observable and robust:

  • Listeners: interfaces such as ITestListener, ISuiteListener, IReporter, and IAnnotationTransformer let you hook into start, success, failure, and skip events to log, screenshot, or customize behavior.
  • Reporting: TestNG generates HTML reports plus a machine-readable testng-results.xml out of the box, and an emailable report for quick sharing.
  • Timeouts: @Test(timeOut = ...) fails a method that runs longer than the allowed milliseconds, catching hangs.
  • Expected exceptions: @Test(expectedExceptions = SomeException.class) asserts that a method throws a specific exception, the clean way to test error paths.
  • Retry analyzer: implementing IRetryAnalyzer lets TestNG automatically re-run a failing test a set number of times, reducing noise from genuinely flaky cases.

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?.

Frequently Asked Questions

What are the major features of TestNG?

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.

What is the difference between hard and soft assertions in TestNG?

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.

How does TestNG run tests in parallel?

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.

What is the @DataProvider annotation used for?

@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.

What does dependsOnMethods do 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.

Is TestNG only for unit testing?

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.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

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

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests