Custom Logger in TestNG
Custom logger in the TestNG will discuss about writing our own logger in TestNG. Logging is one of the most important things to remember in automation testing. Logger will keep on eye of the execution flow that where went wrong in the script or it very useful to debug the script.
To generate custom loggers in testng we need to use Listeners. To implement listeners, need to implement the ITestListener interface in your class. These classes are notified at runtime by TestNG when the test case executes.
For this we need to have at least 2 classes. One is for actual test cases and other is for to implement the ITestListener.
Below is the sample program for test cases class:
import org.testng.Assert; import org.testng.annotations.Test; public class loggerTest { @Test public void testOne() { Assert.assertTrue(true); } @Test public void testTwo() { Assert.assertTrue(false); } }
When you execute the above program, the status of the test cases should be listen by the listener class. We have two methods called testOne and testTwo in the above program one is to pass and second is to fail status. While executing the tests, the listener will track the status of the each test..
Below is the sample listener class which will implements the ITestListener
import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class listenerTest implements ITestListener { // It executes for on each test Start @Override public void onTestStart(ITestResult result) { System.out.println("Test Method Started :" + result.getName()); } // It executes for only Passed tests @Override public void onTestSuccess(ITestResult result) { System.out.println("Test Method Passed :" + result.getName()); } // It executes for only Failed tests @Override public void onTestFailure(ITestResult result) { System.out.println("Test Method Failed :" + result.getName()); } // It executes for only Skipped tests @Override public void onTestSkipped(ITestResult result) { System.out.println("Test Method Skipped :" + result.getName()); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // No need to implement all the methods. Can leave empty. } // It executes for on Suite start @Override public void onStart(ITestContext context) { System.out.println("Test on Start :" + context.getName()); } // It executes for on Suite finish @Override public void onFinish(ITestContext context) { System.out.println("Test on Finish :" + context.getName()); } }
The above class implements the ITestListener. So, it has to implement all the methods which are available in that interface. And it is not mandatory to use all the methods, we can left blank some of the methods which are not relevant to the requirement.
Now we have to create a testng.xml file to execute the above tests and it should have a mandatory tag called listener. It will call the listener class to listen the status of the each and every test which will execute.
Below is the sample program to execute the above program:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <listeners> <listener class-name="listenerTest"></listener> </listeners> <test name="Test"> <classes> <class name="loggerTest"/> </classes> </test> </suite>
Output for the above program is:
This way we can use custom listeners to log the messages while executing.
Please watch the you tube video for better understanding.