Best Testng code snippet using org.testng.SuiteRunner.addListener
Source:SuiteRunner.java
...148 tr.addMethodInterceptor(methodInterceptor);149 }150 // Reuse the same text reporter so we can accumulate all the results151 // (this is used to display the final suite report at the end)152 tr.addListener(m_textReporter);153 m_testRunners.add(tr);154 // Add the methods found in this test to our global count155 m_allTestMethods.addAll(Arrays.asList(tr.getAllTestMethods()));156 }157 }158 @Override159 public XmlSuite getXmlSuite() {160 return m_suite;161 }162 @Override163 public String getName() {164 return m_suite.getName();165 }166 public void setObjectFactory(ITestObjectFactory objectFactory) {167 m_objectFactory = objectFactory;168 }169 public void setReportResults(boolean reportResults) {170 m_useDefaultListeners = reportResults;171 }172 private void invokeListeners(boolean start) {173 for (ISuiteListener sl : m_listeners.values()) {174 if (start) {175 sl.onStart(this);176 }177 else {178 sl.onFinish(this);179 }180 }181 }182 private void setOutputDir(String outputdir) {183 if (isStringBlank(outputdir) && m_useDefaultListeners) {184 outputdir = DEFAULT_OUTPUT_DIR;185 }186 m_outputDir = (null != outputdir) ? new File(outputdir).getAbsolutePath()187 : null;188 }189 private ITestRunnerFactory buildRunnerFactory() {190 ITestRunnerFactory factory = null;191 if (null == m_tmpRunnerFactory) {192 factory = new DefaultTestRunnerFactory(m_configuration,193 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),194 m_useDefaultListeners, m_skipFailedInvocationCounts);195 }196 else {197 factory = new ProxyTestRunnerFactory(198 m_testListeners.toArray(new ITestListener[m_testListeners.size()]),199 m_tmpRunnerFactory);200 }201 return factory;202 }203 @Override204 public String getParallel() {205 return m_suite.getParallel().toString();206 }207 public String getParentModule() {208 return m_suite.getParentModule();209 }210 @Override211 public String getGuiceStage() {212 return m_suite.getGuiceStage();213 }214 public Injector getParentInjector() {215 return m_parentInjector;216 }217 public void setParentInjector(Injector injector) {218 m_parentInjector = injector;219 }220 @Override221 public void run() {222 invokeListeners(true /* start */);223 try {224 privateRun();225 }226 finally {227 invokeListeners(false /* stop */);228 }229 }230 private void privateRun() {231 // Map for unicity, Linked for guaranteed order232 Map<Method, ITestNGMethod> beforeSuiteMethods= new LinkedHashMap<>();233 Map<Method, ITestNGMethod> afterSuiteMethods = new LinkedHashMap<>();234 IInvoker invoker = null;235 // Get the invoker and find all the suite level methods236 for (TestRunner tr: m_testRunners) {237 // TODO: Code smell. Invoker should belong to SuiteRunner, not TestRunner238 // -- cbeust239 invoker = tr.getInvoker();240 for (ITestNGMethod m : tr.getBeforeSuiteMethods()) {241 beforeSuiteMethods.put(m.getMethod(), m);242 }243 for (ITestNGMethod m : tr.getAfterSuiteMethods()) {244 afterSuiteMethods.put(m.getMethod(), m);245 }246 }247 //248 // Invoke beforeSuite methods (the invoker can be null249 // if the suite we are currently running only contains250 // a <file-suite> tag and no real tests)251 //252 if (invoker != null) {253 if(beforeSuiteMethods.values().size() > 0) {254 invoker.invokeConfigurations(null,255 beforeSuiteMethods.values().toArray(new ITestNGMethod[beforeSuiteMethods.size()]),256 m_suite, m_suite.getParameters(), null, /* no parameter values */257 null /* instance */258 );259 }260 Utils.log("SuiteRunner", 3, "Created " + m_testRunners.size() + " TestRunners");261 //262 // Run all the test runners263 //264 boolean testsInParallel = XmlSuite.ParallelMode.TESTS.equals(m_suite.getParallel());265 if (!testsInParallel) {266 runSequentially();267 }268 else {269 runInParallelTestMode();270 }271// SuitePlan sp = new SuitePlan();272// for (TestRunner tr : m_testRunners) {273// sp.addTestPlan(tr.getTestPlan());274// }275// sp.dump();276 //277 // Invoke afterSuite methods278 //279 if (afterSuiteMethods.values().size() > 0) {280 invoker.invokeConfigurations(null,281 afterSuiteMethods.values().toArray(new ITestNGMethod[afterSuiteMethods.size()]),282 m_suite, m_suite.getAllParameters(), null, /* no parameter values */283 null /* instance */);284 }285 }286 }287 private List<IReporter> m_reporters = Lists.newArrayList();288 private void addReporter(IReporter listener) {289 m_reporters.add(listener);290 }291 void addConfigurationListener(IConfigurationListener listener) {292 m_configuration.addConfigurationListener(listener);293 }294 public List<IReporter> getReporters() {295 return m_reporters;296 }297 private void runSequentially() {298 for (TestRunner tr : m_testRunners) {299 runTest(tr);300 }301 }302 private void runTest(TestRunner tr) {303 tr.run();304 ISuiteResult sr = new SuiteResult(m_suite, tr);305 m_suiteResults.put(tr.getName(), sr);306 }307 /**308 * Implement <suite parallel="tests">.309 * Since this kind of parallelism happens at the suite level, we need a special code path310 * to execute it. All the other parallelism strategies are implemented at the test level311 * in TestRunner#createParallelWorkers (but since this method deals with just one <test>312 * tag, it can't implement <suite parallel="tests">, which is why we're doing it here).313 */314 private void runInParallelTestMode() {315 List<Runnable> tasks= Lists.newArrayList(m_testRunners.size());316 for(TestRunner tr: m_testRunners) {317 tasks.add(new SuiteWorker(tr));318 }319 ThreadUtil.execute(tasks, m_suite.getThreadCount(),320 m_suite.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS), false);321 }322 private class SuiteWorker implements Runnable {323 private TestRunner m_testRunner;324 public SuiteWorker(TestRunner tr) {325 m_testRunner = tr;326 }327 @Override328 public void run() {329 Utils.log("[SuiteWorker]", 4, "Running XML Test '"330 + m_testRunner.getTest().getName() + "' in Parallel");331 runTest(m_testRunner);332 }333 }334 /**335 * Registers ISuiteListeners interested in reporting the result of the current336 * suite.337 *338 * @param reporter339 */340 protected void addListener(ISuiteListener reporter) {341 m_listeners.put(reporter.getClass(), reporter);342 }343 @Override344 public void addListener(ITestNGListener listener) {345 if (listener instanceof IInvokedMethodListener) {346 IInvokedMethodListener invokedMethodListener = (IInvokedMethodListener) listener;347 m_invokedMethodListeners.put(invokedMethodListener.getClass(), invokedMethodListener);348 }349 if (listener instanceof ISuiteListener) {350 addListener((ISuiteListener) listener);351 }352 if (listener instanceof IReporter) {353 addReporter((IReporter) listener);354 }355 if (listener instanceof IConfigurationListener) {356 addConfigurationListener((IConfigurationListener) listener);357 }358 }359 @Override360 public String getOutputDirectory() {361 return m_outputDir + File.separatorChar + getName();362 }363 @Override364 public Map<String, ISuiteResult> getResults() {365 return m_suiteResults;366 }367 /**368 * FIXME: should be removed?369 *370 * @see org.testng.ISuite#getParameter(java.lang.String)371 */372 @Override373 public String getParameter(String parameterName) {374 return m_suite.getParameter(parameterName);375 }376 /**377 * @see org.testng.ISuite#getMethodsByGroups()378 */379 @Override380 public Map<String, Collection<ITestNGMethod>> getMethodsByGroups() {381 Map<String, Collection<ITestNGMethod>> result = Maps.newHashMap();382 for (TestRunner tr : m_testRunners) {383 ITestNGMethod[] methods = tr.getAllTestMethods();384 for (ITestNGMethod m : methods) {385 String[] groups = m.getGroups();386 for (String groupName : groups) {387 Collection<ITestNGMethod> testMethods = result.get(groupName);388 if (null == testMethods) {389 testMethods = Lists.newArrayList();390 result.put(groupName, testMethods);391 }392 testMethods.add(m);393 }394 }395 }396 return result;397 }398 /**399 * @see org.testng.ISuite#getInvokedMethods()400 */401 @Override402 public Collection<ITestNGMethod> getInvokedMethods() {403 return getIncludedOrExcludedMethods(true /* included */);404 }405 /**406 * @see org.testng.ISuite#getExcludedMethods()407 */408 @Override409 public Collection<ITestNGMethod> getExcludedMethods() {410 return getIncludedOrExcludedMethods(false/* included */);411 }412 private Collection<ITestNGMethod> getIncludedOrExcludedMethods(boolean included) {413 List<ITestNGMethod> result= Lists.newArrayList();414 for (TestRunner tr : m_testRunners) {415 Collection<ITestNGMethod> methods = included ? tr.getInvokedMethods() : tr.getExcludedMethods();416 for (ITestNGMethod m : methods) {417 result.add(m);418 }419 }420 return result;421 }422 @Override423 public IObjectFactory getObjectFactory() {424 return m_objectFactory instanceof IObjectFactory ? (IObjectFactory) m_objectFactory : null;425 }426 @Override427 public IObjectFactory2 getObjectFactory2() {428 return m_objectFactory instanceof IObjectFactory2 ? (IObjectFactory2) m_objectFactory : null;429 }430 /**431 * Returns the annotation finder for the given annotation type.432 * @return the annotation finder for the given annotation type.433 */434 @Override435 public IAnnotationFinder getAnnotationFinder() {436 return m_configuration.getAnnotationFinder();437 }438 public static void ppp(String s) {439 System.out.println("[SuiteRunner] " + s);440 }441 /**442 * The default implementation of {@link ITestRunnerFactory}.443 */444 private static class DefaultTestRunnerFactory implements ITestRunnerFactory {445 private ITestListener[] m_failureGenerators;446 private boolean m_useDefaultListeners;447 private boolean m_skipFailedInvocationCounts;448 private IConfiguration m_configuration;449 public DefaultTestRunnerFactory(IConfiguration configuration,450 ITestListener[] failureListeners,451 boolean useDefaultListeners,452 boolean skipFailedInvocationCounts)453 {454 m_configuration = configuration;455 m_failureGenerators = failureListeners;456 m_useDefaultListeners = useDefaultListeners;457 m_skipFailedInvocationCounts = skipFailedInvocationCounts;458 }459 @Override460 public TestRunner newTestRunner(ISuite suite, XmlTest test,461 Collection<IInvokedMethodListener> listeners, List<IClassListener> classListeners) {462 boolean skip = m_skipFailedInvocationCounts;463 if (! skip) {464 skip = test.skipFailedInvocationCounts();465 }466 TestRunner testRunner = new TestRunner(m_configuration, suite, test,467 suite.getOutputDirectory(), suite.getAnnotationFinder(), skip,468 listeners, classListeners);469 if (m_useDefaultListeners) {470 testRunner.addListener(new TestHTMLReporter());471 testRunner.addListener(new JUnitXMLReporter());472 //TODO: Moved these here because maven2 has output reporters running473 //already, the output from these causes directories to be created with474 //files. This is not the desired behaviour of running tests in maven2.475 //Don't know what to do about this though, are people relying on these476 //to be added even with defaultListeners set to false?477 testRunner.addListener(new TextReporter(testRunner.getName(), TestRunner.getVerbose()));478 }479 for (ITestListener itl : m_failureGenerators) {480 testRunner.addListener(itl);481 }482 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {483 testRunner.addConfigurationListener(cl);484 }485 return testRunner;486 }487 }488 private static class ProxyTestRunnerFactory implements ITestRunnerFactory {489 private ITestListener[] m_failureGenerators;490 private ITestRunnerFactory m_target;491 public ProxyTestRunnerFactory(ITestListener[] failureListeners, ITestRunnerFactory target) {492 m_failureGenerators = failureListeners;493 m_target= target;494 }495 @Override496 public TestRunner newTestRunner(ISuite suite, XmlTest test,497 Collection<IInvokedMethodListener> listeners, List<IClassListener> classListeners) {498 TestRunner testRunner= m_target.newTestRunner(suite, test, listeners, classListeners);499 testRunner.addListener(new TextReporter(testRunner.getName(), TestRunner.getVerbose()));500 for (ITestListener itl : m_failureGenerators) {501 testRunner.addListener(itl);502 }503 return testRunner;504 }505 }506 public void setHost(String host) {507 m_host = host;508 }509 @Override510 public String getHost() {511 return m_host;512 }513 private SuiteRunState m_suiteState= new SuiteRunState();514 /**515 * @see org.testng.ISuite#getSuiteState()...
Source:SuiteRunnerWorker.java
...44 SuiteRunner suiteRunner = (SuiteRunner) suiteRunnerMap.get(xmlSuite);45 suiteRunner.run();46 //TODO: this should be handled properly47 // for (IReporter r : suiteRunner.getReporters()) {48 // addListener(r);49 // }50 // PoolService.getInstance().shutdown();51 //52 // Display the final statistics53 //54 if (xmlSuite.getVerbose() > 0) {55 SuiteResultCounts counts = new SuiteResultCounts();56 synchronized (suiteRunnerMap) {57 counts.calculateResultCounts(xmlSuite, suiteRunnerMap);58 }59 StringBuffer bufLog = new StringBuffer("\n===============================================\n")60 .append(xmlSuite.getName());61 bufLog.append("\nTotal tests run: ")62 .append(counts.m_total).append(", Failures: ").append(counts.m_failed)...
Source:ParamInheritanceTest.java
...18 TestListenerAdapter tla = new TestListenerAdapter();19 TestNG tng = create();20 tng.setTestSuites(Arrays.asList(getPathToResource("param-inheritance/parent-suite.xml")));21 tng.setVerbose(2);22 tng.addListener(tla);23 OutputStream os = new ByteArrayOutputStream();24 PrintStream out = System.out;25 PrintStream err = System.err;26 try {27 /*28 * Changing system print streams so that exception or results stmt is not logged29 * while running test (avoid confusing person running tests)30 */31 System.setOut(new PrintStream(os));32 System.setErr(new PrintStream(os));33 tng.run();34 Assert.assertEquals(tla.getPassedTests().size(), 1);35 }36 finally {37 try {38 os.close();39 }40 catch (IOException e) {41 //no need to handle this42 }43 System.setOut(out);44 System.setErr(err);45 }46 }47 @Test(description = "Checks to make sure parameters are inherited and overridden properly")48 public void parameterInheritanceAndOverriding() {49 TestListenerAdapter tla = new TestListenerAdapter();50 TestNG tng = create();51 tng.setUseDefaultListeners(false);52 tng.setTestSuites(Arrays.asList(getPathToResource("parametertest/parent-suite.xml")));53 tng.addListener(tla);54 tng.run();55 Assert.assertEquals(tla.getPassedTests().size(), 3);56 }57}...
Source:43b55.java
...5@@ -569,7 +569,7 @@6 }7 8 for (ITestListener itl : m_failureGenerators) {9- testRunner.addListener(itl);10+ testRunner.addTestListener(itl);11 }12 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {13 testRunner.addConfigurationListener(cl);...
addListener
Using AI Code Generation
1package com.test;2import org.testng.ITestContext;3import org.testng.ITestListener;4import org.testng.ITestResult;5public class TestNGListner implements ITestListener {6 public void onFinish(ITestContext arg0) {7 System.out.println("Test Finished");8 }9 public void onStart(ITestContext arg0) {10 System.out.println("Test Started");11 }12 public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {13 System.out.println("Test Failed But Within Success Percentage");14 }15 public void onTestFailure(ITestResult arg0) {16 System.out.println("Test Failed");17 }18 public void onTestSkipped(ITestResult arg0) {19 System.out.println("Test Skipped");20 }21 public void onTestStart(ITestResult arg0) {22 System.out.println("Test Started");23 }24 public void onTestSuccess(ITestResult arg0) {25 System.out.println("Test Success");26 }27}28package com.test;29import org.testng.annotations.Listeners;30import org.testng.annotations.Test;31@Listeners(TestNGListner.class)32public class TestNGListnerDemo {33 public void test1() {34 System.out.println("Test1");35 }36 public void test2() {37 System.out.println("Test2");38 }39}40onTestFailedButWithinSuccessPercentage(ITestResult result): This method is
addListener
Using AI Code Generation
1import org.testng.SuiteRunner;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import java.util.List;5public class TestNGListenerExample {6 public static void main(String[] args) {7 TestNG testNG = new TestNG();8 testNG.setTestSuites(List.of("src/test/resources/suites/testng.xml"));9 testNG.addListener(new MyTestListener());10 testNG.run();11 }12}13import org.testng.TestNG;14import org.testng.xml.XmlSuite;15import java.util.List;16public class TestNGListenerExample {17 public static void main(String[] args) {18 TestNG testNG = new TestNG();19 testNG.setTestSuites(List.of("src/test/resources/suites/testng.xml"));20 testNG.addListener(new MyTestListener());21 testNG.run();22 }23}24import org.testng.TestNG;25import org.testng.xml.XmlSuite;26import java.util.List;27public class TestNGListenerExample {28 public static void main(String[] args) {29 TestNG testNG = new TestNG();30 testNG.setTestSuites(List.of("src/test/resources/suites/testng.xml"));31 testNG.addListener(new MyTestListener());32 testNG.run();33 }34}35import org.testng.TestNG;36import org.testng.xml.XmlSuite;37import java.util.List;38public class TestNGListenerExample {39 public static void main(String[] args) {40 TestNG testNG = new TestNG();41 testNG.setTestSuites(List.of("src/test/resources/suites/testng.xml"));42 testNG.addListener(new MyTestListener());43 testNG.run();44 }45}46import org.testng.TestNG;47import org.testng.xml.XmlSuite;48import java.util.List;49public class TestNGListenerExample {50 public static void main(String[] args) {51 TestNG testNG = new TestNG();52 testNG.setTestSuites(List.of("src/test/resources/suites/testng.xml"));53 testNG.addListener(new MyTestListener());54 testNG.run();55 }56}57import org.testng.TestNG;58import org.testng
addListener
Using AI Code Generation
1package org.testng;2import java.util.List;3import java.util.ArrayList;4import java.util.Iterator;5import java.util.Arrays;6import java.util.Map;7import java.util.HashMap;8import java.util.Set;9import java.util.HashSet;10import java.lang.reflect.Method;11import java.lang.reflect.InvocationTargetException;12import java.lang.reflect.Constructor;13import org.testng.collections.Lists;14import org.testng.collections.Maps;15import org.testng.collections.Sets;16import org.testng.internal.ClassHelper;17import org.testng.internal.ClassImpl;18import org.testng.internal.ConstructorOrMethod;19import org.testng.internal.DynamicGraph;20import org.testng.internal.DynamicGraph.Status;21import org.testng.internal.IConstructorOrMethodFinder;22import org.testng.internal.IResultListener;23import org.testng.internal.IResultListener2;24import org.testng.internal.Invoker;25import org.testng.internal.MethodHelper;26import org.testng.internal.MethodInstance;27import org.testng.internal.MethodMatcher;28import org.testng.internal.ParameterHandler;29import org.testng.internal.Parameters;30import org.testng.internal.Parameters.IParameterProvider;31import org.testng.internal.Parameters.Parameter;32import org.testng.internal.Parameters.ParameterHolder;33import org.testng.internal.Parameters.ParameterIndex;34import org.testng.internal.Parameters.ParameterValue;35import org.testng.internal.Parameters.ParameterValueHolder;36import org.testng.internal.ParameterMap;37import org.testng.internal.RuntimeBehavior;38import org.testng.internal.TestNGClassFinder;39import org.testng.internal.Utils;40import
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!!