Open In App

TestNG Overview

Last Updated : 06 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TestNG (Test Next Generation) is a popular testing framework for Java applications that facilitates the efficient and organized execution of test cases. One of its key features is the ability to parameterize tests, allowing developers to run the same test method with different sets of data.

Features:

  • Annotations: Defines test lifecycle (e.g., @Test, @BeforeClass, @AfterClass), making test code more readable and manageable.
  • Flexible Test Configuration: Allows grouping tests, prioritizing methods, and running tests in parallel for better flexibility.
  • Parallel Test Execution: Enables parallel test execution to speed up the overall test process, especially for large test suites.
  • Data-Driven Testing: Supports @DataProvider for running the same test with different data sets.
  • Test Reporting: Automatically generates detailed test reports (HTML, XML) to help with test execution analysis and failure debugging.
  • Grouping Tests: Groups related tests with the @Test(groups = "groupName") annotation for logical test execution or selective exclusions.
  • Dependency Testing: Supports defining test dependencies, ensuring tests run in a specific order or only if others pass.

TestNG is a Java framework designed to facilitate software testing in Java. It runs tests in classes, creating corresponding test classes and processing them efficiently. As an advanced framework, TestNG overcomes the limitations found in JUnit, offering greater flexibility. It utilizes the same classes to execute all tests and manages threads to run procedures concurrently, which speeds up the overall testing process.

Advantages-of-TestNG-over-Junit
TestNG

TestNG XML Configuration

TestNG allows you to configure and run tests using an XML file (testng.xml). In this file, you can define your test suites, organize test groups, and specify the execution order of the tests. This provides flexibility in managing complex test scenarios and controlling how tests are executed.

Create first TestClass1 and TestClass2 Java files that correspond to the test classes in your 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

Java
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");
        // Sample test condition
        Assert.assertNotNull("Hello", "Test Method 3 passed");
    }

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

Here’s the corrected TestNG XML file, which is placed in the same package as your test classes (GeneralXML):

XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="General Test Suite">
    <!-- Test for TestClass1 -->
    <test name="Test 1">
        <classes>
            <class name="GeneralXML.TestClass1" />
        </classes>
    </test>

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


TestNG tests can be run in several ways:

1. From IDE: Right-click the test class or XML file in your IDE (e.g., Eclipse or IntelliJ) and select Run As > TestNG Test.

2. From Command Line: You can run TestNG tests using Maven or Gradle by executing the following commands:

  • Maven: mvn test
  • Gradle: gradle test
testng-Suit-output
TestNG Suit Output



Article Tags :

Explore