TestNG Overview (original) (raw)

Last Updated : 20 May, 2026

TestNG (Test Next Generation) is a powerful testing framework used for automating tests in Java applications. It provides advanced features for organizing, executing, and managing test cases efficiently.

TestNG Execution Workflow

This diagram illustrates how TestNG executes test cases and generates different types of output reports.

test_cases

TestNG Execution Workflow

TestNG XML Configuration

TestNG allows you to configure and execute test cases using an XML file (testng.xml). It helps define test suites, organize test classes, and control execution flow efficiently.

TestNG XML File (testng.xml)

The XML file defines which test classes should be executed and how they are organized in the test suite.

XML `

<!-- Test for TestClass2 -->
<test name="Test 2">
    <classes>
        <class name="GeneralXML.TestClass2" />
    </classes>
</test>

`

Creating Test Classes

First, create Java test classes that will be executed using the TestNG XML configuration.
These classes contain test methods annotated with @Test that define the test logic and will be executed by TestNG.

**TestClass1.java

Java `

package GeneralXML;

import org.testng.Assert; import org.testng.annotations.Test;

public class TestClass1 {

@Test
public void testMethod1() {
    System.out.println("Executing Test Method 1 in TestClass1");
    // Sample test condition
    Assert.assertTrue(true, "Test Method 1 passed");
}

@Test
public void testMethod2() {
    System.out.println("Executing Test Method 2 in TestClass1");
    // Sample test condition
    Assert.assertEquals(2 + 2, 4, "Test Method 2 passed");
}

}

`

**TestClass2.java

C++ `

package GeneralXML;

import org.testng.Assert; import org.testng.annotations.Test;

public class TestClass2 {

@Test
public void testMethod3() {
    System.out.println("Executing Test Method 3 in TestClass2");
    Assert.assertNotNull("Hello", "Test Method 3 passed");
}

@Test
public void testMethod4() {
    System.out.println("Executing Test Method 4 in TestClass2");
    Assert.assertEquals(3 * 3, 9, "Test Method 4 passed");
}

}

`

Running TestNG Tests

TestNG tests can be executed using different methods based on your setup.

testng-Suit-output

TestNG Suit Output

TestNG with Selenium

TestNG is widely used with Selenium to build scalable and maintainable automation frameworks. Selenium handles browser automation, while TestNG manages test execution, reporting, grouping, and parallel testing.

TestNG Features and Use Cases

TestNG provides powerful features that help in designing flexible, scalable, and maintainable test automation frameworks.

1. Assertions

Assertions are used to validate the expected outcome of test cases.

**Example:

Java `

@Test public void testAddition() { int result = Calculator.add(2, 3); Assert.assertEquals(result, 5, "Addition result is incorrect!"); }

`

2. Prioritizing Tests

Although TestNG supports test prioritization, it is recommended to keep test cases independent whenever possible. Excessive dependency on priorities can make test suites harder to maintain and may lead to unstable execution in large automation frameworks.

**Example:

Java `

@Test(priority = 1) public void loginTest() { System.out.println("Login Test"); }

@Test(priority = 2) public void dashboardTest() { System.out.println("Dashboard Test"); }

`

3. Grouping Tests

Tests can be grouped for selective execution (e.g., smoke, regression).

**Example:

Java `

@Test(groups = {"smoke"}) public void smokeTest() { System.out.println("Executing Smoke Test"); }

@Test(groups = {"regression"}) public void regressionTest() { System.out.println("Executing Regression Test"); }

`

4. Parameterization

TestNG supports running tests with multiple data sets using @Parameters and @DataProvider.

**Using testng.xml:

Java `

@Test @Parameters({"username", "password"}) public void loginTest(String username, String password) { System.out.println("Logging in with " + username + " and " + password); }

`

**Using @DataProvider:

Java `

@DataProvider(name = "loginData") public Object[][] dataProviderMethod() { return new Object[][] {{"user1", "pass1"}, {"user2", "pass2"}}; }

@Test(dataProvider = "loginData") public void loginTest(String username, String password) { System.out.println("Logging in with " + username + " and " + password); }

`

5. Listeners

Listeners are used to track test execution events such as pass, fail, or skip.

Java `

public class TestListener implements ITestListener { @Override public void onTestSuccess(ITestResult result) { System.out.println(result.getName() + " passed successfully."); } }

`

6. Annotations

Annotations define the lifecycle and execution flow of test cases.

**Example:

Java `

public class TestExample { @BeforeClass public void setUp() { System.out.println("Setting up the test class."); }

@Test
public void test1() {
    System.out.println("Executing Test 1");
}

@Test
public void test2() {
    System.out.println("Executing Test 2");
}

@AfterClass
public void tearDown() {
    System.out.println("Cleaning up after tests.");
}

}

`

Advantages of TestNG

TestNG offers several benefits that make it a preferred framework for test automation.

Limitations of TestNG

Although TestNG provides powerful features for test automation, it also has certain limitations that should be considered while designing automation frameworks.

Best Practices in TestNG

Following best practices helps create reliable, maintainable, and scalable test automation suites.