TestNG - Basic Annotations - Listeners



@Listeners annotation defines listeners on a test class. @Listeners annotated method listens to certain events and keep track of test execution while performing some action at every stage of test execution. Events can be anything like say for example success of test method, failure of test method, start of test method etc.

Following are a few listeners or interfaces that allow you to modify TestNG's behavior:

  • IAnnotationTransformer

  • IAnnotationTransformer2

  • IHookable

  • IInvokedMethodListener

  • IMethodInterceptor

  • IReporter

  • ISuiteListener

  • ITestListener

@Listeners can be implemented at class level and suite level

The following is a list of attributes supported by the @Listeners annotation:

Attribute Description

value

An array of classes that extend org.testng.ITestNGListener

Create Test Case Class

Let's see how to invoke test methods using TestNG Listeners.

  • Create a java test class, say, SimpleTestClass.java in /work/testng/src.

  • Add a test method sum() and testtofail() to your test class.

  • Add an Annotation @Test to methods sum() and testtofail().

Following are the SimpleTestClass.java contents:

  import org.testng.Assert;
  import org.testng.annotations.Test;

  public class SimpleTestClass {

  	@Test
  	public void sum()
  	{
  		int sum=0;
  		int a=15;
  		int b=27;
  		sum=a+b;
  		System.out.println("sum="+sum);
  	}
  	@Test
  	public void testtofail()
  	{
  		System.out.println("Test case has failed");
  		Assert.assertTrue(false);
  	}
  }

Create Listener Test Class

Create a another java class, say, TestListener.java in /work/testng/src. We will implement the ITestListener.

  • ITestListener has following methods

    • onTestStart- onTestStart method is called when any Test starts.

    • onTestSuccess- onTestSuccess method is called on the success of any Test.

    • onTestFailure- onTestFailure method is called on the failure of any Test.

    • onTestSkipped- onTestSkipped method is called on skipped of any Test.

    • onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage.

    • onFinish- onFinish method is called after all Tests are executed.

TestListener.java is as below:

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

  public class TestListener  implements ITestListener   {
  	@Override
  	public void onTestStart(ITestResult result) {
  	}

  	@Override
  	public void onTestSuccess(ITestResult result) {
  	System.out.println("Success of test cases and its details are : "+result.getName());
  	}

  	@Override
  	public void onTestFailure(ITestResult result) {
  	System.out.println("Failure of test cases and its details are : "+result.getName());
  	}

  	@Override
  	public void onTestSkipped(ITestResult result) {
  	System.out.println("Skip of test cases and its details are : "+result.getName());
  	}

  	@Override
  	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
  	System.out.println("Failure of test cases and its details are : "+result.getName());
  	}

  	@Override
  	public void onStart(ITestContext context) {
  	// TODO Auto-generated method stub
  	}

  	@Override
  	public void onFinish(ITestContext context) {
  	// TODO Auto-generated method stub
  	}
  }

Create testng.xml

Next, let's create testng.xml file in /work/testng/src, to execute test case(s). This file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  <suite name="Suite">
    <listeners>
      <listener class-name="TestListener"/>
    </listeners>
    <test thread-count="5" name="Test">
      <classes>
        <class name="SimpleTestClass"/>
      </classes>
    </test> <!-- Test -->
  </suite> <!-- Suite -->

As you can see we need to define listeners in this XML file.

Compile the test case using javac.

/work/testng/src$ javac SimpleTestClass.java TestListener.java

Now, run the testng.xml, which will run the test case defined in <test> tag.

/work/testng/src$ java org.testng.TestNG testng.xml

Verify the output.

  sum=42
  Success of test cases and its details are : sum
  Test case has failed
  Failure of test cases and its details are : testtofail

  ===============================================
  Suite
  Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
  ===============================================

Advertisements