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.

Introduction to TestNG XML

TestNG is a powerful testing framework for Java developers, inspired by JUnit and NUnit. It supports a wide range of testing requirements, from unit testing to integration testing. One of TestNG's key features is the ability to control test execution using a TestNG XML file (testng.xml).

The testng.xml file defines which test classes and methods should be executed, allowing you to:

  • Organize test suites
  • Group related test cases
  • Configure test execution
  • Control execution order
  • Execute multiple test classes together

In this article, you'll learn how to create a TestNG XML file, execute it in Eclipse, and generate the built-in HTML and emailable reports provided by TestNG.


Prerequisites

Before you begin, ensure that you have the following installed:

  • Java Development Kit (JDK)
  • Eclipse IDE (or IntelliJ IDEA)
  • TestNG plugin
  • A Java Maven project (recommended)

Step 1: Set Up Your TestNG Project

Before creating a TestNG XML file, ensure that your project is configured with TestNG.

Install TestNG in Eclipse

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


Add TestNG Dependency (Maven)

If you're using Maven, add the following dependency to your pom.xml file:


Step 2: Create Test Classes

Create a Java class containing your test methods. Ensure that each test method is annotated with @Test.


Step 3: Create the TestNG XML File

Create the XML File

  1. Right-click the src/test/resources folder.
  2. Select New → File.
  3. Name the file:

Define the Test Suite

Add the following configuration to the testng.xml file.

XML Tag Explanation

TagDescription
\<suite>Defines a TestNG test suite. A suite can contain multiple tests.
\<test>Defines an individual test inside the suite.
\<classes>Specifies the list of test classes to execute.
\<class>Represents a single Java test class.

Step 4: Execute the TestNG XML File

Once the XML file has been created, you can execute the entire test suite.

Run the TestNG Suite in Eclipse

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

TestNG executes all classes defined in the XML file and automatically generates execution reports.


Step 5: View the Generated Reports

After execution, TestNG creates a test-output directory inside your project.

project  
│  
├── src  
├── test-output  
│   ├── index.html  
│   ├── emailable-report.html  
│   └── testng-results.xml  

HTML Report

The HTML report provides a comprehensive summary of the test execution.

To view it:

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

The report includes:

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


Emailable Report

The emailable report is a simplified version of the execution report intended for sharing with team members.

To view it:

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

The report provides:

  • Test execution summary
  • Pass/fail statistics
  • Overall execution status
    ---

Step 6: View the Test Output in Eclipse

After execution, Eclipse displays the test results in the TestNG console.

The console shows:

  • Passed tests
  • Failed tests
  • Skipped tests
  • Exceptions
  • Stack traces
  • Execution logs

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


Conclusion

The TestNG XML file is a powerful mechanism for organizing and controlling test execution. It enables developers to execute multiple test classes, configure test suites, and generate comprehensive reports with minimal effort.

By following this guide, you can:

  • Set up TestNG in your project
  • Create and configure a testng.xml file
  • Execute TestNG suites from Eclipse
  • Generate HTML and emailable reports
  • Analyze test execution results efficiently

Leveraging TestNG's reporting capabilities helps improve test visibility, simplifies debugging, and makes it easier to share execution results with your team.

Hi! I'm ERICA. Ask me anything!