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

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
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
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 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 Features Uescases
1. Assertions
Assertions validate the outcomes of your tests.
Example:
@Test
public void testAddition() {
int result = Calculator.add(2, 3);
Assert.assertEquals(result, 5, "Addition result is incorrect!");
}
2. Prioritizing Tests
Control test execution order using the priority attribute in @Test.
Example:
@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
Categorize tests into groups like smoke, regression, etc.
Example:
@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 parameterized testing through testng.xml or @DataProvider.
Example using testng.xml:
@Test
@Parameters({"username", "password"})
public void loginTest(String username, String password) {
System.out.println("Logging in with " + username + " and " + password);
}
Example using @DataProvider:
@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 track events during test execution.
Example:
public class TestListener implements ITestListener {
@Override
public void onTestSuccess(ITestResult result) {
System.out.println(result.getName() + " passed successfully.");
}
}
6. Annotations
TestNG annotations define the lifecycle and execution order of tests. Common annotations include:
@Test: Marks a method as a test.@BeforeSuiteand@AfterSuite: Run before and after the entire test suite.@BeforeClassand@AfterClass: Run before and after a test class.@BeforeMethodand@AfterMethod: Run before and after each test method.
Example:
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.");
}
}
Best Practices
- Use descriptive test names and logical groupings.
- Validate test results with assertions.
- Keep your
testng.xmlfile organized and up to date. - Use parameterization for data-driven testing.
- Regularly review and maintain your test cases.

