Understanding Listeners and Extent Reports in TestNG

In the world of software testing, the ability to monitor and report the results of test executions is crucial. TestNG, a popular testing framework for Java, provides powerful features like Listeners and Extent Reports that enhance the testing experience. In this blog post, we will explore what Listeners are, how to implement them, and how to integrate Extent Reports to generate comprehensive test reports.

What are Listeners in TestNG?

Listeners in TestNG are special interfaces that allow you to listen to the events occurring during test execution. They enable you to define actions that should be taken at specific points in the testing process, such as when a test starts, finishes, or fails. This functionality helps in customizing the reporting and handling of test results effectively.

Types of Listeners

TestNG provides several built-in listener interfaces that you can implement in your tests:

  1. ITestListener: This listener is triggered for test-level events, such as when a test starts, succeeds, or fails.
  2. ISuiteListener: This listener is invoked for suite-level events, such as when a test suite starts or finishes.
  3. IReporter: This interface is used for generating custom reports after the test execution.

Implementing Listeners

To create a custom listener, you need to implement one of the listener interfaces. Here’s a basic example using the ITestListener interface:

import org.testng.ITestContext;

import org.testng.ITestListener;

import org.testng.ITestResult;


public class CustomListener implements ITestListener {


    @Override

    public void onTestStart(ITestResult result) {

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

    }


    @Override

    public void onTestSuccess(ITestResult result) {

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

    }


    @Override

    public void onTestFailure(ITestResult result) {

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

    }


    @Override

    public void onFinish(ITestContext context) {

        System.out.println("All tests finished.");

    }

}

To use this listener in your test suite, you can annotate your test class with @Listeners:

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(CustomListener.class)
public class SampleTest {

    @Test
    public void testMethod1() {
        // Your test code here
    }

    @Test
    public void testMethod2() {
        // Your test code here
    }
}

Integrating Extent Reports with TestNG

While TestNG provides basic reporting features, integrating Extent Reports adds a rich and visually appealing report format. Extent Reports allows you to generate detailed HTML reports that include logs, screenshots, and additional information about the tests.

Setting Up Extent Reports

1. Add Dependencies: First, you need to add the Extent Reports dependency to your pom.xml if you are using Maven:

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>5.0.9</version>
</dependency>

2. Create an Extent Report Listener: Create a listener that initializes Extent Reports and generates the report.

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ExtentReportListener implements ITestListener {
    private ExtentReports extent;
    private ExtentTest test;

    @Override
    public void onStart(ITestContext context) {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent.html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
    }

    @Override
    public void onTestStart(ITestResult result) {
        test = extent.createTest(result.getMethod().getMethodName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        test.pass("Test passed");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        test.fail("Test failed: " + result.getThrowable());
    }

    @Override
    public void onFinish(ITestContext context) {
        extent.flush();
    }
}

3. Using the Extent Report Listener: Similar to the previous listener, add the @Listeners annotation to your test class:

@Listeners(ExtentReportListener.class)
public class SampleTest {

    @Test
    public void testMethod1() {
        // Your test code here
    }

    @Test
    public void testMethod2() {
        // Your test code here
    }
}


Followers