This post will explain the integration of any java logger (let’s say Log4j) with ITestListener to customise result output and print the same to log file.
We have seen testNG and it’s listener ITestListener to customise the result output.
Watch details here
Log4j maven dependency
Use below dependency –
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Log4j properties file
Create a new folder “resources” under “src > main” folder, and create a new file log4j.properties under src > main > resources with below content
# Root logger option log4j.rootLogger=INFO, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Redirect log messages to a log file, support file rolling. log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=${user.dir}/Log4j/application.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Project folder structure
Implement ITestListener interface
Create a package “testNGListener.Logger”, and then create TestListeners.java
package testNGListener.Logger; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class TestListeners implements ITestListener{ public void onTestStart(ITestResult result) { Log.info(result.getMethod().getMethodName() + " Started"); Log.info(result.getMethod().getDescription()); } public void onTestSuccess(ITestResult result) { Log.info(result.getMethod().getMethodName() + " Passed"); } public void onTestFailure(ITestResult result) { Log.info("Failed because of - "+ result.getThrowable()); } public void onTestSkipped(ITestResult result) { Log.info("Skipped because of - "+ result.getThrowable()); } public void onTestFailedButWithinSuccessPercentage(ITestResult result) { // TODO Auto-generated method stub } public void onStart(ITestContext context) { Log.info("=========== onStart :-" + context.getName() + "==============="); } public void onFinish(ITestContext context) { Log.info("=========== onFinish :-" + context.getName() + "==============="); } }
Create a common Log class
Log.java
package testNGListener.Logger; import org.apache.log4j.Logger; public class Log { private static Logger Log = Logger.getLogger(Log.class.getName()); public static void info (String message) { Log.info(message); } public static void error (String message) { Log.error(message); } public static void debug (String message) { Log.debug(message); } }
Test class or Test case
TestSample.java
package testNGListener.Logger; import org.testng.SkipException; import org.testng.annotations.Test; public class TestSample { @Test(description = "Desc - Passing scenario") public void passTestMethod() { Log.info("Inside passTestMethod"); } @Test(description = "Desc - Failing scenario") public void failTestMethod() { int a = 9/0; } @Test(description = "Desc - Skipping scenario") public void skipTestMethod() { throw new SkipException("Intentionally skipping"); } }
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <listeners> <listener class-name="testNGListener.Logger.TestListeners" /> </listeners> <test name="Regression1" > <classes> <class name="testNGListener.Logger.TestSample" /> </classes> </test> </suite>
Run testng.xml, a folder “Log4j” will be created under current project with application.log file
2018-08-17 17:26:24 INFO Log:12 - =========== onStart :-Regression1=============== 2018-08-17 17:26:24 INFO Log:12 - failTestMethod Started 2018-08-17 17:26:24 INFO Log:12 - Desc - Failing scenario 2018-08-17 17:26:24 INFO Log:12 - Failed because of - java.lang.ArithmeticException: / by zero 2018-08-17 17:26:24 INFO Log:12 - passTestMethod Started 2018-08-17 17:26:24 INFO Log:12 - Desc - Passing scenario 2018-08-17 17:26:24 INFO Log:12 - passTestMethod Passed 2018-08-17 17:26:24 INFO Log:12 - skipTestMethod Started 2018-08-17 17:26:24 INFO Log:12 - Desc - Skipping scenario 2018-08-17 17:26:24 INFO Log:12 - Skipped because of - org.testng.SkipException: Intentionally skipping 2018-08-17 17:26:25 INFO Log:12 - =========== onFinish :-Regression1===============
References
For more info on log4j, you can refer http://www.mkyong.com/logging/log4j-hello-world-example/