Automation testing ensures software is robust, reliable, and bug-free. Among various frameworks, TestNG stands out for its power, flexibility, and ease of use. It helps developers and testers create efficient, scalable, and manageable test workflows. TestNG provides a range of features that make it the best choice for automation testing:

- XML Configuration: Test cases can be configured using XML files, offering better organization and control. For instance, you can define test groups, prioritize test cases, and specify dependencies in the XML file.
- Annotations: Annotations like
@Test,@BeforeMethod,@AfterMethod, etc., allow developers to control the test execution flow, manage dependencies between methods, and group test cases easily. - Data-Driven Testing: TestNG supports data-driven testing using the
@DataProviderannotation, enabling you to run the same test case with multiple data sets. - Built-in Reporting: Detailed HTML reports are generated by default after execution, providing insights into test results, including passed, failed, and skipped tests.
- Parameterized Testing: TestNG allows parameters to be passed to test methods using the
@Parametersannotation, making it easier to test with varying inputs. - Parallel Execution: TestNG supports parallel test execution, making it efficient for large test suites and improving testing speed.
we’ll explore the core features of TestNG, including annotations, parameterization, multithreading, and more.
1. Multiple Before and After Annotation Options
TestNG having the annotations that allow developers to specify actions to be executed before and after test methods, test classes, test groups, or entire test suites. It will improve the structure and reusability of test scripts.
- @BeforeSuite: Executes once before the test suite starts.
- @AfterSuite: Executes once after the test suite finishes.
- @BeforeTest: Executes before any test methods within the
<test>tag in the XML file. - @AfterTest: Executes after all test methods in the
<test>tag have been run. - @BeforeClass: Executes before the first method of the current class is invoked.
- @AfterClass: Executes after all the methods of the current class have been executed.
- @BeforeMethod: Executes before each test method.
- @AfterMethod: Executes after each test method.
AllAnnotationsTestGFG
package com.example.tests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AllAnnotationsTestGFG {
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@Test
public void testMethod1() {
System.out.println("Test Method 1");
}
@Test
public void testMethod2() {
System.out.println("Test Method 2");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}
}
Output:

2. XML-based Test Configuration
TestNG uses an XML file (testng.xml) to configure and organize tests. an TestNG XML file is a essential part of TestNG, making it easier to control how tests are executed and grouped in the proper flow.
Key Features
- Test Suites: Organize multiple tests into a suite for the proper ordered execution of the classes.
- Groups: Group tests based on functionality, enabling selective execution which is need to be.
- Parameters: Pass parameters to test methods from the XML file.
- Test Method Configuration: Specify which test methods to include or exclude in the execution for easy skipping any test or focus on the specific part which we need to test.
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite">
<test name="Test 1">
<classes>
<class name="com.example.tests.SampleTest"/>
<class name="com.example.tests.PriorityExampleTest"/>
</classes>
</test>
</suite>
Output:

3. Dependent Methods
With the dependsOnMethods attribute testng will be used to define dependencies between test methods and you can specify that a test method should only be executed if other methods have successfully passed with some cases.
- Depend-on-method: Specifies that a test depends on the completion of one or more other test methods which is need to be execute before that testcase which is need to be execute.
- Flexibility: You can configure these method in any scenario which you like when some condition fails then only the testcase will be execute which is having the method.
package com.example.tests;
import org.testng.annotations.Test;
public class testNGDependantMethodGFG {
@Test
public void test1() {
System.out.println("Test 1");
}
@Test(dependsOnMethods = {"test1"})
public void test2() {
System.out.println("Test 2");
}
@Test(dependsOnMethods = {"test1", "test2"})
public void test3() {
System.out.println("Test 3");
}
}
Output:
test2 and test3 depend on test1. If test1 fails, neither test2 nor test3 will be executed.

4. Groups and Group of Groups
TestNG provides the ability to group test methods and organize them in hierarchical structures. You can declare test methods in a group and later gGroups and Group of Groupsroup them inside other groups, which makes organizing tests more flexible.
- Group Inclusion: You can include a specific set of groups in your execution.
- Group Exclusion: You can exclude a certain set of groups from execution.
package com.example.tests;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SampleTestGroups {
@Test(groups = "smoke")
public void testSmoke() {
System.out.println("Smoke Test");
}
@Test(groups = "regression")
public void testRegression() {
System.out.println("Regression Test");
}
}
Use the testng.xml for the execution purpose:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Example Suite">
<test name="Smoke Tests">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="com.example.tests.TestNGExample"/>
</classes>
</test>
</suite>
Output:

5. Parameterization of Test Methods
TestNG allows you to pass parameters to your test methods to run the same test with different inputs. This feature supports both @Parameters annotation and DataProviders.
Using @Parameters Annotation: This annotation allows you to pass parameters from testng.xml into your test methods.
package com.example.tests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestNGExample {
@DataProvider(name = "userData")
public Object[][] dataProvider() {
return new Object[][] {
{ "admin", "password123" },
{ "user", "password456" }
};
}
@Test(dataProvider = "userData")
public void loginTest(String username, String password) {
System.out.println("Username: " + username);
System.out.println("Password: " + password);
}
}
testng.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Login Test">
<parameter name="username" value="testUser"/>
<parameter name="password" value="testPass"/>
<classes>
<class name="com.example.tests.ParameterizedTest"/>
</classes>
</test>
</suite>
Output:

6. Multithreaded Execution
TestNG supports multithreaded execution, which allows you to run tests in parallel, speeding up test execution. This is especially useful for large test suites or performance testing.
Multithreading can be configured in testng.xml using the parallel attribute.
<suite name="Parallel Suite" parallel="methods">
<test name="Test 1">
<classes>
<class name="com.example.TestNGExample"/>
</classes>
</test>
</suite>
7. Better Reporting
TestNG generates detailed XML and HTML reports by default, making it easy to track the execution of test cases. You can also configure custom reports if needed.
Benefits of Using TestNG
Here are the few benefits of the TestNG which are follows:

- Simplified Test Structure: Test cases are defined with the
@Testannotation, eliminating the need for amainmethod. TestNG internally manages the test execution. - Built-in Assertions: Assertions like
Assert.assertEquals()provide a direct way to validate test results, replacing manualif-elsechecks. - Detailed Reporting: TestNG generates comprehensive HTML reports, showcasing the status of test cases along with execution times and error messages.
- Annotations for Control: Annotations such as
@BeforeSuite,@AfterSuite,@BeforeTest, and@AfterTestallow fine-grained control over the testing lifecycle. - Scalability: TestNG handles large test suites effortlessly with XML-based configurations and supports integration with tools like Maven for build management.
TestNG is a powerful testing framework that simplifies test execution, organization, and reporting. It supports grouping, parameterization, multithreading, and detailed reports, making automation efficient. Whether for unit or integration testing, TestNG enhances maintainability, flexibility, and reliability, making it a must-have for developers.