Best Testng code snippet using org.testng.TestNG.setListenersToSkipFromBeingWiredInViaServiceLoaders
Source:TestNG.java
...193 /**194 * @param listeners - An array of fully qualified class names that should be skipped from being195 * wired in via service loaders.196 */197 public void setListenersToSkipFromBeingWiredInViaServiceLoaders(String... listeners) {198 m_listenersToSkipFromBeingWiredIn.addAll(Arrays.asList(listeners));199 }200 public int getStatus() {201 if (exitCodeListener.noTestsFound()) {202 return ExitCode.HAS_NO_TEST;203 }204 return exitCode.getExitCode();205 }206 /**207 * Sets the output directory where the reports will be created.208 *209 * @param outputdir The directory.210 */211 public void setOutputDirectory(final String outputdir) {212 if (isStringNotEmpty(outputdir)) {213 m_outputDir = outputdir;214 }215 }216 /**217 * @param useDefaultListeners If true before run(), the default listeners will not be used.218 *219 * <ul>220 * <li>org.testng.reporters.TestHTMLReporter221 * <li>org.testng.reporters.JUnitXMLReporter222 * <li>org.testng.reporters.XMLReporter223 * </ul>224 *225 * @see org.testng.reporters.TestHTMLReporter226 * @see org.testng.reporters.JUnitXMLReporter227 * @see org.testng.reporters.XMLReporter228 */229 public void setUseDefaultListeners(boolean useDefaultListeners) {230 m_useDefaultListeners = useDefaultListeners;231 }232 /**233 * Sets a jar containing a testng.xml file.234 *235 * @param jarPath - Path of the jar236 */237 public void setTestJar(String jarPath) {238 m_jarPath = jarPath;239 }240 /** @param xmlPathInJar Sets the path to the XML file in the test jar file. */241 public void setXmlPathInJar(String xmlPathInJar) {242 m_xmlPathInJar = xmlPathInJar;243 }244 private void parseSuiteFiles() {245 IPostProcessor processor = getProcessor();246 for (XmlSuite s : m_suites) {247 if (s.isParsed()) {248 continue;249 }250 for (String suiteFile : s.getSuiteFiles()) {251 try {252 String fileNameToUse = s.getFileName();253 if (fileNameToUse == null || fileNameToUse.trim().isEmpty()) {254 fileNameToUse = suiteFile;255 }256 Collection<XmlSuite> childSuites = Parser.parse(fileNameToUse, processor);257 for (XmlSuite cSuite : childSuites) {258 cSuite.setParentSuite(s);259 s.getChildSuites().add(cSuite);260 }261 } catch (IOException e) {262 e.printStackTrace(System.out);263 }264 }265 }266 }267 private OverrideProcessor getProcessor() {268 return new OverrideProcessor(m_includedGroups, m_excludedGroups);269 }270 private void parseSuite(String suitePath) {271 if (LOGGER.isDebugEnabled()) {272 LOGGER.debug("suiteXmlPath: \"" + suitePath + "\"");273 }274 try {275 Collection<XmlSuite> allSuites = Parser.parse(suitePath, getProcessor());276 for (XmlSuite s : allSuites) {277 if (this.m_parallelMode != null) {278 s.setParallel(this.m_parallelMode);279 }280 if (this.m_threadCount > 0) {281 s.setThreadCount(this.m_threadCount);282 }283 if (m_testNames == null) {284 m_suites.add(s);285 continue;286 }287 // If test names were specified, only run these test names288 TestNamesMatcher testNamesMatcher = new TestNamesMatcher(s, m_testNames);289 List<String> missMatchedTestname = testNamesMatcher.getMissMatchedTestNames();290 if (!missMatchedTestname.isEmpty()) {291 throw new TestNGException("The test(s) <" + missMatchedTestname + "> cannot be found.");292 }293 m_suites.addAll(testNamesMatcher.getSuitesMatchingTestNames());294 }295 } catch (IOException e) {296 e.printStackTrace(System.out);297 } catch (Exception ex) {298 // Probably a Yaml exception, unnest it299 Throwable t = ex;300 while (t.getCause() != null) {301 t = t.getCause();302 }303 if (t instanceof TestNGException) {304 throw (TestNGException) t;305 }306 throw new TestNGException(t);307 }308 }309 public void initializeSuitesAndJarFile() {310 // The IntelliJ plug-in might have invoked this method already so don't initialize suites twice.311 if (isSuiteInitialized) {312 return;313 }314 isSuiteInitialized = true;315 if (!m_suites.isEmpty()) {316 parseSuiteFiles(); // to parse the suite files (<suite-file>), if any317 return;318 }319 //320 // Parse the suites that were passed on the command line321 //322 for (String suitePath : m_stringSuites) {323 parseSuite(suitePath);324 }325 //326 // jar path327 //328 // If suites were passed on the command line, they take precedence over the suite file329 // inside that jar path330 if (m_jarPath != null && !m_stringSuites.isEmpty()) {331 StringBuilder suites = new StringBuilder();332 for (String s : m_stringSuites) {333 suites.append(s);334 }335 Utils.log(336 "TestNG",337 2,338 "Ignoring the XML file inside " + m_jarPath + " and using " + suites + " instead");339 return;340 }341 if (isStringEmpty(m_jarPath)) {342 return;343 }344 // We have a jar file and no XML file was specified: try to find an XML file inside the jar345 File jarFile = new File(m_jarPath);346 JarFileUtils utils =347 new JarFileUtils(getProcessor(), m_xmlPathInJar, m_testNames, m_parallelMode);348 m_suites.addAll(utils.extractSuitesFrom(jarFile));349 }350 /** @param threadCount Define the number of threads in the thread pool. */351 public void setThreadCount(int threadCount) {352 if (threadCount < 1) {353 exitWithError("Cannot use a threadCount parameter less than 1; 1 > " + threadCount);354 }355 m_threadCount = threadCount;356 }357 /**358 * @param parallel Define whether this run will be run in parallel mode.359 * @deprecated Use #setParallel(XmlSuite.ParallelMode) instead360 */361 @Deprecated362 // TODO: krmahadevan: This method is being used by Gradle. Removal causes build failures.363 public void setParallel(String parallel) {364 setParallel(XmlSuite.ParallelMode.getValidParallel(parallel));365 }366 public void setParallel(XmlSuite.ParallelMode parallel) {367 m_parallelMode = parallel;368 }369 public void setCommandLineSuite(XmlSuite suite) {370 m_cmdlineSuites = Lists.newArrayList();371 m_cmdlineSuites.add(suite);372 m_suites.add(suite);373 }374 /**375 * Set the test classes to be run by this TestNG object. This method will create a dummy suite376 * that will wrap these classes called "Command Line Test".377 *378 * <p>If used together with threadCount, parallel, groups, excludedGroups than this one must be379 * set first.380 *381 * @param classes An array of classes that contain TestNG annotations.382 */383 public void setTestClasses(Class[] classes) {384 m_suites.clear();385 m_commandLineTestClasses = classes;386 }387 /**388 * Given a string com.example.Foo.f1, return an array where [0] is the class and [1] is the389 * method.390 */391 private String[] splitMethod(String m) {392 int index = m.lastIndexOf(".");393 if (index < 0) {394 throw new TestNGException(395 "Bad format for command line method:" + m + ", expected <class>.<method>");396 }397 return new String[] {m.substring(0, index), m.substring(index + 1).replaceAll("\\*", "\\.\\*")};398 }399 /**400 * @return a list of XmlSuite objects that represent the list of classes and methods passed in401 * parameter.402 * @param commandLineMethods a string with the form "com.example.Foo.f1,com.example.Bar.f2"403 */404 private List<XmlSuite> createCommandLineSuitesForMethods(List<String> commandLineMethods) {405 //406 // Create the <classes> tag407 //408 Set<Class> classes = Sets.newHashSet();409 for (String m : commandLineMethods) {410 Class c = ClassHelper.forName(splitMethod(m)[0]);411 if (c != null) {412 classes.add(c);413 }414 }415 List<XmlSuite> result = createCommandLineSuitesForClasses(classes.toArray(new Class[0]));416 //417 // Add the method tags418 //419 List<XmlClass> xmlClasses = Lists.newArrayList();420 for (XmlSuite s : result) {421 for (XmlTest t : s.getTests()) {422 xmlClasses.addAll(t.getClasses());423 }424 }425 for (XmlClass xc : xmlClasses) {426 for (String m : commandLineMethods) {427 String[] split = splitMethod(m);428 String className = split[0];429 if (xc.getName().equals(className)) {430 XmlInclude includedMethod = new XmlInclude(split[1]);431 xc.getIncludedMethods().add(includedMethod);432 }433 }434 }435 return result;436 }437 private List<XmlSuite> createCommandLineSuitesForClasses(Class[] classes) {438 //439 // See if any of the classes has an xmlSuite or xmlTest attribute.440 // If it does, create the appropriate XmlSuite, otherwise, create441 // the default one442 //443 XmlClass[] xmlClasses = Arrays.stream(classes)444 .map(clazz -> new XmlClass(clazz, true))445 .toArray(XmlClass[]::new);446 Map<String, XmlSuite> suites = Maps.newHashMap();447 IAnnotationFinder finder = m_configuration.getAnnotationFinder();448 for (int i = 0; i < classes.length; i++) {449 Class<?> c = classes[i];450 ITestAnnotation test = finder.findAnnotation(c, ITestAnnotation.class);451 String suiteName = getDefaultSuiteName();452 String testName = getDefaultTestName();453 boolean isJUnit = false;454 if (test != null) {455 suiteName = defaultIfStringEmpty(test.getSuiteName(), suiteName);456 testName = defaultIfStringEmpty(test.getTestName(), testName);457 } else {458 if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {459 isJUnit = true;460 testName = c.getName();461 }462 }463 XmlSuite xmlSuite = suites.get(suiteName);464 if (xmlSuite == null) {465 xmlSuite = new XmlSuite();466 xmlSuite.setName(suiteName);467 suites.put(suiteName, xmlSuite);468 }469 if (m_dataProviderThreadCount != null) {470 xmlSuite.setDataProviderThreadCount(m_dataProviderThreadCount);471 }472 XmlTest xmlTest = null;473 for (XmlTest xt : xmlSuite.getTests()) {474 if (xt.getName().equals(testName)) {475 xmlTest = xt;476 break;477 }478 }479 if (xmlTest == null) {480 xmlTest = new XmlTest(xmlSuite);481 xmlTest.setName(testName);482 xmlTest.setJUnit(isJUnit);483 }484 xmlTest.getXmlClasses().add(xmlClasses[i]);485 }486 return new ArrayList<>(suites.values());487 }488 public void addMethodSelector(String className, int priority) {489 if (Strings.isNotNullAndNotEmpty(className)) {490 m_methodDescriptors.put(className, priority);491 }492 }493 public void addMethodSelector(XmlMethodSelector selector) {494 m_selectors.add(selector);495 }496 /**497 * Set the suites file names to be run by this TestNG object. This method tries to load and parse498 * the specified TestNG suite xml files. If a file is missing, it is ignored.499 *500 * @param suites A list of paths to one more XML files defining the tests. For example:501 * <pre>502 * TestNG tng = new TestNG();503 * List<String> suites = Lists.newArrayList();504 * suites.add("c:/tests/testng1.xml");505 * suites.add("c:/tests/testng2.xml");506 * tng.setTestSuites(suites);507 * tng.run();508 * </pre>509 */510 public void setTestSuites(List<String> suites) {511 m_stringSuites = suites;512 }513 /**514 * Specifies the XmlSuite objects to run.515 *516 * @param suites - The list of {@link XmlSuite} objects.517 * @see org.testng.xml.XmlSuite518 */519 public void setXmlSuites(List<XmlSuite> suites) {520 m_suites = suites;521 }522 /**523 * Define which groups will be excluded from this run.524 *525 * @param groups A list of group names separated by a comma.526 */527 public void setExcludedGroups(String groups) {528 m_excludedGroups = Utils.split(groups, ",");529 }530 /**531 * Define which groups will be included from this run.532 *533 * @param groups A list of group names separated by a comma.534 */535 public void setGroups(String groups) {536 m_includedGroups = Utils.split(groups, ",");537 }538 private void setTestRunnerFactoryClass(Class testRunnerFactoryClass) {539 setTestRunnerFactory((ITestRunnerFactory) InstanceCreator.newInstance(testRunnerFactoryClass));540 }541 protected void setTestRunnerFactory(ITestRunnerFactory itrf) {542 m_testRunnerFactory = itrf;543 }544 public void setObjectFactory(Class c) {545 m_objectFactory = (ITestObjectFactory) InstanceCreator.newInstance(c);546 }547 public void setObjectFactory(ITestObjectFactory factory) {548 m_objectFactory = factory;549 }550 /**551 * Define which listeners to user for this run.552 *553 * @param classes A list of classes, which must be either ISuiteListener, ITestListener or554 * IReporter555 */556 public void setListenerClasses(List<Class<? extends ITestNGListener>> classes) {557 for (Class<? extends ITestNGListener> cls : classes) {558 addListener(InstanceCreator.newInstance(cls));559 }560 }561 /**562 * @param listener The listener to add563 * @deprecated Use addListener(ITestNGListener) instead564 */565 // TODO remove later /!\ Caution: IntelliJ is using it. Check with @akozlova before removing it566 @Deprecated567 public void addListener(Object listener) {568 if (!(listener instanceof ITestNGListener)) {569 exitWithError(570 "Listener "571 + listener572 + " must be one of ITestListener, ISuiteListener, IReporter, "573 + " IAnnotationTransformer, IMethodInterceptor or IInvokedMethodListener");574 }575 addListener((ITestNGListener) listener);576 }577 private static <E> void maybeAddListener(Map<Class<? extends E>, E> map, E value) {578 maybeAddListener(map, (Class<? extends E>) value.getClass(), value, false);579 }580 private static <E> void maybeAddListener(581 Map<Class<? extends E>, E> map, Class<? extends E> type, E value, boolean quiet) {582 if (map.putIfAbsent(type, value) != null && !quiet) {583 LOGGER.warn("Ignoring duplicate listener : " + type.getName());584 }585 }586 public void addListener(ITestNGListener listener) {587 if (listener == null) {588 return;589 }590 if (listener instanceof IExecutionVisualiser) {591 IExecutionVisualiser visualiser = (IExecutionVisualiser) listener;592 maybeAddListener(m_executionVisualisers, visualiser);593 }594 if (listener instanceof ISuiteListener) {595 ISuiteListener suite = (ISuiteListener) listener;596 maybeAddListener(m_suiteListeners, suite);597 }598 if (listener instanceof ITestListener) {599 ITestListener test = (ITestListener) listener;600 maybeAddListener(m_testListeners, test);601 }602 if (listener instanceof IClassListener) {603 IClassListener clazz = (IClassListener) listener;604 maybeAddListener(m_classListeners, clazz);605 }606 if (listener instanceof IReporter) {607 IReporter reporter = (IReporter) listener;608 maybeAddListener(m_reporters, reporter);609 }610 if (listener instanceof IAnnotationTransformer) {611 setAnnotationTransformer((IAnnotationTransformer) listener);612 }613 if (listener instanceof IMethodInterceptor) {614 m_methodInterceptors.add((IMethodInterceptor) listener);615 }616 if (listener instanceof IInvokedMethodListener) {617 IInvokedMethodListener method = (IInvokedMethodListener) listener;618 maybeAddListener(m_invokedMethodListeners, method);619 }620 if (listener instanceof IHookable) {621 setHookable((IHookable) listener);622 }623 if (listener instanceof IConfigurable) {624 setConfigurable((IConfigurable) listener);625 }626 if (listener instanceof IExecutionListener) {627 m_configuration.addExecutionListenerIfAbsent((IExecutionListener) listener);628 }629 if (listener instanceof IConfigurationListener) {630 m_configuration.addConfigurationListener((IConfigurationListener) listener);631 }632 if (listener instanceof IAlterSuiteListener) {633 IAlterSuiteListener alter = (IAlterSuiteListener) listener;634 maybeAddListener(m_alterSuiteListeners, alter);635 }636 if (listener instanceof IDataProviderListener) {637 IDataProviderListener dataProvider = (IDataProviderListener) listener;638 maybeAddListener(m_dataProviderListeners, dataProvider);639 }640 if (listener instanceof IDataProviderInterceptor) {641 IDataProviderInterceptor interceptor = (IDataProviderInterceptor) listener;642 maybeAddListener(m_dataProviderInterceptors, interceptor);643 }644 }645 public Set<IReporter> getReporters() {646 // This will now cause a different behavior for consumers of this method because unlike before647 // they are no longer648 // going to be getting the original set but only a copy of it (since we internally moved from649 // Sets to Maps)650 return Sets.newHashSet(m_reporters.values());651 }652 public List<ITestListener> getTestListeners() {653 return Lists.newArrayList(m_testListeners.values());654 }655 public List<ISuiteListener> getSuiteListeners() {656 return Lists.newArrayList(m_suiteListeners.values());657 }658 /** If m_verbose gets set, it will override the verbose setting in testng.xml */659 private Integer m_verbose = null;660 private final IAnnotationTransformer m_defaultAnnoProcessor = new DefaultAnnotationTransformer();661 private IAnnotationTransformer m_annotationTransformer = m_defaultAnnoProcessor;662 private Boolean m_skipFailedInvocationCounts = false;663 private final List<IMethodInterceptor> m_methodInterceptors = Lists.newArrayList();664 /** The list of test names to run from the given suite */665 private List<String> m_testNames;666 private Integer m_suiteThreadPoolSize = CommandLineArgs.SUITE_THREAD_POOL_SIZE_DEFAULT;667 private boolean m_randomizeSuites = Boolean.FALSE;668 private boolean m_alwaysRun = Boolean.TRUE;669 private Boolean m_preserveOrder = XmlSuite.DEFAULT_PRESERVE_ORDER;670 private Boolean m_groupByInstances;671 private IConfiguration m_configuration;672 /**673 * Sets the level of verbosity. This value will override the value specified in the test suites.674 *675 * @param verbose the verbosity level (0 to 10 where 10 is most detailed) Actually, this is a lie:676 * you can specify -1 and this will put TestNG in debug mode (no longer slicing off stack677 * traces and all).678 */679 public void setVerbose(int verbose) {680 m_verbose = verbose;681 }682 public void setExecutorFactoryClass(String clazzName) {683 this.m_executorFactory = createExecutorFactoryInstanceUsing(clazzName);684 }685 private IExecutorFactory createExecutorFactoryInstanceUsing(String clazzName) {686 Class<?> cls = ClassHelper.forName(clazzName);687 Object instance = InstanceCreator.newInstance(cls);688 if (instance instanceof IExecutorFactory) {689 return (IExecutorFactory) instance;690 }691 throw new IllegalArgumentException(692 clazzName + " does not implement " + IExecutorFactory.class.getName());693 }694 public void setExecutorFactory(IExecutorFactory factory) {695 this.m_executorFactory = factory;696 }697 public IExecutorFactory getExecutorFactory() {698 if (this.m_executorFactory == null) {699 this.m_executorFactory = createExecutorFactoryInstanceUsing(DEFAULT_THREADPOOL_FACTORY);700 }701 return this.m_executorFactory;702 }703 private void initializeCommandLineSuites() {704 if (m_commandLineTestClasses != null || m_commandLineMethods != null) {705 if (null != m_commandLineMethods) {706 m_cmdlineSuites = createCommandLineSuitesForMethods(m_commandLineMethods);707 } else {708 m_cmdlineSuites = createCommandLineSuitesForClasses(m_commandLineTestClasses);709 }710 for (XmlSuite s : m_cmdlineSuites) {711 for (XmlTest t : s.getTests()) {712 t.setPreserveOrder(m_preserveOrder);713 }714 m_suites.add(s);715 if (m_groupByInstances != null) {716 s.setGroupByInstances(m_groupByInstances);717 }718 }719 }720 }721 private void initializeCommandLineSuitesParams() {722 if (null == m_cmdlineSuites) {723 return;724 }725 for (XmlSuite s : m_cmdlineSuites) {726 if (m_threadCount != -1) {727 s.setThreadCount(m_threadCount);728 }729 if (m_parallelMode != null) {730 s.setParallel(m_parallelMode);731 }732 if (m_configFailurePolicy != null) {733 s.setConfigFailurePolicy(m_configFailurePolicy);734 }735 }736 }737 private void initializeCommandLineSuitesGroups() {738 // If groups were specified on the command line, they should override groups739 // specified in the XML file740 boolean hasIncludedGroups = null != m_includedGroups && m_includedGroups.length > 0;741 boolean hasExcludedGroups = null != m_excludedGroups && m_excludedGroups.length > 0;742 List<XmlSuite> suites = m_cmdlineSuites != null ? m_cmdlineSuites : m_suites;743 if (hasIncludedGroups || hasExcludedGroups) {744 for (XmlSuite s : suites) {745 initializeCommandLineSuitesGroups(746 s, hasIncludedGroups, m_includedGroups, hasExcludedGroups, m_excludedGroups);747 }748 }749 }750 private static void initializeCommandLineSuitesGroups(751 XmlSuite s,752 boolean hasIncludedGroups,753 String[] m_includedGroups,754 boolean hasExcludedGroups,755 String[] m_excludedGroups) {756 if (hasIncludedGroups) {757 s.setIncludedGroups(Arrays.asList(m_includedGroups));758 }759 if (hasExcludedGroups) {760 s.setExcludedGroups(Arrays.asList(m_excludedGroups));761 }762 for (XmlSuite child : s.getChildSuites()) {763 initializeCommandLineSuitesGroups(764 child, hasIncludedGroups, m_includedGroups, hasExcludedGroups, m_excludedGroups);765 }766 }767 private void addReporter(Class<? extends IReporter> r) {768 if (!m_reporters.containsKey(r)) {769 m_reporters.put(r, InstanceCreator.newInstance(r));770 }771 }772 private void initializeDefaultListeners() {773 if (m_failIfAllTestsSkipped) {774 this.exitCodeListener.failIfAllTestsSkipped();775 }776 addListener(this.exitCodeListener);777 if (m_useDefaultListeners) {778 addReporter(SuiteHTMLReporter.class);779 addReporter(Main.class);780 addReporter(FailedReporter.class);781 addReporter(XMLReporter.class);782 if (RuntimeBehavior.useOldTestNGEmailableReporter()) {783 addReporter(EmailableReporter.class);784 } else if (RuntimeBehavior.useEmailableReporter()) {785 addReporter(EmailableReporter2.class);786 }787 addReporter(JUnitReportReporter.class);788 if (m_verbose != null && m_verbose > 4) {789 addListener(new VerboseReporter("[TestNG] "));790 }791 }792 }793 private void initializeConfiguration() {794 ITestObjectFactory factory = m_objectFactory;795 //796 // Install the listeners found in ServiceLoader (or use the class797 // loader for tests, if specified).798 //799 addServiceLoaderListeners();800 //801 // Install the listeners found in the suites802 //803 for (XmlSuite s : m_suites) {804 addListeners(s);805 //806 // Install the method selectors807 //808 for (XmlMethodSelector methodSelector : s.getMethodSelectors()) {809 addMethodSelector(methodSelector.getClassName(), methodSelector.getPriority());810 addMethodSelector(methodSelector);811 }812 //813 // Find if we have an object factory814 //815 if (s.getObjectFactory() != null) {816 if (factory == null) {817 factory = s.getObjectFactory();818 } else {819 throw new TestNGException("Found more than one object-factory tag in your suites");820 }821 }822 }823 m_configuration.setAnnotationFinder(new JDK15AnnotationFinder(getAnnotationTransformer()));824 m_configuration.setHookable(m_hookable);825 m_configuration.setConfigurable(m_configurable);826 m_configuration.setObjectFactory(factory);827 m_configuration.setAlwaysRunListeners(this.m_alwaysRun);828 m_configuration.setExecutorFactory(getExecutorFactory());829 }830 private void addListeners(XmlSuite s) {831 for (String listenerName : s.getListeners()) {832 Class<?> listenerClass = ClassHelper.forName(listenerName);833 // If specified listener does not exist, a TestNGException will be thrown834 if (listenerClass == null) {835 throw new TestNGException(836 "Listener " + listenerName + " was not found in project's classpath");837 }838 ITestNGListener listener = (ITestNGListener) InstanceCreator.newInstance(listenerClass);839 addListener(listener);840 }841 // Add the child suite listeners842 List<XmlSuite> childSuites = s.getChildSuites();843 for (XmlSuite c : childSuites) {844 addListeners(c);845 }846 }847 /** Using reflection to remain Java 5 compliant. */848 private void addServiceLoaderListeners() {849 Iterable<ITestNGListener> loader =850 m_serviceLoaderClassLoader != null851 ? ServiceLoader.load(ITestNGListener.class, m_serviceLoaderClassLoader)852 : ServiceLoader.load(ITestNGListener.class);853 for (ITestNGListener l : loader) {854 Utils.log("[TestNG]", 2, "Adding ServiceLoader listener:" + l);855 if (m_listenersToSkipFromBeingWiredIn.contains(l.getClass().getName())) {856 Utils.log("[TestNG]", 2, "Skipping adding the listener :" + l);857 continue;858 }859 addListener(l);860 addServiceLoaderListener(l);861 }862 }863 /**864 * Before suites are executed, do a sanity check to ensure all required conditions are met. If865 * not, throw an exception to stop test execution866 *867 * @throws TestNGException if the sanity check fails868 */869 private void sanityCheck() {870 XmlSuiteUtils.validateIfSuitesContainDuplicateTests(m_suites);871 XmlSuiteUtils.adjustSuiteNamesToEnsureUniqueness(m_suites);872 }873 /** Invoked by the remote runner. */874 public void initializeEverything() {875 // The Eclipse plug-in (RemoteTestNG) might have invoked this method already876 // so don't initialize suites twice.877 if (m_isInitialized) {878 return;879 }880 initializeSuitesAndJarFile();881 initializeConfiguration();882 initializeDefaultListeners();883 initializeCommandLineSuites();884 initializeCommandLineSuitesParams();885 initializeCommandLineSuitesGroups();886 m_isInitialized = true;887 }888 /** Run TestNG. */889 public void run() {890 initializeEverything();891 sanityCheck();892 runExecutionListeners(true /* start */);893 runSuiteAlterationListeners();894 m_start = System.currentTimeMillis();895 List<ISuite> suiteRunners = runSuites();896 m_end = System.currentTimeMillis();897 if (null != suiteRunners) {898 generateReports(suiteRunners);899 }900 runExecutionListeners(false /* finish */);901 exitCode = this.exitCodeListener.getStatus();902 if (exitCodeListener.noTestsFound()) {903 if (TestRunner.getVerbose() > 1) {904 System.err.println("[TestNG] No tests found. Nothing was run");905 usage();906 }907 }908 m_instance = null;909 m_jCommander = null;910 }911 /**912 * Run the test suites.913 *914 * <p>This method can be overridden by subclass. <br>915 * For example, DistributedTestNG to run in master/slave mode according to commandline args.916 *917 * @return - List of suites that were run as {@link ISuite} objects.918 * @since 6.9.11 when moving distributed/remote classes out into separate project919 */920 protected List<ISuite> runSuites() {921 return runSuitesLocally();922 }923 private void runSuiteAlterationListeners() {924 for (IAlterSuiteListener l : m_alterSuiteListeners.values()) {925 l.alter(m_suites);926 }927 }928 private void runExecutionListeners(boolean start) {929 for (IExecutionListener l : m_configuration.getExecutionListeners()) {930 if (start) {931 l.onExecutionStart();932 } else {933 l.onExecutionFinish();934 }935 }936 }937 private static void usage() {938 if (m_jCommander == null) {939 m_jCommander = new JCommander(new CommandLineArgs());940 }941 m_jCommander.usage();942 }943 private void generateReports(List<ISuite> suiteRunners) {944 for (IReporter reporter : m_reporters.values()) {945 try {946 long start = System.currentTimeMillis();947 reporter.generateReport(m_suites, suiteRunners, m_outputDir);948 Utils.log(949 "TestNG",950 2,951 "Time taken by " + reporter + ": " + (System.currentTimeMillis() - start) + " ms");952 } catch (Exception ex) {953 System.err.println("[TestNG] Reporter " + reporter + " failed");954 ex.printStackTrace(System.err);955 }956 }957 }958 /**959 * This needs to be public for maven2, for now..At least until an alternative mechanism is found.960 * @return The locally run suites961 */962 public List<ISuite> runSuitesLocally() {963 if (m_suites.isEmpty()) {964 error("No test suite found. Nothing to run");965 usage();966 return Collections.emptyList();967 }968 SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap();969 // First initialize the suite runners to ensure there are no configuration issues.970 // Create a map with XmlSuite as key and corresponding SuiteRunner as value971 for (XmlSuite xmlSuite : m_suites) {972 createSuiteRunners(suiteRunnerMap, xmlSuite);973 }974 //975 // Run suites976 //977 if (m_suiteThreadPoolSize == 1 && !m_randomizeSuites) {978 // Single threaded and not randomized: run the suites in order979 for (XmlSuite xmlSuite : m_suites) {980 runSuitesSequentially(981 xmlSuite, suiteRunnerMap, getVerbose(xmlSuite), getDefaultSuiteName());982 }983 //984 // Generate the suites report985 //986 return Lists.newArrayList(suiteRunnerMap.values());987 }988 // Multithreaded: generate a dynamic graph that stores the suite hierarchy. This is then989 // used to run related suites in specific order. Parent suites are run only990 // once all the child suites have completed execution991 IDynamicGraph<ISuite> suiteGraph = new DynamicGraph<>();992 for (XmlSuite xmlSuite : m_suites) {993 populateSuiteGraph(suiteGraph, suiteRunnerMap, xmlSuite);994 }995 IThreadWorkerFactory<ISuite> factory =996 new SuiteWorkerFactory(997 suiteRunnerMap, 0 /* verbose hasn't been set yet */, getDefaultSuiteName());998 ITestNGThreadPoolExecutor pooledExecutor = this.getExecutorFactory().newSuiteExecutor(999 "suites",1000 suiteGraph,1001 factory,1002 m_suiteThreadPoolSize,1003 m_suiteThreadPoolSize,1004 Integer.MAX_VALUE,1005 TimeUnit.MILLISECONDS,1006 new LinkedBlockingQueue<>(),1007 null);1008 Utils.log("TestNG", 2, "Starting executor for all suites");1009 // Run all suites in parallel1010 pooledExecutor.run();1011 try {1012 pooledExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);1013 pooledExecutor.shutdownNow();1014 } catch (InterruptedException handled) {1015 Thread.currentThread().interrupt();1016 error("Error waiting for concurrent executors to finish " + handled.getMessage());1017 }1018 //1019 // Generate the suites report1020 //1021 return Lists.newArrayList(suiteRunnerMap.values());1022 }1023 private static void error(String s) {1024 LOGGER.error(s);1025 }1026 /**1027 * @return the verbose level, checking in order: the verbose level on the suite, the verbose level1028 * on the TestNG object, or 1.1029 */1030 private int getVerbose(XmlSuite xmlSuite) {1031 return xmlSuite.getVerbose() != null1032 ? xmlSuite.getVerbose()1033 : (m_verbose != null ? m_verbose : DEFAULT_VERBOSE);1034 }1035 /**1036 * Recursively runs suites. Runs the children suites before running the parent suite. This is done1037 * so that the results for parent suite can reflect the combined results of the children suites.1038 *1039 * @param xmlSuite XML Suite to be executed1040 * @param suiteRunnerMap Maps {@code XmlSuite}s to respective {@code ISuite}1041 * @param verbose verbose level1042 * @param defaultSuiteName default suite name1043 */1044 private void runSuitesSequentially(1045 XmlSuite xmlSuite, SuiteRunnerMap suiteRunnerMap, int verbose, String defaultSuiteName) {1046 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1047 runSuitesSequentially(childSuite, suiteRunnerMap, verbose, defaultSuiteName);1048 }1049 SuiteRunnerWorker srw =1050 new SuiteRunnerWorker(1051 suiteRunnerMap.get(xmlSuite), suiteRunnerMap, verbose, defaultSuiteName);1052 srw.run();1053 }1054 /**1055 * Populates the dynamic graph with the reverse hierarchy of suites. Edges are added pointing from1056 * child suite runners to parent suite runners, hence making parent suite runners dependent on all1057 * the child suite runners1058 *1059 * @param suiteGraph dynamic graph representing the reverse hierarchy of SuiteRunners1060 * @param suiteRunnerMap Map with XMLSuite as key and its respective SuiteRunner as value1061 * @param xmlSuite XML Suite1062 */1063 private void populateSuiteGraph(1064 IDynamicGraph<ISuite> suiteGraph /* OUT */, SuiteRunnerMap suiteRunnerMap, XmlSuite xmlSuite) {1065 ISuite parentSuiteRunner = suiteRunnerMap.get(xmlSuite);1066 if (xmlSuite.getChildSuites().isEmpty()) {1067 suiteGraph.addNode(parentSuiteRunner);1068 } else {1069 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1070 suiteGraph.addEdge(0, parentSuiteRunner, suiteRunnerMap.get(childSuite));1071 populateSuiteGraph(suiteGraph, suiteRunnerMap, childSuite);1072 }1073 }1074 }1075 /**1076 * Creates the {@code SuiteRunner}s and populates the suite runner map with this information1077 *1078 * @param suiteRunnerMap Map with XMLSuite as key and it's respective SuiteRunner as value. This1079 * is updated as part of this method call1080 * @param xmlSuite Xml Suite (and its children) for which {@code SuiteRunner}s are created1081 */1082 private void createSuiteRunners(SuiteRunnerMap suiteRunnerMap /* OUT */, XmlSuite xmlSuite) {1083 if (null != m_isJUnit && !m_isJUnit.equals(XmlSuite.DEFAULT_JUNIT)) {1084 xmlSuite.setJUnit(m_isJUnit);1085 }1086 // If the skip flag was invoked on the command line, it1087 // takes precedence1088 if (null != m_skipFailedInvocationCounts) {1089 xmlSuite.setSkipFailedInvocationCounts(m_skipFailedInvocationCounts);1090 }1091 // Override the XmlSuite verbose value with the one from TestNG1092 if (m_verbose != null) {1093 xmlSuite.setVerbose(m_verbose);1094 }1095 if (null != m_configFailurePolicy) {1096 xmlSuite.setConfigFailurePolicy(m_configFailurePolicy);1097 }1098 Set<XmlMethodSelector> selectors = Sets.newHashSet();1099 for (XmlTest t : xmlSuite.getTests()) {1100 for (Map.Entry<String, Integer> ms : m_methodDescriptors.entrySet()) {1101 XmlMethodSelector xms = new XmlMethodSelector();1102 xms.setName(ms.getKey());1103 xms.setPriority(ms.getValue());1104 selectors.add(xms);1105 }1106 selectors.addAll(m_selectors);1107 t.getMethodSelectors().addAll(Lists.newArrayList(selectors));1108 }1109 suiteRunnerMap.put(xmlSuite, createSuiteRunner(xmlSuite));1110 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1111 createSuiteRunners(suiteRunnerMap, childSuite);1112 }1113 }1114 /** Creates a suite runner and configures its initial state */1115 private SuiteRunner createSuiteRunner(XmlSuite xmlSuite) {1116 DataProviderHolder holder = new DataProviderHolder();1117 holder.addListeners(m_dataProviderListeners.values());1118 holder.addInterceptors(m_dataProviderInterceptors.values());1119 SuiteRunner result =1120 new SuiteRunner(1121 getConfiguration(),1122 xmlSuite,1123 m_outputDir,1124 m_testRunnerFactory,1125 m_useDefaultListeners,1126 m_methodInterceptors,1127 m_invokedMethodListeners.values(),1128 m_testListeners.values(),1129 m_classListeners.values(),1130 holder,1131 Systematiser.getComparator());1132 for (ISuiteListener isl : m_suiteListeners.values()) {1133 result.addListener(isl);1134 }1135 for (IReporter r : result.getReporters()) {1136 maybeAddListener(m_reporters, r.getClass(), r, true);1137 }1138 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {1139 result.addConfigurationListener(cl);1140 }1141 m_executionVisualisers.values().forEach(result::addListener);1142 return result;1143 }1144 protected IConfiguration getConfiguration() {1145 return m_configuration;1146 }1147 /**1148 * The TestNG entry point for command line execution.1149 *1150 * @param argv the TestNG command line parameters.1151 */1152 public static void main(String[] argv) {1153 TestNG testng = privateMain(argv, null);1154 System.exit(testng.getStatus());1155 }1156 /**1157 * <B>Note</B>: this method is not part of the public API and is meant for internal usage only.1158 *1159 * @param argv The param arguments1160 * @param listener The listener1161 * @return The TestNG instance1162 */1163 public static TestNG privateMain(String[] argv, ITestListener listener) {1164 TestNG result = new TestNG();1165 if (null != listener) {1166 result.addListener(listener);1167 }1168 //1169 // Parse the arguments1170 //1171 try {1172 CommandLineArgs cla = new CommandLineArgs();1173 m_jCommander = new JCommander(cla);1174 m_jCommander.parse(argv);1175 validateCommandLineParameters(cla);1176 result.configure(cla);1177 } catch (ParameterException ex) {1178 exitWithError(ex.getMessage());1179 }1180 //1181 // Run1182 //1183 try {1184 result.run();1185 } catch (TestNGException ex) {1186 if (TestRunner.getVerbose() > 1) {1187 ex.printStackTrace(System.out);1188 } else {1189 error(ex.getMessage());1190 }1191 result.exitCode = ExitCode.newExitCodeRepresentingFailure();1192 }1193 return result;1194 }1195 /**1196 * Configure the TestNG instance based on the command line parameters.1197 *1198 * @param cla The command line parameters1199 */1200 protected void configure(CommandLineArgs cla) {1201 if (cla.verbose != null) {1202 setVerbose(cla.verbose);1203 }1204 if (cla.dependencyInjectorFactoryClass != null) {1205 Class<?> clazz = ClassHelper.forName(cla.dependencyInjectorFactoryClass);1206 if (clazz != null && IInjectorFactory.class.isAssignableFrom(clazz)) {1207 m_configuration.setInjectorFactory(InstanceCreator.newInstance((Class<IInjectorFactory>) clazz));1208 }1209 }1210 if (cla.threadPoolFactoryClass != null) {1211 setExecutorFactoryClass(cla.threadPoolFactoryClass);1212 }1213 setOutputDirectory(cla.outputDirectory);1214 String testClasses = cla.testClass;1215 if (null != testClasses) {1216 String[] strClasses = testClasses.split(",");1217 List<Class<?>> classes = Lists.newArrayList();1218 for (String c : strClasses) {1219 classes.add(ClassHelper.fileToClass(c));1220 }1221 setTestClasses(classes.toArray(new Class[0]));1222 }1223 setOutputDirectory(cla.outputDirectory);1224 if (cla.testNames != null) {1225 setTestNames(Arrays.asList(cla.testNames.split(",")));1226 }1227 // Note: can't use a Boolean field here because we are allowing a boolean1228 // parameter with an arity of 1 ("-usedefaultlisteners false")1229 if (cla.useDefaultListeners != null) {1230 setUseDefaultListeners("true".equalsIgnoreCase(cla.useDefaultListeners));1231 }1232 setGroups(cla.groups);1233 setExcludedGroups(cla.excludedGroups);1234 setTestJar(cla.testJar);1235 setXmlPathInJar(cla.xmlPathInJar);1236 setJUnit(cla.junit);1237 setMixed(cla.mixed);1238 setSkipFailedInvocationCounts(cla.skipFailedInvocationCounts);1239 toggleFailureIfAllTestsWereSkipped(cla.failIfAllTestsSkipped);1240 setListenersToSkipFromBeingWiredInViaServiceLoaders(cla.spiListenersToSkip.split(","));1241 m_configuration.setOverrideIncludedMethods(cla.overrideIncludedMethods);1242 if (cla.parallelMode != null) {1243 setParallel(cla.parallelMode);1244 }1245 if (cla.configFailurePolicy != null) {1246 setConfigFailurePolicy(XmlSuite.FailurePolicy.getValidPolicy(cla.configFailurePolicy));1247 }1248 if (cla.threadCount != null) {1249 setThreadCount(cla.threadCount);1250 }1251 if (cla.dataProviderThreadCount != null) {1252 setDataProviderThreadCount(cla.dataProviderThreadCount);1253 }1254 if (cla.suiteName != null) {...
Source:ServiceLoaderTest.java
...28 URL url = getClass().getClassLoader().getResource("serviceloader.jar");29 URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { url });30 tng.setServiceLoaderClassLoader(ucl);31 String dontLoad = "test.serviceloader.TmpSuiteListener";32 tng.setListenersToSkipFromBeingWiredInViaServiceLoaders(dontLoad);33 tng.run();34 List<String> loaded = tng.getServiceLoaderListeners().stream().map(l->l.getClass().getName()).collect(35 Collectors.toList());36 assertThat(loaded).doesNotContain(dontLoad);37 }38 @Test(description = "GITHUB-2259")39 public void ensureSpiLoadedListenersCanBeSkipped2() {40 TestNG tng = create(ServiceLoaderSampleTest.class);41 URL url = getClass().getClassLoader().getResource("serviceloader.jar");42 URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { url });43 tng.setServiceLoaderClassLoader(ucl);44 String dontLoad = "test.serviceloader.TmpSuiteListener";45 Map<String, String> cli = new HashMap<>();46 cli.put(CommandLineArgs.LISTENERS_TO_SKIP_VIA_SPI, dontLoad);...
setListenersToSkipFromBeingWiredInViaServiceLoaders
Using AI Code Generation
1import org.testng.ITestContext;2import org.testng.ITestListener;3import org.testng.ITestResult;4import org.testng.TestListenerAdapter;5import org.testng.TestNG;6import org.testng.annotations.Test;7import java.util.ArrayList;8import java.util.List;9public class TestNGListenersExample {10 public static void main(String[] args) {11 TestNG testNG = new TestNG();12 testNG.setTestClasses(new Class[]{TestNGListenersExample.class});13 List<ITestListener> listeners = new ArrayList<>();14 listeners.add(new TestListenerAdapter() {15 public void onTestStart(ITestResult result) {16 System.out.println("Test Started: " + result.getName());17 }18 public void onTestSuccess(ITestResult result) {19 System.out.println("Test Passed: " + result.getName());20 }21 public void onTestFailure(ITestResult result) {22 System.out.println("Test Failed: " + result.getName());23 }24 public void onTestSkipped(ITestResult result) {25 System.out.println("Test Skipped: " + result.getName());26 }27 });28 testNG.setListeners(listeners);29 testNG.run();30 }31 public void test1() {32 System.out.println("Test 1");33 }34 public void test2() {35 System.out.println("Test 2");36 }37 public void test3() {38 System.out.println("Test 3");39 }40}
setListenersToSkipFromBeingWiredInViaServiceLoaders
Using AI Code Generation
1package org.testng;2import java.lang.reflect.Method;3import java.util.Iterator;4import java.util.List;5import java.util.Set;6import java.util.concurrent.CopyOnWriteArrayList;7import org.testng.collections.Lists;8import org.testng.collections.Sets;9import org.testng.internal.ClassHelper;10import org.testng.internal.Configuration;11import org.testng.internal.ConstructorOrMethod;12import org.testng.internal.DynamicGraph.Status;13import org.testng.internal.IConfiguration;14import org.testng.internal.IConfigurationListener;15import org.testng.internal.IInvoker;16import org.testng.internal.IResultListener;17import org.testng.internal.IResultMap;18import org.testng.internal.ITestContext;19import org.testng.internal.ITestListener;20import org.testng.internal.ITestNGMethod;21import org.testng.internal.ITestResult;22import org.testng.internal.Invoker;23import org.testng.internal.MethodHelper;24import org.testng.internal.MethodInstance;25import org.testng.internal.Parameters;26import org.testng.internal.ResultMap;27import org.testng.internal.RuntimeBehavior;28import org.testng.internal.SuiteRunnerMap;29import org.testng.internal.TestNGMethod;30import org.testng.internal.TestNGMethodFinder;31import org.testng.internal.Utils;32import org.testng.internal.annotations.IAnnotationFinder;33import org.testng.internal.annotations.JDK15AnnotationFinder;34import org.testng.internal.annotations.JDK15AnnotationFinder.ParameterNamesNotFoundException;35import org.testng.internal.annotations.JDK15AnnotationFinder.ParameterTypesNotFoundException;36import org.testng.internal.annotations.JDK15AnnotationFinder.ParametersNotFoundException;37import org.testng.internal.annotations.JDK15AnnotationFinder.TestNameNotFoundException;38import org.testng.internal.annotations.JDK15AnnotationFinderFactory;39import org.testng.internal.annotations.JDK15AnnotationFinderFactory.AnnotationFinderType;40import org.testng.internal.annotations.JDK15AnnotationFinderFactory.AnnotationFinderTypeFactory;41import org.testng.internal.annotations.TestAnnotation;42import org.testng.internal.annotations.TestAnnotationParser;43import org.testng.internal.annotations.TestAnnotationParserFactory;44import org.testng.internal.annotations.TestAnnotationParserFactory.TestAnnotationParserType;45import org.testng.internal.annotations.TestAnnotationParserFactory.TestAnnotationParserTypeFactory;46import org.testng.internal.thread.graph.IWorker;47import org.testng.internal.thread.graph.IWorkerFactory;48import org.testng.internal.thread.graph.WorkerFactory;49import org.testng.internal.thread.graph.WorkerGraph;50import org.testng.internal.thread.graph.WorkerGraphFactory;51import org.testng.internal.thread.graph.WorkerGraphFactory.WorkerGraphType;52import org.testng.internal.thread.graph.WorkerGraphFactory.WorkerGraphTypeFactory;53import org.testng.log4testng.Logger;54import org.testng.xml.XmlClass;55import org.testng.xml.Xml
setListenersToSkipFromBeingWiredInViaServiceLoaders
Using AI Code Generation
1public void setListenersToSkipFromBeingWiredInViaServiceLoaders(List<Class<? extends ITestListener>> listenersToSkip) {2 this.listenersToSkip = listenersToSkip;3}4public List<ITestListener> getListeners() {5 if (null == listeners) {6 listeners = new ArrayList<ITestListener>();7 ServiceLoader<ITestListener> listenerLoader = ServiceLoader.load(ITestListener.class);8 for (ITestListener listener : listenerLoader) {9 if (null == listenersToSkip || !listenersToSkip.contains(listener.getClass())) {10 listeners.add(listener);11 }12 }13 }14 return listeners;15 }16public void addListener(ITestListener listener) {17 if (null == listeners) {18 getListeners();19 }20 listeners.add(listener);21 }22public void removeListener(ITestListener listener) {23 if (null == listeners) {24 getListeners();25 }26 listeners.remove(listener);27 }28public void run() {29 if (null == listeners) {30 getListeners();31 }32 for (ITestListener listener : listeners) {33 listener.onStart(this);34 }35 runSuitesLocally();36 for (ITestListener listener : listeners) {37 listener.onFinish(this);38 }39 }40private void runSuitesLocally() {41 for (XmlSuite suite : m_suites) {42 if (suite.getTests().size() > 0) {43 for (ITestListener listener : listeners) {44 listener.onBeforeSuite(suite);45 }46 run(suite);47 for (ITestListener listener : listeners) {48 listener.onAfterSuite(suite);49 }50 }51 }52 }53private void run(XmlSuite suite) {54 XmlPackageRunner packageRunner = new XmlPackageRunner(suite, this);55 packageRunner.run();56 }57public Set<ITestResult> getFailedTests() {
setListenersToSkipFromBeingWiredInViaServiceLoaders
Using AI Code Generation
1 TestNG testNG = new TestNG();2 testNG.setListenersToSkipFromBeingWiredInViaServiceLoaders(new Class[]{MyListener.class});3 testNG.setTestClasses(new Class[]{MyTest.class});4 testNG.run();5 TestNG testNG = new TestNG();6 testNG.addListener(new MyListener());7 testNG.setTestClasses(new Class[]{MyTest.class});8 testNG.run();9 TestNG testNG = new TestNG();10 testNG.addListener(new MyListener());11 testNG.setTestClasses(new Class[]{MyTest.class});12 testNG.run();13 TestNG testNG = new TestNG();14 testNG.addListener(new MyListener());15 testNG.setTestClasses(new Class[]{MyTest.class});16 testNG.run();17 TestNG testNG = new TestNG();18 testNG.addListener(new MyListener());19 testNG.setTestClasses(new Class[]{MyTest.class});20 testNG.run();21 TestNG testNG = new TestNG();22 testNG.addListener(new MyListener());23 testNG.setTestClasses(new Class[]{MyTest.class});24 testNG.run();25 TestNG testNG = new TestNG();26 testNG.addListener(new MyListener());27 testNG.setTestClasses(new Class[]{MyTest.class});28 testNG.run();29 TestNG testNG = new TestNG();30 testNG.addListener(new MyListener());31 testNG.setTestClasses(new Class[]{MyTest.class});32 testNG.run();33 TestNG testNG = new TestNG();34 testNG.addListener(new MyListener());35 testNG.setTestClasses(new Class[]{MyTest.class});36 testNG.run();
setListenersToSkipFromBeingWiredInViaServiceLoaders
Using AI Code Generation
1public class TestNGTest {2 public void test() {3 TestNG testNG = new TestNG();4 testNG.setTestClasses(new Class[] {Test1.class, Test2.class});5 testNG.setListenersToSkipFromBeingWiredInViaServiceLoaders(new Class[] {ITestListener.class});6 testNG.run();7 }8}9public class Test1 {10 public void test1() {11 System.out.println("test1");12 }13}14public class Test2 {15 public void test2() {16 System.out.println("test2");17 }18}19public class ITestListener implements ITestListener {20 public void onTestStart(ITestResult result) {21 System.out.println("onTestStart");22 }23 public void onTestSuccess(ITestResult result) {24 System.out.println("onTestSuccess");25 }26 public void onTestFailure(ITestResult result) {27 System.out.println("onTestFailure");28 }29 public void onTestSkipped(ITestResult result) {30 System.out.println("onTestSkipped");31 }32 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {33 System.out.println("onTestFailedButWithinSuccessPercentage");34 }35 public void onStart(ITestContext context) {36 System.out.println("onStart");37 }38 public void onFinish(ITestContext context) {39 System.out.println("onFinish");40 }41}
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!!