Next Generation Testing with TestNG

A powerful testing framework for Java inspired by JUnit and NUnit with innovative features designed for modern testing needs.

What is TestNG?

A testing framework designed to simplify a broad range of testing needs

Beyond JUnit

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.

Key Concepts:

Why Choose TestNG?

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.

A Simple TestNG Test

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.

The TestNG Workflow

01

Write Tests

Create test classes with TestNG annotations to define test methods and their behavior

 

02

Configure XML

Create TestNG XML files to organize tests into suites and define execution parameters

03

Run Tests

Execute tests via IDE, Maven, Gradle, or command line with detailed control over test execution

04

Analyze Reports

Review comprehensive test reports and integrate with CI/CD pipelines

Key Features

Why developers choose TestNG for their testing needs

Annotations

Powerful annotations like @Test, @BeforeMethod, @AfterClass, and more to control test execution flow and behavior with minimal code.

Flexible Test Configuration

Configure tests using XML files or programmatically, allowing for fine-grained control over test execution order, grouping, and parallelization.

Data-Driven Testing

Run tests with multiple data sets using @DataProvider annotation, making it easy to test the same functionality with different inputs.

Parallel Execution

Run tests in parallel at various levels (methods, classes, or suites) to significantly reduce test execution time in large test suites.

Dependencies

Define dependencies between test methods, ensuring that dependent tests are skipped if prerequisite tests fail, creating more robust test suites.

Comprehensive Reporting

Generate detailed HTML reports with test execution statistics, making it easy to identify and fix issues in your test suite.

Getting Started

Begin your TestNG journey in minutes

Installation

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 Structure

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

Creating Your First Test

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

Best Practices

Writing Good Tests

TestNG Configuration

TestNG in Action

Real-world examples to inspire your testing

Data-Driven 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);
    }
}

Test Dependencies

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);
    }
}

Parallel Test Execution

<!-- 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.

Groups and Filtering

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>

Learning Resources

Expand your TestNG knowledge

Official Documentation

From software developers to IT managers, we connect tech talent with innovative companies.

Video Tutorials

Expert financial professionals to help your business manage resources effectively.

Community Forums

Qualified healthcare professionals to provide exceptional patient care.

Sample Projects

From software developers to IT managers, we connect tech talent with innovative companies.

Cheat Sheets

Expert financial professionals to help your business manage resources effectively.

Online Courses

Qualified healthcare professionals to provide exceptional patient care.

Ready to get started with TestNG?

Join thousands of developers who are building better tests with TestNG

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec