Wednesday, December 17, 2025

thumbnail

How to Create TestNG Reports

 How to Create TestNG Reports


TestNG is a popular testing framework inspired by JUnit but with more powerful features for test configuration, parallel execution, and reporting. One of the key features of TestNG is its ability to generate detailed and customizable test reports that help developers and testers understand test results.


TestNG generates HTML and XML reports by default. In this guide, we'll explain how to create and customize TestNG reports, and how to extend the reporting functionality with advanced tools.


1. Default TestNG Report Generation


When you run your tests with TestNG, it automatically generates two types of reports:


HTML Report: A detailed, user-friendly report in HTML format.


XML Report: An XML file containing detailed test execution information.


Running Tests with TestNG (Basic Execution)


If you're running your tests using Maven, the TestNG plugin will generate the reports by default.


mvn test



For JUnit/IDE users, you can run the tests directly using the TestNG runner in the IDE, which will also generate reports.


Default Report Locations:


HTML Report: Located at target/surefire-reports/index.html (if using Maven)


XML Report: Located at target/testng-*.xml


These reports give you detailed insights into how your tests performed. For example:


HTML Report shows the test suite, test methods, passed, failed, and skipped tests.


XML Report is more machine-readable and contains all the details, including suite and test execution info.


2. Customizing TestNG Report Output


TestNG allows you to customize and enhance the default reports by creating listeners and reporters. This is useful if you want to add additional information, change the format of reports, or generate more advanced reports.


Custom Reporters


You can create custom reporters in TestNG by implementing the org.testng.reporters.IReporter interface. TestNG provides a built-in TestNGReporter class, which you can extend to customize your reports.


Example: Creating a Custom Reporter


Create a class that implements IReporter to customize the report:


import org.testng.ITestContext;

import org.testng.ITestResult;

import org.testng.reporters.XMLReporterConfig;

import org.testng.reporters.IReporter;

import org.testng.xml.XmlSuite;

import org.testng.xml.XmlTest;

import org.testng.reporters.XMLReporter;


import java.util.List;


public class CustomReportGenerator implements IReporter {


    @Override

    public void generateReport(List<XmlSuite> xmlSuites, List<XmlTest> tests, ITestContext context) {

        System.out.println("Custom Report Generation Started");


        // Process each test case here

        for (ITestResult result : context.getPassedTests().getAllResults()) {

            System.out.println("Test Passed: " + result.getName());

        }

        for (ITestResult result : context.getFailedTests().getAllResults()) {

            System.out.println("Test Failed: " + result.getName());

        }

    }

}



In this example, the CustomReportGenerator class will print the test names for passed and failed tests. You can extend this to generate a detailed report or log in any format (e.g., CSV, database, or JSON).


3. Use TestNG's Built-in Reporters


TestNG comes with several built-in reporters that allow you to generate detailed reports without much configuration:


3.1. HTML Reporter (Default)


The HTML Reporter is enabled by default, and TestNG generates an HTML report after the tests are executed. This is a quick way to view test results. To ensure it's enabled, you can add the following configuration to your testng.xml file:


<suite name="MySuite">

    <test name="MyTest">

        <classes>

            <class name="com.example.tests.MyTest"/>

        </classes>

    </test>

</suite>



Running the tests will automatically generate an HTML report at target/surefire-reports/index.html.


3.2. Emailable Report


TestNG also provides a "emailable" report that can be sent via email. You can enable this feature by using the EmailableReporter class.


To use the emailable report, add the following dependency to your pom.xml (if using Maven):


<dependency>

    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>7.4.0</version>  <!-- Use latest version -->

</dependency>



TestNG will generate a simple, email-friendly HTML report that you can send to stakeholders or team members.


3.3. XML Report (Detailed)


TestNG generates a detailed XML report by default, which you can use for further analysis, integrations, or CI/CD pipelines. This XML report is located in the target/testng-results.xml file.


To generate XML output, you simply need to configure TestNG as follows:


<suite name="Suite">

    <test name="Test">

        <classes>

            <class name="com.example.tests.MyTest"/>

        </classes>

    </test>

</suite>


4. Integrating with Other Reporting Tools


For more advanced reporting, you can integrate TestNG with third-party tools like Allure or ExtentReports for rich, interactive, and visually appealing reports.


4.1. Allure Reporting


Allure

 is a popular reporting framework that integrates well with TestNG, providing beautiful, interactive reports.


Steps to integrate Allure:


Add dependencies in pom.xml:


<dependencies>

    <dependency>

        <groupId>io.qameta.allure</groupId>

        <artifactId>allure-testng</artifactId>

        <version>2.18.1</version> <!-- Check for the latest version -->

    </dependency>

</dependencies>



Generate Allure Report after running tests:


Add the following Maven plugin for Allure report generation:


<build>

    <plugins>

        <plugin>

            <groupId>io.qameta.allure</groupId>

            <artifactId>allure-maven</artifactId>

            <version>2.9</version>

            <executions>

                <execution>

                    <phase>test</phase>

                    <goals>

                        <goal>serve</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>



Generate and serve the Allure report:


Run the following command after your tests have finished:


mvn clean test allure:serve



This will open the Allure report in your default web browser.


4.2. ExtentReports Integration


ExtentReports

 is another powerful reporting library that provides detailed, customizable, and interactive reports.


Steps to integrate ExtentReports:


Add dependency to pom.xml:


<dependency>

    <groupId>com.aventstack</groupId>

    <artifactId>extentreports</artifactId>

    <version>4.0.9</version> <!-- Latest version -->

</dependency>



Configure and generate reports in your test class:


import com.aventstack.extentreports.ExtentReports;

import com.aventstack.extentreports.ExtentTest;

import com.aventstack.extentreports.reporter.ExtentHtmlReporter;


public class MyTest {


    private static ExtentReports extent;


    @BeforeSuite

    public static void setup() {

        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extentReports.html");

        extent = new ExtentReports();

        extent.attachReporter(htmlReporter);

    }


    @Test

    public void testMethod() {

        ExtentTest test = extent.createTest("Test Method");

        test.info("Test Started");

        assertTrue(true);

        test.pass("Test Passed");

    }


    @AfterSuite

    public static void tearDown() {

        extent.flush();

    }

}



Run the tests: When the tests complete, an HTML report will be generated in the extentReports.html file.


5. Customizing TestNG Reports


TestNG allows you to customize how reports are generated by implementing listeners or by overriding certain reporting behavior. For example, you can customize the name of your reports, change their location, and include additional details like screenshots, logs, or execution time.


Conclusion


TestNG provides a solid foundation for generating reports out of the box, including HTML and XML reports, and allows customization via listeners and custom reporters. For more advanced reporting, you can integrate third-party tools like Allure and ExtentReports for interactive, visually appealing reports.


Default Reports: TestNG generates simple HTML and XML reports.


Custom Reports: You can implement custom reporting logic using TestNG listeners or by creating custom reporters.


Third-Party Reports: Tools like Allure and ExtentReports enhance your reports with rich, interactive features.


By using these approaches, you can generate detailed, insightful, and professional test reports to help teams track test results and make informed decisions.

Learn Selenium with JAVA Training in Hyderabad

Read More

Maven Project Setup for Selenium Testing

Adding Custom Reports in Your Framework

How to Use POM with PageFactory

Introduction to Data Driven Framework

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive