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
- Open Eclipse.
- Navigate to Help → Eclipse Marketplace.
- Search for TestNG.
- Install the TestNG plugin.
- 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
- Right-click the
src/test/resourcesfolder. - Select New → File.
- Name the file:
Define the Test Suite
Add the following configuration to the testng.xml file.
XML Tag Explanation
| Tag | Description |
|---|---|
\<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
- Right-click testng.xml.
- 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:
- Open the
test-outputfolder. - Open
index.htmlin 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:
- Navigate to the
test-outputfolder. - 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.xmlfile - 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.