Features

What are TestNG listeners?

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

When you are using a test framework such as TestNG, you can utilise listeners. Listeners are components or classes that are implemented to listen to events (and trigger potential actions) during different phases of test execution.

These events can include the start of a test, the completion of one ore more tests, the success or failure of a test step and more. By using listeners, you can customize and extend the behavior of your automated tests without modifying the code of the test scripts directly.

Listeners are part of the TestNG testing framework. TestNG has excellent support for Selenium WebDriver automation. TestNG allows you to define listeners that respond to various events during the test lifecycle. Some commonly used listeners are listed below.

TestListenerAdapter

  • onTestStart(ITestResult result): Invoked when a test method starts.
  • onTestSuccess(ITestResult result): Invoked when a test method succeeds.
  • onTestFailure(ITestResult result): Invoked when a test method fails.
  • onTestSkipped(ITestResult result): Invoked when a test method is skipped.

ISuiteListener

  • onStart(ISuite suite): Invoked before the Suite starts.
  • onFinish(ISuite suite): Invoked after the Suite finishes.

IInvokedMethodListener

  • beforeInvocation(IInvokedMethod method, ITestResult testResult): Invoked before the test method is invoked.
  • afterInvocation(IInvokedMethod method, ITestResult testResult): Invoked after the test method is invoked.

Listeners are implemented by creating classes that extend the corresponding listener interfaces. You can then add these new listener classes to the TestNG test suite configuration.

For an example of such a class, please see the example below.

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomTestListener 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());
    }

    // Other methods of ITestListener can be implemented as needed


}

To use this new listener, you can configure it in your TestNG XML file. See the example below.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
    <listeners>
        <listener class-name="path.to.CustomTestListener"/>
    </listeners>
    <!-- Test classes, methods, etc. -->
</suite>

When you run your TestNG tests, the listener methods will be executed based on the events occurring during the test execution. Listeners provide a powerful mechanism for logging, reporting and performing additional actions in response to test events without modifying the code of your test scripts.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Questions