A powerful testing framework for Java inspired by JUnit and NUnit with innovative features designed for modern testing needs.
A testing framework designed to simplify a broad range of testing needs
TestNG is a testing framework inspired by JUnit and NUnit but with new functionalities that make it more powerful and easier to use. It was designed to cover all categories of tests: unit, functional, end-to-end, integration, etc.
TestNG eliminates most of the limitations of older frameworks like JUnit and includes new functionalities that make your testing more powerful and straightforward, such as flexible test configuration, support for data-driven testing, and powerful execution model.
import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.Assert; public class SimpleTest { private Calculator calculator; @BeforeMethod public void setUp() { calculator = new Calculator(); } @Test public void testAddition() { int result = calculator.add(2, 3); Assert.assertEquals(result, 5); } @Test public void testSubtraction() { int result = calculator.subtract(5, 2); Assert.assertEquals(result, 3); } @Test(groups = {"critical"}) public void testMultiplication() { int result = calculator.multiply(2, 3); Assert.assertEquals(result, 6); } }
This simple example demonstrates TestNG’s annotation-based approach to defining test methods and setup procedures.
Create test classes with TestNG annotations to define test methods and their behavior
Create TestNG XML files to organize tests into suites and define execution parameters
Execute tests via IDE, Maven, Gradle, or command line with detailed control over test execution
Review comprehensive test reports and integrate with CI/CD pipelines
Why developers choose TestNG for their testing needs
Powerful annotations like @Test, @BeforeMethod, @AfterClass, and more to control test execution flow and behavior with minimal code.
Configure tests using XML files or programmatically, allowing for fine-grained control over test execution order, grouping, and parallelization.
Run tests with multiple data sets using @DataProvider annotation, making it easy to test the same functionality with different inputs.
Run tests in parallel at various levels (methods, classes, or suites) to significantly reduce test execution time in large test suites.
Define dependencies between test methods, ensuring that dependent tests are skipped if prerequisite tests fail, creating more robust test suites.
Generate detailed HTML reports with test execution statistics, making it easy to identify and fix issues in your test suite.
Begin your TestNG journey in minutes
Add the following to your pom.xml:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.7.1</version> <scope>test</scope> </dependency>
project/ ├── src/ │ ├── main/java/ │ │ └── com/example/app/ │ │ └── Calculator.java │ └── test/java/ │ └── com/example/app/ │ ├── CalculatorTest.java │ └── AdvancedTest.java ├── src/test/resources/ │ └── testng.xml └── pom.xml
1. Create a simple class to test:
package com.example.app; public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }
2. Create a test class:
package com.example.app; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class CalculatorTest { private Calculator calculator; @BeforeMethod public void setUp() { calculator = new Calculator(); } @Test public void testAddition() { int result = calculator.add(3, 5); Assert.assertEquals(result, 8); } @Test public void testSubtraction() { int result = calculator.subtract(10, 4); Assert.assertEquals(result, 6); } }
3. Create a TestNG XML file (src/test/resources/testng.xml):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Calculator Test Suite"> <test name="Basic Tests"> <classes> <class name="com.example.app.CalculatorTest"/> </classes> </test> </suite>
4. Run your tests:
# Using Maven mvn test # Using Gradle gradle test
Real-world examples to inspire your testing
import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataDrivenTest { @DataProvider(name = "loginData") public Object[][] createLoginData() { return new Object[][] { { "user1", "pass1", true }, { "user2", "wrongpass", false }, { "invaliduser", "pass3", false }, { "admin", "admin123", true } }; } @Test(dataProvider = "loginData") public void testLogin(String username, String password, boolean expectedResult) { LoginService service = new LoginService(); boolean result = service.login(username, password); Assert.assertEquals(result, expectedResult, "Login result incorrect for user: " + username); } }
import org.testng.Assert; import org.testng.annotations.Test; public class DependencyTest { @Test public void createUser() { // Code to create a user System.out.println("User created"); Assert.assertTrue(true); } @Test(dependsOnMethods = {"createUser"}) public void updateUser() { // This test will only run if createUser passes System.out.println("User updated"); Assert.assertTrue(true); } @Test(dependsOnMethods = {"updateUser"}) public void deleteUser() { // This test will only run if updateUser passes System.out.println("User deleted"); Assert.assertTrue(true); } @Test(dependsOnMethods = {"createUser"}, alwaysRun = true) public void listUsers() { // This test will run even if createUser fails System.out.println("Users listed"); Assert.assertTrue(true); } }
<!-- testng-parallel.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Parallel Test Suite" parallel="methods" thread-count="4">
<test name="Parallel Tests">
<classes>
<class name="com.example.app.UserServiceTest"/>
<class name="com.example.app.ProductServiceTest"/>
<class name="com.example.app.OrderServiceTest"/>
</classes>
</test>
</suite>
This configuration runs test methods in parallel with 4 threads, significantly reducing execution time for large test suites.
import org.testng.annotations.Test; public class GroupTest { @Test(groups = {"smoke"}) public void testLogin() { // Basic login test } @Test(groups = {"smoke", "regression"}) public void testBasicSearch() { // Basic search functionality } @Test(groups = {"regression"}) public void testAdvancedSearch() { // Advanced search functionality } @Test(groups = {"performance"}) public void testSearchPerformance() { // Search performance test } }
XML configuration to run only smoke tests:
<suite name="Smoke Test Suite"> <test name="Smoke Tests"> <groups> <run> <include name="smoke"/> </run> </groups> <classes> <class name="com.example.app.GroupTest"/> </classes> </test> </suite>
Expand your TestNG knowledge
From software developers to IT managers, we connect tech talent with innovative companies.
Expert financial professionals to help your business manage resources effectively.
Qualified healthcare professionals to provide exceptional patient care.
From software developers to IT managers, we connect tech talent with innovative companies.
Expert financial professionals to help your business manage resources effectively.
Qualified healthcare professionals to provide exceptional patient care.
Join thousands of developers who are building better tests with TestNG
Sri Mallikarjuna Nilayam,
H NO: 11-13-484
Yadav Nagar ,Alkapuri (Nagole)
Hyderabad
Contact:
9966620266, 9966682866
ssmarketingsoftwaresolutions@gmail.com
SS Marketing & Software Solutions © 2025. All Rights Reserved | Digital Marketing & Job Consultancy
WhatsApp us