Introduction to TestNG XML

Introduction to TestNG XML

·Pavan Kalyan Meda

Learn how to create, configure, and execute a TestNG XML file to organize test execution and generate detailed TestNG reports in Eclipse.

How to Create and Execute TestNG XML Files in Eclipse and Generate Reports

Introduction

TestNG is a powerful Java testing framework inspired by JUnit and NUnit. It is widely used for unit, functional, integration, and end-to-end testing. One of its most useful features is the TestNG XML configuration file, which allows you to control how test cases are executed without modifying your source code.

Using a TestNG XML file, you can:

  • Execute selected test classes or methods.
  • Organize tests into groups.
  • Run multiple test suites.
  • Configure parallel execution.
  • Define test parameters.
  • Control the overall test execution flow.

In this guide, you'll learn how to create a TestNG project, generate and execute a TestNG XML file in Eclipse, and understand the reports that TestNG generates automatically after execution.


Prerequisites

Before you begin, ensure you have the following installed and configured:

RequirementDescription
Java Development Kit (JDK)Java must be installed and configured.
Eclipse IDEEclipse with Java development support.
TestNG PluginInstalled in Eclipse.
MavenRecommended for dependency management.
TestNG ProjectA Java or Maven project configured for TestNG.

Step 1: Set Up a TestNG Project

Before creating a TestNG XML file, configure your project with TestNG.

Install TestNG in Eclipse

  1. Open Eclipse.
  2. Navigate to Help → Eclipse Marketplace.
  3. Search for TestNG.
  4. Click Install.
  5. Restart Eclipse after the installation completes.


Add TestNG to a Maven Project

If your project uses Maven, add the following dependency to the pom.xml file.

\<!-- https://mvnrepository.com/artifact/org.testng/testng -->  
\<dependency>  
    \<groupId>org.testng\</groupId>  
    \<artifactId>testng\</artifactId>  
    \<version>7.10.2\</version>  
    \<scope>test\</scope>  
\</dependency>  

After saving the file, Maven automatically downloads the required TestNG libraries.


Step 2: Create Test Classes

Create one or more Java classes containing your test methods.

Each test method should be annotated with the @Test annotation so that TestNG can identify and execute it.

In the next section, you'll create a TestNG XML file to specify which test classes and methods should be executed during the test run.

Step 3: Create the TestNG XML File

A TestNG XML file defines how your test suite should be executed. It specifies the test classes, execution order, grouping, parallel execution, and other configuration settings.


Create the XML File

  1. Right-click the src/test/resources folder.

    If the folder doesn't exist, create it first.

  2. Select New → File.
  3. Name the file testng.xml.
  4. Click Finish.


Configure the Test Suite

Open the testng.xml file and add the following configuration.

\<?xml version="1.0" encoding="UTF-8"?>  
\<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

\<suite name="Suite">

    \<test thread-count="5" name="Test">

        \<classes>

            \<class name="practice.SampleTest"/>

        \</classes>

    \</test>

\</suite>  

XML Tags Explained

TagDescription
\<suite>Defines a TestNG test suite that can contain one or more test definitions.
\<test>Represents a logical test configuration within the suite.
\<classes>Specifies the list of Java test classes to execute.
\<class>Identifies the fully qualified name of the test class.

Step 4: Execute the TestNG XML File

Once the XML configuration is ready, execute the test suite from Eclipse.

Run the Test Suite

  1. Right-click testng.xml.
  2. Select Run As → TestNG Suite.

TestNG will execute all the test classes defined in the XML file and automatically generate execution reports.


Step 5: View the Generated Reports

After execution completes, TestNG creates a test-output directory in the project root.

This folder contains multiple reports, including detailed HTML reports and a simplified report for sharing.


HTML Report

The HTML report provides comprehensive information about the test execution.

To view it:

  1. Open the test-output folder.
  2. Open index.html in any web browser.

The report includes:

  • Total tests executed
  • Passed tests
  • Failed tests
  • Skipped tests
  • Execution time
  • Error messages
  • Stack traces
  • Test execution details


Emailable Report

TestNG also generates a lightweight report designed for sharing results.

To view it:

  1. Open the test-output folder.
  2. Open emailable-report.html.

This report provides a concise summary of the test execution, making it suitable for email or project status updates.


Step 6: View the Test Execution Output

After the suite finishes execution, you can monitor the results directly within Eclipse.

Open the TestNG Console to view:

  • Test execution status
  • Passed and failed test cases
  • Exceptions
  • Stack traces
  • Execution logs

This helps quickly identify failures and troubleshoot issues during test execution.

Conclusion

TestNG XML files provide a flexible and organized way to manage test execution without modifying the source code. By defining test suites in an XML configuration, you can execute selected test classes, control execution order, and customize your testing workflow.

In this guide, you learned how to:

  • Configure a TestNG project in Eclipse.
  • Create and configure a TestNG XML file.
  • Execute the XML test suite.
  • Generate HTML and emailable reports.
  • Analyze test execution results using the TestNG Console and generated reports.

Using TestNG XML files improves test organization, simplifies automation, and makes it easier to share execution results with your team, making it an essential feature for Java test automation projects.

Hi! I'm ERICA. Ask me anything!