Logging with TestNG using Listeners

In TestNG we can generate logs with the help of Listeners. Listeners implement the interface "org.testng.ITestListener". There are methods which gives you a real time information.

You can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.

What are Listeners in TestNG : -

To say it in simple words, we can make TestNG to listen what we say with the help of Listeners. Listeners give us the flexibility to modify default TestNG's behaviors.

By using TestNG listeners 'ITestListener' or 'TestListenerAdapter' we can change the default behaviour write our own implementation when a Test fails or Skips etc.

We have extended TestListenerAdapter which intern implements ITestListener with empty methods. So again we don't have to override other methods from the ITestListener interface which we may not needed.

We will see the below list of methods in the example

OnTestStart : Invoked each time before a test will be invoked.

OnTestSuccess : Invoked each time a test succeeds.

OnTestFailure : Invoked each time a test fails
We can implement any logic that you want to do when a test fails, Normally most of them prefer taking screen shots when a test fails. Here in this method we can add a logic to take the screen shot and the name of the test as screenshot name.

OnTestSkipped : Invoked each time a test is skipped.

OnTestFinish : Invoked after all the tests have run and all their Configuration methods have been called.

The below is the example program that demonstrates the Logging Listeners.

Step 1: First lets create a simple class called "LoggingClass.java" which has @Test methods.

package com.example.logging;

import org.testng.annotations.Test;

public class LoggingClass {

	@Test(priority = 0)
	public void methodAddingNumbers() {
		System.out.println("Helloo.. Im in method adding numbers");
	}

	@Test(priority = 1)
	public void dividedByZero() {
		System.out.println("Helloo.. Im in method divided by zero");
		int e = 1 / 0;
	}

	@Test(dependsOnMethods = { "dividedByZero" })
	public void methodSkip() {
		System.out.println("Helloo.. Im in method skip");
	}

}

In the above class, the method 'methodAddingNumbers()' will be executed successfully without any problems.

And the 'dividedByZero()' will be FAILED as we are trying to divide a number with Zero and we are not catching the exception.

We can use as '@Test(expectedExceptions=ArithmeticException.class)' to catch the exception and then that method will result as 'PASSED'.

The third method 'methodSkip' will be skipped as the dependent method "dividedByZero()" is FAILED.

Step 2: Now let us create a class called 'ListenersClass.java'

package com.example.logging;

import org.testng.IClass;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class ListenerClass extends TestListenerAdapter {

	@Override
	public void onTestStart(ITestResult tr) {
		log("Test Started....");
	}

	@Override
	public void onTestSuccess(ITestResult tr) {

		log("Test '" + tr.getName() + "' PASSED");

		// This will print the class name in which the method is present
		log(tr.getTestClass());

		// This will print the priority of the method.
		// If the priority is not defined it will print the default priority as
		// 'o'
		log("Priority of this method is " + tr.getMethod().getPriority());

		System.out.println(".....");
	}

	@Override
	public void onTestFailure(ITestResult tr) {

		log("Test '" + tr.getName() + "' FAILED");
		log("Priority of this method is " + tr.getMethod().getPriority());
		System.out.println(".....");
	}

	@Override
	public void onTestSkipped(ITestResult tr) {
		log("Test '" + tr.getName() + "' SKIPPED");
		System.out.println(".....");
	}

	private void log(String methodName) {
		System.out.println(methodName);
	}

	private void log(IClass testClass) {
		System.out.println(testClass);
	}
}

Apart from the above methods, you can also add many other methods which are present in 'TestListenerAdapter' class.

Please click here for more 'TestListenerAdapter' classes

Step 3: The below is the testng.xml file.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Log Suite Example" verbose="1">
	<listeners>
		<listener class-name="com.example.logging.ListenerClass" />
	</listeners>

	<test name="TestNG logs sample" preserve-order="true">
		<classes>
			<class name="com.example.logging.LoggingClass">
				<methods>
					<include name="methodAddingNumbers" />
					<include name="dividedByZero" />
					<include name="methodSkip" />
				</methods>
			</class>
		</classes>
	</test>

</suite>

In the above testng.xml file, We need to pass the class name where we have the test methods. And also we should define listeners in testng.xml file so that TestNG can use them to rewrite the annotations.

The below is the ouput :

[TestNG] Running:
  G:\TestNG\TestNGExamples\testng.xml

Test Started....
Helloo.. Im in method adding numbers
Test 'methodAddingNumbers' PASSED
[TestClass name=class com.example.logging.LoggingClass]
Priority of this method is 0
.....
Test Started....
Helloo.. Im in method divided by zero
Test 'dividedByZero' FAILED
Priority of this method is 1
.....
Test 'methodSkip' SKIPPED
.....

===============================================
Log Suite Example
Total tests run: 3, Failures: 1, Skips: 1
===============================================

If you observe the above output, for the first test, The first line is 'Test Started...' which is printed because 'onTestStart()' was invoked and that method is PASSED.
And we have class name and the priority printed as 'onTestSuccess()' is invoked when the test is succeeds. And

For the second test case which is FAILED, we have printed only the 'Priority'.

Test Frameworks: 

Comments

in real world, how do we generate logs? is it by using log4j or testNG listener??

How can we import all methods automatically? i don't want to write all methods of the class to implement manually. Is there a way we can get all functions automatically

Hi, I have one question on TestNG.
I have few tests , before starting the tests it is executing before class method where it will do some actions on the server.
Sometimes this @Beforeclass it self is failing and causing all test cases to skip which is expected.
Is there any way to take screen shot on @BeforeClass failure?
This is working fine for @Test methods.

Please provide your valuable suggestions.

Add new comment

Image CAPTCHA