Best Testng code snippet using org.testng.reporters.VerboseReporter.log
Source:VerboseReporter.java
...35 * @author Lukas Jungmann36 * @since 6.437 * 38 * THIS IS AN EXACT COPY OF THE CLASS IN org.testng.reporters39 * EXCEPT FOR THIS COMMENT AND log(String) CHANGED TO BE PROTECTED40 */41public class VerboseReporter extends TestListenerAdapter {42 /**43 * Default prefix for messages printed out by this reporter44 *45 */46 public static final String LISTENER_PREFIX = "[VerboseTestNG] ";47 private String suiteName;48 private final String prefix;49 private enum Status {50 SUCCESS(ITestResult.SUCCESS), FAILURE(ITestResult.FAILURE), SKIP(ITestResult.SKIP),51 SUCCESS_PERCENTAGE_FAILURE(ITestResult.SUCCESS_PERCENTAGE_FAILURE), STARTED(ITestResult.STARTED);52 private final int code;53 private Status(int i) {54 code = i;55 }56 57 @SuppressWarnings("unused")58 public int getCode() {59 return code;60 }61 }62 /**63 * Default constructor64 */65 public VerboseReporter() {66 this(LISTENER_PREFIX);67 }68 /**69 * Create VerboseReporter with custom prefix70 *71 * @param prefix prefix for messages printed out by this reporter72 */73 public VerboseReporter(String prefix) {74 this.prefix = prefix;75 }76 @Override77 public void beforeConfiguration(ITestResult tr) {78 super.beforeConfiguration(tr);79 logTestResult(Status.STARTED, tr, true);80 }81 @Override82 public void onConfigurationFailure(ITestResult tr) {83 super.onConfigurationFailure(tr);84 logTestResult(Status.FAILURE, tr, true);85 }86 @Override87 public void onConfigurationSkip(ITestResult tr) {88 super.onConfigurationSkip(tr);89 logTestResult(Status.SKIP, tr, true);90 }91 @Override92 public void onConfigurationSuccess(ITestResult tr) {93 super.onConfigurationSuccess(tr);94 logTestResult(Status.SUCCESS, tr, true);95 }96 @Override97 public void onTestStart(ITestResult tr) {98 logTestResult(Status.STARTED, tr, false);99 }100 @Override101 public void onTestFailure(ITestResult tr) {102 super.onTestFailure(tr);103 logTestResult(Status.FAILURE, tr, false);104 }105 @Override106 public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {107 super.onTestFailedButWithinSuccessPercentage(tr);108 logTestResult(Status.SUCCESS_PERCENTAGE_FAILURE, tr, false);109 }110 @Override111 public void onTestSkipped(ITestResult tr) {112 super.onTestSkipped(tr);113 logTestResult(Status.SKIP, tr, false);114 }115 @Override116 public void onTestSuccess(ITestResult tr) {117 super.onTestSuccess(tr);118 logTestResult(Status.SUCCESS, tr, false);119 }120 @Override121 public void onStart(ITestContext ctx) {122 suiteName = ctx.getName();//ctx.getSuite().getXmlSuite().getFileName();123 log("RUNNING: Suite: \"" + suiteName + "\" containing \"" + ctx.getAllTestMethods().length + "\" Tests (config: " + ctx.getSuite().getXmlSuite().getFileName() + ")");124 }125 @Override126 public void onFinish(ITestContext context) {127 logResults();128 suiteName = null;129 }130 private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {131 ITestNGMethod[] result = new ITestNGMethod[results.size()];132 int i = 0;133 for (ITestResult tr : results) {134 result[i++] = tr.getMethod();135 }136 return result;137 }138 /**139 * Print out test summary140 */141 private void logResults() {142 //143 // Log test summary144 //145 ITestNGMethod[] ft = resultsToMethods(getFailedTests());146 StringBuilder sb = new StringBuilder("\n===============================================\n");147 sb.append(" ").append(suiteName).append("\n");148 sb.append(" Tests run: ").append(getAllTestMethods().length);149 sb.append(", Failures: ").append(ft.length);150 sb.append(", Skips: ").append(resultsToMethods(getSkippedTests()).length);151 int confFailures = getConfigurationFailures().size();152 int confSkips = getConfigurationSkips().size();153 if (confFailures > 0 || confSkips > 0) {154 sb.append("\n").append(" Configuration Failures: ").append(confFailures);155 sb.append(", Skips: ").append(confSkips);156 }157 sb.append("\n===============================================");158 log(sb.toString());159 }160 /**161 * Log meaningful message for passed in arguments.162 * Message itself is of form:163 * $status: "$suiteName" - $methodDeclaration ($actualArguments) finished in $x ms ($run of $totalRuns)164 *165 * @param st status of passed in itr166 * @param itr test result to be described167 * @param isConfMethod is itr describing configuration method168 */169 private void logTestResult(Status st, ITestResult itr, boolean isConfMethod) {170 StringBuilder sb = new StringBuilder();171 String stackTrace = "";172 switch (st) {173 case STARTED:174 sb.append("INVOKING");175 break;176 case SKIP:177 sb.append("SKIPPED");178 stackTrace = itr.getThrowable() != null179 ? Utils.shortStackTrace(itr.getThrowable(), false) : "";180 break;181 case FAILURE:182 sb.append("FAILED");183 stackTrace = itr.getThrowable() != null184 ? Utils.shortStackTrace(itr.getThrowable(), false) : "";185 break;186 case SUCCESS:187 sb.append("PASSED");188 break;189 case SUCCESS_PERCENTAGE_FAILURE:190 sb.append("PASSED with failures");191 break;192 default:193 //not happen194 throw new RuntimeException("Unsupported test status:" + itr.getStatus());195 }196 if (isConfMethod) {197 sb.append(" CONFIGURATION: ");198 } else {199 sb.append(": ");200 }201 ITestNGMethod tm = itr.getMethod();202 int identLevel = sb.length();203 sb.append(getMethodDeclaration(tm));204 Object[] params = itr.getParameters();205 Class<?>[] paramTypes = tm.getConstructorOrMethod().getParameterTypes();206 if (null != params && params.length > 0) {207 // The error might be a data provider parameter mismatch, so make208 // a special case here209 if (params.length != paramTypes.length) {210 sb.append("Wrong number of arguments were passed by the Data Provider: found ");211 sb.append(params.length);212 sb.append(" but expected ");213 sb.append(paramTypes.length);214 } else {215 sb.append("(value(s): ");216 for (int i = 0; i < params.length; i++) {217 if (i > 0) {218 sb.append(", ");219 }220 sb.append(Utils.toString(params[i], paramTypes[i]));221 }222 sb.append(")");223 }224 }225 if (Status.STARTED != st) {226 sb.append(" finished in ");227 sb.append(itr.getEndMillis() - itr.getStartMillis());228 sb.append(" ms");229 if (!Utils.isStringEmpty(tm.getDescription())) {230 sb.append("\n");231 for (int i = 0; i < identLevel; i++) {232 sb.append(" ");233 }234 sb.append(tm.getDescription());235 }236 if (tm.getInvocationCount() > 1) {237 sb.append(" (");238 sb.append(tm.getCurrentInvocationCount());239 sb.append(" of ");240 sb.append(tm.getInvocationCount());241 sb.append(")");242 }243 if (!Utils.isStringEmpty(stackTrace)) {244 sb.append("\n").append(stackTrace.substring(0, stackTrace.lastIndexOf(System.getProperty("line.separator"))));245 }246 } else {247 if (!isConfMethod && tm.getInvocationCount() > 1) {248 sb.append(" success: ");249 sb.append(tm.getSuccessPercentage());250 sb.append("%");251 }252 }253 log(sb.toString());254 }255 protected void log(String message) {256 //prefix all output lines257 System.out.println(message.replaceAll("(?m)^", prefix));258 }259 /**260 *261 * @param method method to be described262 * @return FQN of a class + method declaration for a method passed in263 * ie. test.triangle.CheckCount.testCheckCount(java.lang.String)264 */265 private String getMethodDeclaration(ITestNGMethod method) {266 //see Utils.detailedMethodName267 //perhaps should rather adopt the original method instead268 ConstructorOrMethod m = method.getConstructorOrMethod();269 StringBuilder buf = new StringBuilder();...
Source:LoggingReporter.java
...8 * Contributors:9 * IBM Corporation - initial API and implementation10 *******************************************************************************/11package com.ibm.ws.microprofile.reactive.messaging.fat.kafka.tck;12import java.util.logging.Logger;13import org.testng.reporters.VerboseReporter;14/**15 * TestNG reporter that logs using java.util.logging16 */17public class LoggingReporter extends VerboseReporter {18 private static final Logger LOGGER = Logger.getLogger(LoggingReporter.class.getName());19 public LoggingReporter() {20 super(""); // No message prefix21 }22 @Override23 protected void log(String message) {24 LOGGER.info(message);25 }26}...
Source:LogTestStatusListener.java
...3import org.slf4j.LoggerFactory;4import org.testng.ITestResult;5import org.testng.reporters.VerboseReporter;6public class LogTestStatusListener extends VerboseReporter {7 private static final Logger log = LoggerFactory.getLogger(LogTestStatusListener.class);8 public LogTestStatusListener() {9 super("");10 }11 @Override12 public void onTestSuccess(ITestResult tr) {13 super.onTestSuccess(tr);14 if (tr.getThrowable() != null) {15 log.trace("vvv Test successfully throw exception vvv", tr.getThrowable());16 log.trace("^^^ Test successfully throw exception ^^^");17 }18 }19 @Override20 protected void log(String message) {21 log.trace(message);22 }23}...
Source:VerboseLogReporter.java
...7 VerboseLogReporter() {8 super("[TestNG] ");9 }10 @Override11 protected void log(String message) {12 LOG.info(message.replaceAll("(?m)^", ""));13 }14}...
log
Using AI Code Generation
1import org.testng.reporters.VerboseReporter2new VerboseReporter().log("Hello world")3import org.slf4j.Logger4import org.slf4j.LoggerFactory5LoggerFactory.getLogger("test").log("Hello world")6import org.apache.log4j.Logger7Logger.getLogger("test").log("Hello world")8import org.apache.logging.log4j.Logger9import org.apache.logging.log4j.LogManager10LogManager.getLogger("test").log("Hello world")11import org.apache.commons.logging.Log12import org.apache.commons.logging.LogFactory13LogFactory.getLog("test").log("Hello world")14import org.apache.commons.logging.Log15import org.apache.commons.logging.LogFactory16LogFactory.getLog("test").log("Hello world")17import org.apache.commons.logging.Log18import org.apache.commons.logging.LogFactory19LogFactory.getLog("test").log("Hello world")20import org.apache.commons.logging.Log21import org.apache.commons.logging.LogFactory22LogFactory.getLog("test").log("Hello world")23import org.apache.commons.logging.Log24import org.apache.commons.logging.LogFactory25LogFactory.getLog("test").log("Hello world")26import org.apache.commons.logging.Log27import org.apache.commons.logging.LogFactory28LogFactory.getLog("test").log("Hello world")29import org.apache.commons.logging.Log30import org.apache.commons.logging.LogFactory31LogFactory.getLog("test").log("Hello world")32import org.apache.commons.logging.Log33import org.apache.commons.logging.LogFactory34LogFactory.getLog("test").log("Hello world")35import org.apache.commons.logging.Log36import org.apache.commons.logging.LogFactory37LogFactory.getLog("test").log("Hello world")
log
Using AI Code Generation
1import org.testng.reporters.VerboseReporter;2public class TestNGLogger {3 public static void log(String message) {4 VerboseReporter.log(message);5 }6}7import org.testng.reporters.VerboseReporter;8public class TestNGLogger {9 public static void log(String message) {10 VerboseReporter.log(message);11 }12}13import org.testng.reporters.VerboseReporter;14public class TestNGLogger {15 public static void log(String message) {16 VerboseReporter.log(message);17 }18}19import org.testng.reporters.VerboseReporter;20public class TestNGLogger {21 public static void log(String message) {22 VerboseReporter.log(message);23 }24}25import org.testng.reporters.VerboseReporter;26public class TestNGLogger {27 public static void log(String message) {28 VerboseReporter.log(message);29 }30}31import org.testng.reporters.VerboseReporter;32public class TestNGLogger {33 public static void log(String message) {34 VerboseReporter.log(message);35 }36}37import org.testng.reporters.VerboseReporter;38public class TestNGLogger {39 public static void log(String message) {40 VerboseReporter.log(message);41 }42}43import org.testng.reporters.VerboseReporter;44public class TestNGLogger {45 public static void log(String message) {46 VerboseReporter.log(message);47 }48}
log
Using AI Code Generation
1import org.testng.reporters.VerboseReporter;2VerboseReporter.log("This is a test message");3import org.testng.reporters.VerboseReporter;4VerboseReporter.log("This is a test message");5import org.testng.reporters.VerboseReporter;6VerboseReporter.log("This is a test message");7import org.testng.reporters.VerboseReporter;8VerboseReporter.log("This is a test message");9import org.testng.reporters.VerboseReporter;10VerboseReporter.log("This is a test message");11import org.testng.reporters.VerboseReporter;12VerboseReporter.log("This is a test message");13import org.testng.reporters.VerboseReporter;14VerboseReporter.log("This is a test message");15import org.testng.reporters.VerboseReporter;16VerboseReporter.log("This is a test message");17import org.testng.reporters.VerboseReporter;18VerboseReporter.log("This is a test message");19import org.testng.reporters.VerboseReporter;20VerboseReporter.log("This is a test message");21import org.testng.reporters.VerboseReporter;22VerboseReporter.log("This is a test message");23import org.testng.reporters.VerboseReporter;24VerboseReporter.log("This is a test message");
log
Using AI Code Generation
1log("This is a log message");2Logger.getLogger(this.getClass()).debug("This is a log message");3Logger.getLogger(this.getClass()).log(Level.FATAL, "This is a fatal log message");4Logger.getLogger(this.getClass()).log(Level.ERROR, "This is an error log message");5Logger.getLogger(this.getClass()).log(Level.WARN, "This is a warning log message");6Logger.getLogger(this.getClass()).log(Level.INFO, "This is an info log message");7Logger.getLogger(this.getClass()).log(Level.DEBUG, "This is a debug log message");8Logger.getLogger(this.getClass()).log(Level.TRACE, "This is a trace log message");9Logger.getLogger(this.getClass()).log(Level.FATAL, "This is a fatal log message");10Logger.getLogger(this.getClass()).log(Level.ERROR, "This is an error log message");11Logger.getLogger(this.getClass()).log(Level.WARN, "This is a warning log message");12Logger.getLogger(this.getClass()).log(Level.INFO, "This is an info log message");13Logger.getLogger(this.getClass()).log(Level.DEBUG, "This is a debug log message");14Logger.getLogger(this.getClass()).log(Level.TRACE, "This is a trace log message");
log
Using AI Code Generation
1import org.testng.reporters.VerboseReporter;2VerboseReporter.log("This is a test message");3import org.testng.reporters.VerboseReporter;4VerboseReporter.log("This is a test message");5import org.testng.reporters.VerboseReporter;6VerboseReporter.log("This is a test message");7import org.testng.reporters.VerboseReporter;8VerboseReporter.log("This is a test message");9import org.testng.reporters.VerboseReporter;10VerboseReporter.log("This is a test message");11import org.testng.reporters.VerboseReporter;12VerboseReporter.log("This is a test message");13import org.testng.reporters.VerboseReporter;14VerboseReporter.log("This is a test message");15import org.testng.reporters.VerboseReporter;16VerboseReporter.log("This is a test message");17import org.testng.reporters.VerboseReporter;18VerboseReporter.log("This is a test message");19import org.testng.reporters.VerboseReporter;20VerboseReporter.log("This is a test message");21import org.testng.reporters.VerboseReporter;22VerboseReporter.log("This is a test message");23import org.testng.reporters.VerboseReporter;24VerboseReporter.log("This is a test message");
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!