How to use CommandLineArgs class of org.testng package

Best Testng code snippet using org.testng.CommandLineArgs

Source:TestNG.java Github

copy

Full Screen

...138 m_invokedMethodListeners = Maps.newHashMap();139 private Integer m_dataProviderThreadCount = null;140 private String m_jarPath;141 /** The path of the testng.xml file inside the jar file */142 private String m_xmlPathInJar = CommandLineArgs.XML_PATH_IN_JAR_DEFAULT;143 private List<String> m_stringSuites = Lists.newArrayList();144 private IHookable m_hookable;145 private IConfigurable m_configurable;146 protected long m_end;147 protected long m_start;148 private final Map<Class<? extends IAlterSuiteListener>, IAlterSuiteListener>149 m_alterSuiteListeners = Maps.newHashMap();150 private boolean m_isInitialized = false;151 private boolean isSuiteInitialized = false;152 private final org.testng.internal.ExitCodeListener exitCodeListener =153 new org.testng.internal.ExitCodeListener();154 private ExitCode exitCode;155 private final Map<Class<? extends IExecutionVisualiser>, IExecutionVisualiser>156 m_executionVisualisers = Maps.newHashMap();157 /** Default constructor. Setting also usage of default listeners/reporters. */158 public TestNG() {159 init(true);160 }161 /**162 * Used by maven2 to have 0 output of any kind come out of testng.163 *164 * @param useDefaultListeners Whether or not any default reports should be added to tests.165 */166 public TestNG(boolean useDefaultListeners) {167 init(useDefaultListeners);168 }169 private void init(boolean useDefaultListeners) {170 m_instance = this;171 m_useDefaultListeners = useDefaultListeners;172 m_configuration = new Configuration();173 }174 public int getStatus() {175 if (exitCodeListener.noTestsFound()) {176 return ExitCode.HAS_NO_TEST;177 }178 return exitCode.getExitCode();179 }180 /**181 * Sets the output directory where the reports will be created.182 *183 * @param outputdir The directory.184 */185 public void setOutputDirectory(final String outputdir) {186 if (isStringNotEmpty(outputdir)) {187 m_outputDir = outputdir;188 }189 }190 /**191 * If this method is passed true before run(), the default listeners will not be used.192 *193 * <ul>194 * <li>org.testng.reporters.TestHTMLReporter195 * <li>org.testng.reporters.JUnitXMLReporter196 * <li>org.testng.reporters.XMLReporter197 * </ul>198 *199 * @see org.testng.reporters.TestHTMLReporter200 * @see org.testng.reporters.JUnitXMLReporter201 * @see org.testng.reporters.XMLReporter202 */203 public void setUseDefaultListeners(boolean useDefaultListeners) {204 m_useDefaultListeners = useDefaultListeners;205 }206 /**207 * Sets a jar containing a testng.xml file.208 *209 * @param jarPath210 */211 public void setTestJar(String jarPath) {212 m_jarPath = jarPath;213 }214 /** Sets the path to the XML file in the test jar file. */215 public void setXmlPathInJar(String xmlPathInJar) {216 m_xmlPathInJar = xmlPathInJar;217 }218 private void parseSuiteFiles() {219 IPostProcessor processor = getProcessor();220 for (XmlSuite s : m_suites) {221 if (s.isParsed()) {222 continue;223 }224 for (String suiteFile : s.getSuiteFiles()) {225 try {226 String fileNameToUse = s.getFileName();227 if (fileNameToUse == null || fileNameToUse.trim().isEmpty()) {228 fileNameToUse = suiteFile;229 }230 Collection<XmlSuite> childSuites = Parser.parse(fileNameToUse, processor);231 for (XmlSuite cSuite : childSuites) {232 cSuite.setParentSuite(s);233 s.getChildSuites().add(cSuite);234 }235 } catch (IOException e) {236 e.printStackTrace(System.out);237 }238 }239 }240 }241 private OverrideProcessor getProcessor() {242 return new OverrideProcessor(m_includedGroups, m_excludedGroups);243 }244 private void parseSuite(String suitePath) {245 if (LOGGER.isDebugEnabled()) {246 LOGGER.debug("suiteXmlPath: \"" + suitePath + "\"");247 }248 try {249 Collection<XmlSuite> allSuites = Parser.parse(suitePath, getProcessor());250 for (XmlSuite s : allSuites) {251 if (this.m_parallelMode != null) {252 s.setParallel(this.m_parallelMode);253 }254 if (this.m_threadCount > 0) {255 s.setThreadCount(this.m_threadCount);256 }257 if (m_testNames == null) {258 m_suites.add(s);259 continue;260 }261 // If test names were specified, only run these test names262 TestNamesMatcher testNamesMatcher = new TestNamesMatcher(s, m_testNames);263 List<String> missMatchedTestname = testNamesMatcher.getMissMatchedTestNames();264 if (!missMatchedTestname.isEmpty()) {265 throw new TestNGException("The test(s) <" + missMatchedTestname + "> cannot be found.");266 }267 m_suites.addAll(testNamesMatcher.getSuitesMatchingTestNames());268 }269 } catch (IOException e) {270 e.printStackTrace(System.out);271 } catch (Exception ex) {272 // Probably a Yaml exception, unnest it273 Throwable t = ex;274 while (t.getCause() != null) {275 t = t.getCause();276 }277 if (t instanceof TestNGException) {278 throw (TestNGException) t;279 }280 throw new TestNGException(t);281 }282 }283 public void initializeSuitesAndJarFile() {284 // The IntelliJ plug-in might have invoked this method already so don't initialize suites twice.285 if (isSuiteInitialized) {286 return;287 }288 isSuiteInitialized = true;289 if (!m_suites.isEmpty()) {290 parseSuiteFiles(); // to parse the suite files (<suite-file>), if any291 return;292 }293 //294 // Parse the suites that were passed on the command line295 //296 for (String suitePath : m_stringSuites) {297 parseSuite(suitePath);298 }299 //300 // jar path301 //302 // If suites were passed on the command line, they take precedence over the suite file303 // inside that jar path304 if (m_jarPath != null && !m_stringSuites.isEmpty()) {305 StringBuilder suites = new StringBuilder();306 for (String s : m_stringSuites) {307 suites.append(s);308 }309 Utils.log(310 "TestNG",311 2,312 "Ignoring the XML file inside " + m_jarPath + " and using " + suites + " instead");313 return;314 }315 if (isStringEmpty(m_jarPath)) {316 return;317 }318 // We have a jar file and no XML file was specified: try to find an XML file inside the jar319 File jarFile = new File(m_jarPath);320 JarFileUtils utils =321 new JarFileUtils(getProcessor(), m_xmlPathInJar, m_testNames, m_parallelMode);322 m_suites.addAll(utils.extractSuitesFrom(jarFile));323 }324 /** Define the number of threads in the thread pool. */325 public void setThreadCount(int threadCount) {326 if (threadCount < 1) {327 exitWithError("Cannot use a threadCount parameter less than 1; 1 > " + threadCount);328 }329 m_threadCount = threadCount;330 }331 /**332 * Define whether this run will be run in parallel mode.333 *334 * @deprecated Use #setParallel(XmlSuite.ParallelMode) instead335 */336 @Deprecated337 // TODO: krmahadevan: This method is being used by Gradle. Removal causes build failures.338 public void setParallel(String parallel) {339 if (parallel == null) {340 setParallel(XmlSuite.ParallelMode.NONE);341 } else {342 setParallel(XmlSuite.ParallelMode.getValidParallel(parallel));343 }344 }345 public void setParallel(XmlSuite.ParallelMode parallel) {346 m_parallelMode = skipDeprecatedValues(parallel);347 }348 public void setCommandLineSuite(XmlSuite suite) {349 m_cmdlineSuites = Lists.newArrayList();350 m_cmdlineSuites.add(suite);351 m_suites.add(suite);352 }353 /**354 * Set the test classes to be run by this TestNG object. This method will create a dummy suite355 * that will wrap these classes called "Command Line Test".356 *357 * <p>If used together with threadCount, parallel, groups, excludedGroups than this one must be358 * set first.359 *360 * @param classes An array of classes that contain TestNG annotations.361 */362 public void setTestClasses(Class[] classes) {363 m_suites.clear();364 m_commandLineTestClasses = classes;365 }366 /**367 * Given a string com.example.Foo.f1, return an array where [0] is the class and [1] is the368 * method.369 */370 private String[] splitMethod(String m) {371 int index = m.lastIndexOf(".");372 if (index < 0) {373 throw new TestNGException(374 "Bad format for command line method:" + m + ", expected <class>.<method>");375 }376 return new String[] {m.substring(0, index), m.substring(index + 1).replaceAll("\\*", "\\.\\*")};377 }378 /**379 * @return a list of XmlSuite objects that represent the list of classes and methods passed in380 * parameter.381 * @param commandLineMethods a string with the form "com.example.Foo.f1,com.example.Bar.f2"382 */383 private List<XmlSuite> createCommandLineSuitesForMethods(List<String> commandLineMethods) {384 //385 // Create the <classes> tag386 //387 Set<Class> classes = Sets.newHashSet();388 for (String m : commandLineMethods) {389 Class c = ClassHelper.forName(splitMethod(m)[0]);390 if (c != null) {391 classes.add(c);392 }393 }394 List<XmlSuite> result = createCommandLineSuitesForClasses(classes.toArray(new Class[0]));395 //396 // Add the method tags397 //398 List<XmlClass> xmlClasses = Lists.newArrayList();399 for (XmlSuite s : result) {400 for (XmlTest t : s.getTests()) {401 xmlClasses.addAll(t.getClasses());402 }403 }404 for (XmlClass xc : xmlClasses) {405 for (String m : commandLineMethods) {406 String[] split = splitMethod(m);407 String className = split[0];408 if (xc.getName().equals(className)) {409 XmlInclude includedMethod = new XmlInclude(split[1]);410 xc.getIncludedMethods().add(includedMethod);411 }412 }413 }414 return result;415 }416 private List<XmlSuite> createCommandLineSuitesForClasses(Class[] classes) {417 //418 // See if any of the classes has an xmlSuite or xmlTest attribute.419 // If it does, create the appropriate XmlSuite, otherwise, create420 // the default one421 //422 XmlClass[] xmlClasses = Utils.classesToXmlClasses(classes);423 Map<String, XmlSuite> suites = Maps.newHashMap();424 IAnnotationFinder finder = m_configuration.getAnnotationFinder();425 for (int i = 0; i < classes.length; i++) {426 Class c = classes[i];427 ITestAnnotation test = finder.findAnnotation(c, ITestAnnotation.class);428 String suiteName = getDefaultSuiteName();429 String testName = getDefaultTestName();430 boolean isJUnit = false;431 if (test != null) {432 suiteName = defaultIfStringEmpty(test.getSuiteName(), suiteName);433 testName = defaultIfStringEmpty(test.getTestName(), testName);434 } else {435 if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {436 isJUnit = true;437 testName = c.getName();438 }439 }440 XmlSuite xmlSuite = suites.get(suiteName);441 if (xmlSuite == null) {442 xmlSuite = new XmlSuite();443 xmlSuite.setName(suiteName);444 suites.put(suiteName, xmlSuite);445 }446 if (m_dataProviderThreadCount != null) {447 xmlSuite.setDataProviderThreadCount(m_dataProviderThreadCount);448 }449 XmlTest xmlTest = null;450 for (XmlTest xt : xmlSuite.getTests()) {451 if (xt.getName().equals(testName)) {452 xmlTest = xt;453 break;454 }455 }456 if (xmlTest == null) {457 xmlTest = new XmlTest(xmlSuite);458 xmlTest.setName(testName);459 xmlTest.setJUnit(isJUnit);460 }461 xmlTest.getXmlClasses().add(xmlClasses[i]);462 }463 return new ArrayList<>(suites.values());464 }465 public void addMethodSelector(String className, int priority) {466 if (Strings.isNotNullAndNotEmpty(className)) {467 m_methodDescriptors.put(className, priority);468 }469 }470 public void addMethodSelector(XmlMethodSelector selector) {471 m_selectors.add(selector);472 }473 /**474 * Set the suites file names to be run by this TestNG object. This method tries to load and parse475 * the specified TestNG suite xml files. If a file is missing, it is ignored.476 *477 * @param suites A list of paths to one more XML files defining the tests. For example:478 * <pre>479 * TestNG tng = new TestNG();480 * List<String> suites = Lists.newArrayList();481 * suites.add("c:/tests/testng1.xml");482 * suites.add("c:/tests/testng2.xml");483 * tng.setTestSuites(suites);484 * tng.run();485 * </pre>486 */487 public void setTestSuites(List<String> suites) {488 m_stringSuites = suites;489 }490 /**491 * Specifies the XmlSuite objects to run.492 *493 * @param suites494 * @see org.testng.xml.XmlSuite495 */496 public void setXmlSuites(List<XmlSuite> suites) {497 m_suites = suites;498 }499 /**500 * Define which groups will be excluded from this run.501 *502 * @param groups A list of group names separated by a comma.503 */504 public void setExcludedGroups(String groups) {505 m_excludedGroups = Utils.split(groups, ",");506 }507 /**508 * Define which groups will be included from this run.509 *510 * @param groups A list of group names separated by a comma.511 */512 public void setGroups(String groups) {513 m_includedGroups = Utils.split(groups, ",");514 }515 private void setTestRunnerFactoryClass(Class testRunnerFactoryClass) {516 setTestRunnerFactory((ITestRunnerFactory) ClassHelper.newInstance(testRunnerFactoryClass));517 }518 protected void setTestRunnerFactory(ITestRunnerFactory itrf) {519 m_testRunnerFactory = itrf;520 }521 public void setObjectFactory(Class c) {522 m_objectFactory = (ITestObjectFactory) ClassHelper.newInstance(c);523 }524 public void setObjectFactory(ITestObjectFactory factory) {525 m_objectFactory = factory;526 }527 /**528 * Define which listeners to user for this run.529 *530 * @param classes A list of classes, which must be either ISuiteListener, ITestListener or531 * IReporter532 */533 public void setListenerClasses(List<Class<? extends ITestNGListener>> classes) {534 for (Class<? extends ITestNGListener> cls : classes) {535 addListener(ClassHelper.newInstance(cls));536 }537 }538 /** @deprecated Use addListener(ITestNGListener) instead */539 // TODO remove later /!\ Caution: IntelliJ is using it. Check with @akozlova before removing it540 @Deprecated541 public void addListener(Object listener) {542 if (!(listener instanceof ITestNGListener)) {543 exitWithError(544 "Listener "545 + listener546 + " must be one of ITestListener, ISuiteListener, IReporter, "547 + " IAnnotationTransformer, IMethodInterceptor or IInvokedMethodListener");548 }549 addListener((ITestNGListener) listener);550 }551 private static <E> void maybeAddListener(Map<Class<? extends E>, E> map, E value) {552 maybeAddListener(map, (Class<? extends E>) value.getClass(), value, false);553 }554 private static <E> void maybeAddListener(555 Map<Class<? extends E>, E> map, Class<? extends E> type, E value, boolean quiet) {556 if (map.containsKey(type)) {557 if (!quiet) {558 LOGGER.warn("Ignoring duplicate listener : " + type.getName());559 }560 } else {561 map.put(type, value);562 }563 }564 public void addListener(ITestNGListener listener) {565 if (listener == null) {566 return;567 }568 if (listener instanceof IExecutionVisualiser) {569 IExecutionVisualiser visualiser = (IExecutionVisualiser) listener;570 maybeAddListener(m_executionVisualisers, visualiser);571 }572 if (listener instanceof ISuiteListener) {573 ISuiteListener suite = (ISuiteListener) listener;574 maybeAddListener(m_suiteListeners, suite);575 }576 if (listener instanceof ITestListener) {577 ITestListener test = (ITestListener) listener;578 maybeAddListener(m_testListeners, test);579 }580 if (listener instanceof IClassListener) {581 IClassListener clazz = (IClassListener) listener;582 maybeAddListener(m_classListeners, clazz);583 }584 if (listener instanceof IReporter) {585 IReporter reporter = (IReporter) listener;586 maybeAddListener(m_reporters, reporter);587 }588 if (listener instanceof IAnnotationTransformer) {589 setAnnotationTransformer((IAnnotationTransformer) listener);590 }591 if (listener instanceof IMethodInterceptor) {592 m_methodInterceptors.add((IMethodInterceptor) listener);593 }594 if (listener instanceof IInvokedMethodListener) {595 IInvokedMethodListener method = (IInvokedMethodListener) listener;596 maybeAddListener(m_invokedMethodListeners, method);597 }598 if (listener instanceof IHookable) {599 setHookable((IHookable) listener);600 }601 if (listener instanceof IConfigurable) {602 setConfigurable((IConfigurable) listener);603 }604 if (listener instanceof IExecutionListener) {605 m_configuration.addExecutionListenerIfAbsent((IExecutionListener) listener);606 }607 if (listener instanceof IConfigurationListener) {608 m_configuration.addConfigurationListener((IConfigurationListener) listener);609 }610 if (listener instanceof IAlterSuiteListener) {611 IAlterSuiteListener alter = (IAlterSuiteListener) listener;612 maybeAddListener(m_alterSuiteListeners, alter);613 }614 if (listener instanceof IDataProviderListener) {615 IDataProviderListener dataProvider = (IDataProviderListener) listener;616 maybeAddListener(m_dataProviderListeners, dataProvider);617 }618 }619 public Set<IReporter> getReporters() {620 // This will now cause a different behavior for consumers of this method because unlike before621 // they are no longer622 // going to be getting the original set but only a copy of it (since we internally moved from623 // Sets to Maps)624 return Sets.newHashSet(m_reporters.values());625 }626 public List<ITestListener> getTestListeners() {627 return Lists.newArrayList(m_testListeners.values());628 }629 public List<ISuiteListener> getSuiteListeners() {630 return Lists.newArrayList(m_suiteListeners.values());631 }632 /** If m_verbose gets set, it will override the verbose setting in testng.xml */633 private Integer m_verbose = null;634 private final IAnnotationTransformer m_defaultAnnoProcessor = new DefaultAnnotationTransformer();635 private IAnnotationTransformer m_annotationTransformer = m_defaultAnnoProcessor;636 private Boolean m_skipFailedInvocationCounts = false;637 private List<IMethodInterceptor> m_methodInterceptors = Lists.newArrayList();638 /** The list of test names to run from the given suite */639 private List<String> m_testNames;640 private Integer m_suiteThreadPoolSize = CommandLineArgs.SUITE_THREAD_POOL_SIZE_DEFAULT;641 private boolean m_randomizeSuites = Boolean.FALSE;642 private Boolean m_preserveOrder = XmlSuite.DEFAULT_PRESERVE_ORDER;643 private Boolean m_groupByInstances;644 private IConfiguration m_configuration;645 /**646 * Sets the level of verbosity. This value will override the value specified in the test suites.647 *648 * @param verbose the verbosity level (0 to 10 where 10 is most detailed) Actually, this is a lie:649 * you can specify -1 and this will put TestNG in debug mode (no longer slicing off stack650 * traces and all).651 */652 public void setVerbose(int verbose) {653 m_verbose = verbose;654 }655 private void initializeCommandLineSuites() {656 if (m_commandLineTestClasses != null || m_commandLineMethods != null) {657 if (null != m_commandLineMethods) {658 m_cmdlineSuites = createCommandLineSuitesForMethods(m_commandLineMethods);659 } else {660 m_cmdlineSuites = createCommandLineSuitesForClasses(m_commandLineTestClasses);661 }662 for (XmlSuite s : m_cmdlineSuites) {663 for (XmlTest t : s.getTests()) {664 t.setPreserveOrder(m_preserveOrder);665 }666 m_suites.add(s);667 if (m_groupByInstances != null) {668 s.setGroupByInstances(m_groupByInstances);669 }670 }671 }672 }673 private void initializeCommandLineSuitesParams() {674 if (null == m_cmdlineSuites) {675 return;676 }677 for (XmlSuite s : m_cmdlineSuites) {678 if (m_threadCount != -1) {679 s.setThreadCount(m_threadCount);680 }681 if (m_parallelMode != null) {682 s.setParallel(m_parallelMode);683 }684 if (m_configFailurePolicy != null) {685 s.setConfigFailurePolicy(m_configFailurePolicy);686 }687 }688 }689 private void initializeCommandLineSuitesGroups() {690 // If groups were specified on the command line, they should override groups691 // specified in the XML file692 boolean hasIncludedGroups = null != m_includedGroups && m_includedGroups.length > 0;693 boolean hasExcludedGroups = null != m_excludedGroups && m_excludedGroups.length > 0;694 List<XmlSuite> suites = m_cmdlineSuites != null ? m_cmdlineSuites : m_suites;695 if (hasIncludedGroups || hasExcludedGroups) {696 for (XmlSuite s : suites) {697 initializeCommandLineSuitesGroups(698 s, hasIncludedGroups, m_includedGroups, hasExcludedGroups, m_excludedGroups);699 }700 }701 }702 private static void initializeCommandLineSuitesGroups(703 XmlSuite s,704 boolean hasIncludedGroups,705 String[] m_includedGroups,706 boolean hasExcludedGroups,707 String[] m_excludedGroups) {708 if (hasIncludedGroups) {709 s.setIncludedGroups(Arrays.asList(m_includedGroups));710 }711 if (hasExcludedGroups) {712 s.setExcludedGroups(Arrays.asList(m_excludedGroups));713 }714 for (XmlSuite child : s.getChildSuites()) {715 initializeCommandLineSuitesGroups(716 child, hasIncludedGroups, m_includedGroups, hasExcludedGroups, m_excludedGroups);717 }718 }719 private void addReporter(Class<? extends IReporter> r) {720 if (!m_reporters.containsKey(r)) {721 m_reporters.put(r, ClassHelper.newInstance(r));722 }723 }724 private void initializeDefaultListeners() {725 addListener(this.exitCodeListener);726 if (m_useDefaultListeners) {727 addReporter(SuiteHTMLReporter.class);728 addReporter(Main.class);729 addReporter(FailedReporter.class);730 addReporter(XMLReporter.class);731 if (RuntimeBehavior.useOldTestNGEmailableReporter()) {732 addReporter(EmailableReporter.class);733 } else if (RuntimeBehavior.useEmailableReporter()) {734 addReporter(EmailableReporter2.class);735 }736 addReporter(JUnitReportReporter.class);737 if (m_verbose != null && m_verbose > 4) {738 addListener(new VerboseReporter("[TestNG] "));739 }740 }741 }742 private void initializeConfiguration() {743 ITestObjectFactory factory = m_objectFactory;744 //745 // Install the listeners found in ServiceLoader (or use the class746 // loader for tests, if specified).747 //748 addServiceLoaderListeners();749 //750 // Install the listeners found in the suites751 //752 for (XmlSuite s : m_suites) {753 addListeners(s);754 //755 // Install the method selectors756 //757 for (XmlMethodSelector methodSelector : s.getMethodSelectors()) {758 addMethodSelector(methodSelector.getClassName(), methodSelector.getPriority());759 addMethodSelector(methodSelector);760 }761 //762 // Find if we have an object factory763 //764 if (s.getObjectFactory() != null) {765 if (factory == null) {766 factory = s.getObjectFactory();767 } else {768 throw new TestNGException("Found more than one object-factory tag in your suites");769 }770 }771 }772 m_configuration.setAnnotationFinder(new JDK15AnnotationFinder(getAnnotationTransformer()));773 m_configuration.setHookable(m_hookable);774 m_configuration.setConfigurable(m_configurable);775 m_configuration.setObjectFactory(factory);776 }777 private void addListeners(XmlSuite s) {778 for (String listenerName : s.getListeners()) {779 Class<?> listenerClass = ClassHelper.forName(listenerName);780 // If specified listener does not exist, a TestNGException will be thrown781 if (listenerClass == null) {782 throw new TestNGException(783 "Listener " + listenerName + " was not found in project's classpath");784 }785 ITestNGListener listener = (ITestNGListener) ClassHelper.newInstance(listenerClass);786 addListener(listener);787 }788 // Add the child suite listeners789 List<XmlSuite> childSuites = s.getChildSuites();790 for (XmlSuite c : childSuites) {791 addListeners(c);792 }793 }794 /** Using reflection to remain Java 5 compliant. */795 private void addServiceLoaderListeners() {796 Iterable<ITestNGListener> loader =797 m_serviceLoaderClassLoader != null798 ? ServiceLoader.load(ITestNGListener.class, m_serviceLoaderClassLoader)799 : ServiceLoader.load(ITestNGListener.class);800 for (ITestNGListener l : loader) {801 Utils.log("[TestNG]", 2, "Adding ServiceLoader listener:" + l);802 addListener(l);803 addServiceLoaderListener(l);804 }805 }806 /**807 * Before suites are executed, do a sanity check to ensure all required conditions are met. If808 * not, throw an exception to stop test execution809 *810 * @throws TestNGException if the sanity check fails811 */812 private void sanityCheck() {813 XmlSuiteUtils.validateIfSuitesContainDuplicateTests(m_suites);814 XmlSuiteUtils.adjustSuiteNamesToEnsureUniqueness(m_suites);815 }816 /** Invoked by the remote runner. */817 public void initializeEverything() {818 // The Eclipse plug-in (RemoteTestNG) might have invoked this method already819 // so don't initialize suites twice.820 if (m_isInitialized) {821 return;822 }823 initializeSuitesAndJarFile();824 initializeConfiguration();825 initializeDefaultListeners();826 initializeCommandLineSuites();827 initializeCommandLineSuitesParams();828 initializeCommandLineSuitesGroups();829 m_isInitialized = true;830 }831 /** Run TestNG. */832 public void run() {833 initializeEverything();834 sanityCheck();835 runExecutionListeners(true /* start */);836 runSuiteAlterationListeners();837 m_start = System.currentTimeMillis();838 List<ISuite> suiteRunners = runSuites();839 m_end = System.currentTimeMillis();840 if (null != suiteRunners) {841 generateReports(suiteRunners);842 }843 runExecutionListeners(false /* finish */);844 exitCode = this.exitCodeListener.getStatus();845 if (exitCodeListener.noTestsFound()) {846 if (TestRunner.getVerbose() > 1) {847 System.err.println("[TestNG] No tests found. Nothing was run");848 usage();849 }850 }851 m_instance = null;852 m_jCommander = null;853 }854 /**855 * Run the test suites.856 *857 * <p>This method can be overridden by subclass. <br>858 * For example, DistributedTestNG to run in master/slave mode according to commandline args.859 *860 * @return - List of suites that were run as {@link ISuite} objects.861 * @since 6.9.11 when moving distributed/remote classes out into separate project862 */863 protected List<ISuite> runSuites() {864 return runSuitesLocally();865 }866 private void runSuiteAlterationListeners() {867 for (IAlterSuiteListener l : m_alterSuiteListeners.values()) {868 l.alter(m_suites);869 }870 }871 private void runExecutionListeners(boolean start) {872 for (IExecutionListener l : m_configuration.getExecutionListeners()) {873 if (start) {874 l.onExecutionStart();875 } else {876 l.onExecutionFinish();877 }878 }879 }880 private static void usage() {881 if (m_jCommander == null) {882 m_jCommander = new JCommander(new CommandLineArgs());883 }884 m_jCommander.usage();885 }886 private void generateReports(List<ISuite> suiteRunners) {887 for (IReporter reporter : m_reporters.values()) {888 try {889 long start = System.currentTimeMillis();890 reporter.generateReport(m_suites, suiteRunners, m_outputDir);891 Utils.log(892 "TestNG",893 2,894 "Time taken by " + reporter + ": " + (System.currentTimeMillis() - start) + " ms");895 } catch (Exception ex) {896 System.err.println("[TestNG] Reporter " + reporter + " failed");897 ex.printStackTrace(System.err);898 }899 }900 }901 /**902 * This needs to be public for maven2, for now..At least until an alternative mechanism is found.903 */904 public List<ISuite> runSuitesLocally() {905 if (m_suites.isEmpty()) {906 error("No test suite found. Nothing to run");907 usage();908 return Collections.emptyList();909 }910 SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap();911 if (m_suites.get(0).getVerbose() >= 2) {912 Version.displayBanner();913 }914 // First initialize the suite runners to ensure there are no configuration issues.915 // Create a map with XmlSuite as key and corresponding SuiteRunner as value916 for (XmlSuite xmlSuite : m_suites) {917 createSuiteRunners(suiteRunnerMap, xmlSuite);918 }919 //920 // Run suites921 //922 if (m_suiteThreadPoolSize == 1 && !m_randomizeSuites) {923 // Single threaded and not randomized: run the suites in order924 for (XmlSuite xmlSuite : m_suites) {925 runSuitesSequentially(926 xmlSuite, suiteRunnerMap, getVerbose(xmlSuite), getDefaultSuiteName());927 }928 //929 // Generate the suites report930 //931 return Lists.newArrayList(suiteRunnerMap.values());932 }933 // Multithreaded: generate a dynamic graph that stores the suite hierarchy. This is then934 // used to run related suites in specific order. Parent suites are run only935 // once all the child suites have completed execution936 DynamicGraph<ISuite> suiteGraph = new DynamicGraph<>();937 for (XmlSuite xmlSuite : m_suites) {938 populateSuiteGraph(suiteGraph, suiteRunnerMap, xmlSuite);939 }940 IThreadWorkerFactory<ISuite> factory =941 new SuiteWorkerFactory(942 suiteRunnerMap, 0 /* verbose hasn't been set yet */, getDefaultSuiteName());943 GraphThreadPoolExecutor<ISuite> pooledExecutor =944 new GraphThreadPoolExecutor<>(945 "suites",946 suiteGraph,947 factory,948 m_suiteThreadPoolSize,949 m_suiteThreadPoolSize,950 Integer.MAX_VALUE,951 TimeUnit.MILLISECONDS,952 new LinkedBlockingQueue<>());953 Utils.log("TestNG", 2, "Starting executor for all suites");954 // Run all suites in parallel955 pooledExecutor.run();956 try {957 pooledExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);958 pooledExecutor.shutdownNow();959 } catch (InterruptedException handled) {960 Thread.currentThread().interrupt();961 error("Error waiting for concurrent executors to finish " + handled.getMessage());962 }963 //964 // Generate the suites report965 //966 return Lists.newArrayList(suiteRunnerMap.values());967 }968 private static void error(String s) {969 LOGGER.error(s);970 }971 /**972 * @return the verbose level, checking in order: the verbose level on the suite, the verbose level973 * on the TestNG object, or 1.974 */975 private int getVerbose(XmlSuite xmlSuite) {976 return xmlSuite.getVerbose() != null977 ? xmlSuite.getVerbose()978 : (m_verbose != null ? m_verbose : DEFAULT_VERBOSE);979 }980 /**981 * Recursively runs suites. Runs the children suites before running the parent suite. This is done982 * so that the results for parent suite can reflect the combined results of the children suites.983 *984 * @param xmlSuite XML Suite to be executed985 * @param suiteRunnerMap Maps {@code XmlSuite}s to respective {@code ISuite}986 * @param verbose verbose level987 * @param defaultSuiteName default suite name988 */989 private void runSuitesSequentially(990 XmlSuite xmlSuite, SuiteRunnerMap suiteRunnerMap, int verbose, String defaultSuiteName) {991 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {992 runSuitesSequentially(childSuite, suiteRunnerMap, verbose, defaultSuiteName);993 }994 SuiteRunnerWorker srw =995 new SuiteRunnerWorker(996 suiteRunnerMap.get(xmlSuite), suiteRunnerMap, verbose, defaultSuiteName);997 srw.run();998 }999 /**1000 * Populates the dynamic graph with the reverse hierarchy of suites. Edges are added pointing from1001 * child suite runners to parent suite runners, hence making parent suite runners dependent on all1002 * the child suite runners1003 *1004 * @param suiteGraph dynamic graph representing the reverse hierarchy of SuiteRunners1005 * @param suiteRunnerMap Map with XMLSuite as key and its respective SuiteRunner as value1006 * @param xmlSuite XML Suite1007 */1008 private void populateSuiteGraph(1009 DynamicGraph<ISuite> suiteGraph /* OUT */, SuiteRunnerMap suiteRunnerMap, XmlSuite xmlSuite) {1010 ISuite parentSuiteRunner = suiteRunnerMap.get(xmlSuite);1011 if (xmlSuite.getChildSuites().isEmpty()) {1012 suiteGraph.addNode(parentSuiteRunner);1013 } else {1014 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1015 suiteGraph.addEdge(0, parentSuiteRunner, suiteRunnerMap.get(childSuite));1016 populateSuiteGraph(suiteGraph, suiteRunnerMap, childSuite);1017 }1018 }1019 }1020 /**1021 * Creates the {@code SuiteRunner}s and populates the suite runner map with this information1022 *1023 * @param suiteRunnerMap Map with XMLSuite as key and it's respective SuiteRunner as value. This1024 * is updated as part of this method call1025 * @param xmlSuite Xml Suite (and its children) for which {@code SuiteRunner}s are created1026 */1027 private void createSuiteRunners(SuiteRunnerMap suiteRunnerMap /* OUT */, XmlSuite xmlSuite) {1028 if (null != m_isJUnit && !m_isJUnit.equals(XmlSuite.DEFAULT_JUNIT)) {1029 xmlSuite.setJUnit(m_isJUnit);1030 }1031 // If the skip flag was invoked on the command line, it1032 // takes precedence1033 if (null != m_skipFailedInvocationCounts) {1034 xmlSuite.setSkipFailedInvocationCounts(m_skipFailedInvocationCounts);1035 }1036 // Override the XmlSuite verbose value with the one from TestNG1037 if (m_verbose != null) {1038 xmlSuite.setVerbose(m_verbose);1039 }1040 if (null != m_configFailurePolicy) {1041 xmlSuite.setConfigFailurePolicy(m_configFailurePolicy);1042 }1043 for (XmlTest t : xmlSuite.getTests()) {1044 for (Map.Entry<String, Integer> ms : m_methodDescriptors.entrySet()) {1045 XmlMethodSelector xms = new XmlMethodSelector();1046 xms.setName(ms.getKey());1047 xms.setPriority(ms.getValue());1048 t.getMethodSelectors().add(xms);1049 }1050 for (XmlMethodSelector selector : m_selectors) {1051 t.getMethodSelectors().add(selector);1052 }1053 }1054 suiteRunnerMap.put(xmlSuite, createSuiteRunner(xmlSuite));1055 for (XmlSuite childSuite : xmlSuite.getChildSuites()) {1056 createSuiteRunners(suiteRunnerMap, childSuite);1057 }1058 }1059 /** Creates a suite runner and configures its initial state */1060 private SuiteRunner createSuiteRunner(XmlSuite xmlSuite) {1061 SuiteRunner result =1062 new SuiteRunner(1063 getConfiguration(),1064 xmlSuite,1065 m_outputDir,1066 m_testRunnerFactory,1067 m_useDefaultListeners,1068 m_methodInterceptors,1069 m_invokedMethodListeners.values(),1070 m_testListeners.values(),1071 m_classListeners.values(),1072 m_dataProviderListeners,1073 Systematiser.getComparator());1074 for (ISuiteListener isl : m_suiteListeners.values()) {1075 result.addListener(isl);1076 }1077 for (IReporter r : result.getReporters()) {1078 maybeAddListener(m_reporters, r.getClass(), r, true);1079 }1080 for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {1081 result.addConfigurationListener(cl);1082 }1083 m_executionVisualisers.values().forEach(result::addListener);1084 return result;1085 }1086 protected IConfiguration getConfiguration() {1087 return m_configuration;1088 }1089 /**1090 * The TestNG entry point for command line execution.1091 *1092 * @param argv the TestNG command line parameters.1093 */1094 public static void main(String[] argv) {1095 TestNG testng = privateMain(argv, null);1096 System.exit(testng.getStatus());1097 }1098 /**1099 * <B>Note</B>: this method is not part of the public API and is meant for internal usage only.1100 */1101 public static TestNG privateMain(String[] argv, ITestListener listener) {1102 TestNG result = new TestNG();1103 if (null != listener) {1104 result.addListener(listener);1105 }1106 //1107 // Parse the arguments1108 //1109 try {1110 CommandLineArgs cla = new CommandLineArgs();1111 m_jCommander = new JCommander(cla, argv);1112 validateCommandLineParameters(cla);1113 result.configure(cla);1114 } catch (ParameterException ex) {1115 exitWithError(ex.getMessage());1116 }1117 //1118 // Run1119 //1120 try {1121 result.run();1122 } catch (TestNGException ex) {1123 if (TestRunner.getVerbose() > 1) {1124 ex.printStackTrace(System.out);1125 } else {1126 error(ex.getMessage());1127 }1128 result.exitCode = ExitCode.newExitCodeRepresentingFailure();1129 }1130 return result;1131 }1132 /** Configure the TestNG instance based on the command line parameters. */1133 protected void configure(CommandLineArgs cla) {1134 if (cla.verbose != null) {1135 setVerbose(cla.verbose);1136 }1137 setOutputDirectory(cla.outputDirectory);1138 String testClasses = cla.testClass;1139 if (null != testClasses) {1140 String[] strClasses = testClasses.split(",");1141 List<Class> classes = Lists.newArrayList();1142 for (String c : strClasses) {1143 classes.add(ClassHelper.fileToClass(c));1144 }1145 setTestClasses(classes.toArray(new Class[0]));1146 }1147 setOutputDirectory(cla.outputDirectory);1148 if (cla.testNames != null) {1149 setTestNames(Arrays.asList(cla.testNames.split(",")));1150 }1151 // Note: can't use a Boolean field here because we are allowing a boolean1152 // parameter with an arity of 1 ("-usedefaultlisteners false")1153 if (cla.useDefaultListeners != null) {1154 setUseDefaultListeners("true".equalsIgnoreCase(cla.useDefaultListeners));1155 }1156 setGroups(cla.groups);1157 setExcludedGroups(cla.excludedGroups);1158 setTestJar(cla.testJar);1159 setXmlPathInJar(cla.xmlPathInJar);1160 setJUnit(cla.junit);1161 setMixed(cla.mixed);1162 setSkipFailedInvocationCounts(cla.skipFailedInvocationCounts);1163 if (cla.parallelMode != null) {1164 setParallel(cla.parallelMode);1165 }1166 if (cla.configFailurePolicy != null) {1167 setConfigFailurePolicy(XmlSuite.FailurePolicy.getValidPolicy(cla.configFailurePolicy));1168 }1169 if (cla.threadCount != null) {1170 setThreadCount(cla.threadCount);1171 }1172 if (cla.dataProviderThreadCount != null) {1173 setDataProviderThreadCount(cla.dataProviderThreadCount);1174 }1175 if (cla.suiteName != null) {1176 setDefaultSuiteName(cla.suiteName);1177 }1178 if (cla.testName != null) {1179 setDefaultTestName(cla.testName);1180 }1181 if (cla.listener != null) {1182 String sep = ";";1183 if (cla.listener.contains(",")) {1184 sep = ",";1185 }1186 String[] strs = Utils.split(cla.listener, sep);1187 List<Class<? extends ITestNGListener>> classes = Lists.newArrayList();1188 for (String cls : strs) {1189 Class<?> clazz = ClassHelper.fileToClass(cls);1190 if (ITestNGListener.class.isAssignableFrom(clazz)) {1191 classes.add((Class<? extends ITestNGListener>) clazz);1192 }1193 }1194 setListenerClasses(classes);1195 }1196 if (null != cla.methodSelectors) {1197 String[] strs = Utils.split(cla.methodSelectors, ",");1198 for (String cls : strs) {1199 String[] sel = Utils.split(cls, ":");1200 try {1201 if (sel.length == 2) {1202 addMethodSelector(sel[0], Integer.parseInt(sel[1]));1203 } else {1204 error("Method selector value was not in the format org.example.Selector:4");1205 }1206 } catch (NumberFormatException nfe) {1207 error("Method selector value was not in the format org.example.Selector:4");1208 }1209 }1210 }1211 if (cla.objectFactory != null) {1212 setObjectFactory(ClassHelper.fileToClass(cla.objectFactory));1213 }1214 if (cla.testRunnerFactory != null) {1215 setTestRunnerFactoryClass(ClassHelper.fileToClass(cla.testRunnerFactory));1216 }1217 if (cla.reporter != null) {1218 ReporterConfig reporterConfig = ReporterConfig.deserialize(cla.reporter);1219 addReporter(reporterConfig);1220 }1221 if (cla.commandLineMethods.size() > 0) {1222 m_commandLineMethods = cla.commandLineMethods;1223 }1224 if (cla.suiteFiles != null) {1225 setTestSuites(cla.suiteFiles);1226 }1227 setSuiteThreadPoolSize(cla.suiteThreadPoolSize);1228 setRandomizeSuites(cla.randomizeSuites);1229 }1230 public void setSuiteThreadPoolSize(Integer suiteThreadPoolSize) {1231 m_suiteThreadPoolSize = suiteThreadPoolSize;1232 }1233 public Integer getSuiteThreadPoolSize() {1234 return m_suiteThreadPoolSize;1235 }1236 public void setRandomizeSuites(boolean randomizeSuites) {1237 m_randomizeSuites = randomizeSuites;1238 }1239 /**1240 * This method is invoked by Maven's Surefire, only remove it once Surefire has been modified to1241 * no longer call it.1242 */1243 public void setSourcePath(String path) {1244 // nop1245 }1246 private static int parseInt(Object value) {1247 if (value == null) {1248 return -1;1249 }1250 if (value instanceof String) {1251 return Integer.parseInt(String.valueOf(value));1252 }1253 if (value instanceof Integer) {1254 return (Integer) value;1255 }1256 throw new IllegalArgumentException("Unable to parse " + value + " as an Integer.");1257 }1258 /**1259 * This method is invoked by Maven's Surefire to configure the runner, do not remove unless you1260 * know for sure that Surefire has been updated to use the new configure(CommandLineArgs) method.1261 *1262 * @deprecated use new configure(CommandLineArgs) method1263 */1264 @SuppressWarnings({"unchecked"})1265 @Deprecated1266 public void configure(Map cmdLineArgs) {1267 CommandLineArgs result = new CommandLineArgs();1268 int value = parseInt(cmdLineArgs.get(CommandLineArgs.LOG));1269 if (value != -1) {1270 result.verbose = value;1271 }1272 result.outputDirectory = (String) cmdLineArgs.get(CommandLineArgs.OUTPUT_DIRECTORY);1273 String testClasses = (String) cmdLineArgs.get(CommandLineArgs.TEST_CLASS);1274 if (null != testClasses) {1275 result.testClass = testClasses;1276 }1277 String testNames = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAMES);1278 if (testNames != null) {1279 result.testNames = testNames;1280 }1281 String useDefaultListeners = (String) cmdLineArgs.get(CommandLineArgs.USE_DEFAULT_LISTENERS);1282 if (null != useDefaultListeners) {1283 result.useDefaultListeners = useDefaultListeners;1284 }1285 result.groups = (String) cmdLineArgs.get(CommandLineArgs.GROUPS);1286 result.excludedGroups = (String) cmdLineArgs.get(CommandLineArgs.EXCLUDED_GROUPS);1287 result.testJar = (String) cmdLineArgs.get(CommandLineArgs.TEST_JAR);1288 result.xmlPathInJar = (String) cmdLineArgs.get(CommandLineArgs.XML_PATH_IN_JAR);1289 result.junit = (Boolean) cmdLineArgs.get(CommandLineArgs.JUNIT);1290 result.mixed = (Boolean) cmdLineArgs.get(CommandLineArgs.MIXED);1291 result.skipFailedInvocationCounts =1292 (Boolean) cmdLineArgs.get(CommandLineArgs.SKIP_FAILED_INVOCATION_COUNTS);1293 String parallelMode = (String) cmdLineArgs.get(CommandLineArgs.PARALLEL);1294 if (parallelMode != null) {1295 result.parallelMode = XmlSuite.ParallelMode.getValidParallel(parallelMode);1296 }1297 value = parseInt(cmdLineArgs.get(CommandLineArgs.THREAD_COUNT));1298 if (value != -1) {1299 result.threadCount = value;1300 }1301 // Not supported by Surefire yet1302 value = parseInt(cmdLineArgs.get(CommandLineArgs.DATA_PROVIDER_THREAD_COUNT));1303 if (value != -1) {1304 result.dataProviderThreadCount = value;1305 }1306 String defaultSuiteName = (String) cmdLineArgs.get(CommandLineArgs.SUITE_NAME);1307 if (defaultSuiteName != null) {1308 result.suiteName = defaultSuiteName;1309 }1310 String defaultTestName = (String) cmdLineArgs.get(CommandLineArgs.TEST_NAME);1311 if (defaultTestName != null) {1312 result.testName = defaultTestName;1313 }1314 Object listeners = cmdLineArgs.get(CommandLineArgs.LISTENER);1315 if (listeners instanceof List) {1316 result.listener = Utils.join((List<?>) listeners, ",");1317 } else {1318 result.listener = (String) listeners;1319 }1320 String ms = (String) cmdLineArgs.get(CommandLineArgs.METHOD_SELECTORS);1321 if (null != ms) {1322 result.methodSelectors = ms;1323 }1324 String objectFactory = (String) cmdLineArgs.get(CommandLineArgs.OBJECT_FACTORY);1325 if (null != objectFactory) {1326 result.objectFactory = objectFactory;1327 }1328 String runnerFactory = (String) cmdLineArgs.get(CommandLineArgs.TEST_RUNNER_FACTORY);1329 if (null != runnerFactory) {1330 result.testRunnerFactory = runnerFactory;1331 }1332 String reporterConfigs = (String) cmdLineArgs.get(CommandLineArgs.REPORTER);1333 if (reporterConfigs != null) {1334 result.reporter = reporterConfigs;1335 }1336 String failurePolicy = (String) cmdLineArgs.get(CommandLineArgs.CONFIG_FAILURE_POLICY);1337 if (failurePolicy != null) {1338 result.configFailurePolicy = failurePolicy;1339 }1340 value = parseInt(cmdLineArgs.get(CommandLineArgs.SUITE_THREAD_POOL_SIZE));1341 if (value != -1) {1342 result.suiteThreadPoolSize = value;1343 }1344 configure(result);1345 }1346 /** Only run the specified tests from the suite. */1347 public void setTestNames(List<String> testNames) {1348 m_testNames = testNames;1349 }1350 public void setSkipFailedInvocationCounts(Boolean skip) {1351 m_skipFailedInvocationCounts = skip;1352 }1353 private void addReporter(ReporterConfig reporterConfig) {1354 IReporter instance = reporterConfig.newReporterInstance();1355 if (instance != null) {1356 addListener(instance);1357 } else {1358 LOGGER.warn("Could not find reporter class : " + reporterConfig.getClassName());1359 }1360 }1361 /**1362 * Specify if this run should be made in JUnit mode1363 *1364 * @param isJUnit1365 */1366 public void setJUnit(Boolean isJUnit) {1367 m_isJUnit = isJUnit;1368 }1369 /** Specify if this run should be made in mixed mode */1370 public void setMixed(Boolean isMixed) {1371 if (isMixed == null) {1372 return;1373 }1374 m_isMixed = isMixed;1375 }1376 /** Double check that the command line parameters are valid. */1377 protected static void validateCommandLineParameters(CommandLineArgs args) {1378 String testClasses = args.testClass;1379 List<String> testNgXml = args.suiteFiles;1380 String testJar = args.testJar;1381 List<String> methods = args.commandLineMethods;1382 if (testClasses == null1383 && testJar == null1384 && (testNgXml == null || testNgXml.isEmpty())1385 && (methods == null || methods.isEmpty())) {1386 throw new ParameterException(1387 "You need to specify at least one testng.xml, one class" + " or one method");1388 }1389 String groups = args.groups;1390 String excludedGroups = args.excludedGroups;1391 if (testJar == null1392 && (null != groups || null != excludedGroups)1393 && testClasses == null1394 && (testNgXml == null || testNgXml.isEmpty())) {1395 throw new ParameterException("Groups option should be used with testclass option");1396 }1397 Boolean junit = args.junit;1398 Boolean mixed = args.mixed;1399 if (junit && mixed) {1400 throw new ParameterException(1401 CommandLineArgs.MIXED + " can't be combined with " + CommandLineArgs.JUNIT);1402 }1403 }1404 /** @return true if at least one test failed. */1405 public boolean hasFailure() {1406 return this.exitCode.hasFailure();1407 }1408 /** @return true if at least one test failed within success percentage. */1409 public boolean hasFailureWithinSuccessPercentage() {1410 return this.exitCode.hasFailureWithinSuccessPercentage();1411 }1412 /** @return true if at least one test was skipped. */1413 public boolean hasSkip() {1414 return this.exitCode.hasSkip();1415 }...

Full Screen

Full Screen

Source:RemoteTestNG.java Github

copy

Full Screen

...45import com.beust.jcommander.JCommander;6import com.beust.jcommander.ParameterException;78import org.testng.CommandLineArgs;9import org.testng.IClassListener;10import org.testng.IInvokedMethodListener;11import org.testng.ISuite;12import org.testng.ISuiteListener;13import org.testng.ITestRunnerFactory;14import org.testng.TestNG;15import org.testng.TestNGException;16import org.testng.TestRunner;17import org.testng.collections.Lists;18import org.testng.remote.strprotocol.GenericMessage;19import org.testng.remote.strprotocol.IMessageSender;20import org.testng.remote.strprotocol.MessageHelper;21import org.testng.remote.strprotocol.MessageHub;22import org.testng.remote.strprotocol.RemoteTestListener;23import org.testng.remote.strprotocol.SerializedMessageSender;24import org.testng.remote.strprotocol.StringMessageSender;25import org.testng.remote.strprotocol.SuiteMessage;26import org.testng.reporters.JUnitXMLReporter;27import org.testng.reporters.TestHTMLReporter;28import org.testng.xml.XmlSuite;29import org.testng.xml.XmlTest;3031import java.util.Arrays;32import java.util.Collection;33import java.util.List;3435/**36 * Extension of TestNG registering a remote TestListener.37 *38 * @author Cedric Beust <cedric@beust.com>39 */40public class RemoteTestNG extends TestNG {41 private static final String LOCALHOST = "localhost";4243 // The following constants are referenced by the Eclipse plug-in, make sure you44 // modify the plug-in as well if you change any of them.45 public static final String DEBUG_PORT = "12345";46 public static final String DEBUG_SUITE_FILE = "testng-customsuite.xml";47 public static final String DEBUG_SUITE_DIRECTORY = System.getProperty("java.io.tmpdir");48 public static final String PROPERTY_DEBUG = "testng.eclipse.debug";49 public static final String PROPERTY_VERBOSE = "testng.eclipse.verbose";50 // End of Eclipse constants.5152 private ITestRunnerFactory m_customTestRunnerFactory;53 private String m_host;5455 /** Port used for the string protocol */56 private Integer m_port = null;5758 /** Port used for the serialized protocol */59 private static Integer m_serPort = null;6061 private static boolean m_debug;6263 private static boolean m_dontExit;6465 private static boolean m_ack;6667 public void setHost(String host) {68 m_host = defaultIfStringEmpty(host, LOCALHOST);69 }7071 private void calculateAllSuites(List<XmlSuite> suites, List<XmlSuite> outSuites) {72 for (XmlSuite s : suites) {73 outSuites.add(s);74// calculateAllSuites(s.getChildSuites(), outSuites);75 }76 }7778 @Override79 public void run() {80 IMessageSender sender = m_serPort != null81 ? new SerializedMessageSender(m_host, m_serPort, m_ack)82 : new StringMessageSender(m_host, m_port);83 final MessageHub msh = new MessageHub(sender);84 msh.setDebug(isDebug());85 try {86 msh.connect();87 // We couldn't do this until now in debug mode since the .xml file didn't exist yet.88 // Now that we have connected with the Eclipse client, we know that it created the .xml89 // file so we can proceed with the initialization90 initializeSuitesAndJarFile();9192 List<XmlSuite> suites = Lists.newArrayList();93 calculateAllSuites(m_suites, suites);94// System.out.println("Suites: " + m_suites.get(0).getChildSuites().size()95// + " and:" + suites.get(0).getChildSuites().size());96 if(suites.size() > 0) {9798 int testCount= 0;99100 for (XmlSuite suite : suites) {101 testCount += suite.getTests().size();102 }103104 GenericMessage gm= new GenericMessage(MessageHelper.GENERIC_SUITE_COUNT);105 gm.setSuiteCount(suites.size());106 gm.setTestCount(testCount);107 msh.sendMessage(gm);108109 addListener(new RemoteSuiteListener(msh));110 setTestRunnerFactory(new DelegatingTestRunnerFactory(buildTestRunnerFactory(), msh));111112// System.out.println("RemoteTestNG starting");113 super.run();114 }115 else {116 System.err.println("No test suite found. Nothing to run");117 }118 }119 catch(Throwable cause) {120 cause.printStackTrace(System.err);121 }122 finally {123// System.out.println("RemoteTestNG finishing: " + (getEnd() - getStart()) + " ms");124 msh.shutDown();125 if (! m_debug && ! m_dontExit) {126 System.exit(0);127 }128 }129 }130131 /**132 * Override by the plugin if you need to configure differently the <code>TestRunner</code>133 * (usually this is needed if different listeners/reporters are needed).134 * <b>Note</b>: you don't need to worry about the wiring listener, because it is added135 * automatically.136 */137 protected ITestRunnerFactory buildTestRunnerFactory() {138 if(null == m_customTestRunnerFactory) {139 m_customTestRunnerFactory= new ITestRunnerFactory() {140 @Override141 public TestRunner newTestRunner(ISuite suite, XmlTest xmlTest,142 Collection<IInvokedMethodListener> listeners, List<IClassListener> classListeners) {143 TestRunner runner =144 new TestRunner(getConfiguration(), suite, xmlTest,145 false /*skipFailedInvocationCounts */,146 listeners, classListeners);147 if (m_useDefaultListeners) {148 runner.addListener(new TestHTMLReporter());149 runner.addListener(new JUnitXMLReporter());150 }151152 return runner;153 }154 };155 }156157 return m_customTestRunnerFactory;158 }159160 public static void main(String[] args) throws ParameterException {161 CommandLineArgs cla = new CommandLineArgs();162 RemoteArgs ra = new RemoteArgs();163 new JCommander(Arrays.asList(cla, ra), args);164 m_dontExit = ra.dontExit;165 if (cla.port != null && ra.serPort != null) {166 throw new TestNGException("Can only specify one of " + CommandLineArgs.PORT167 + " and " + RemoteArgs.PORT);168 }169 m_debug = cla.debug;170 m_ack = ra.ack;171 if (m_debug) {172// while (true) {173 initAndRun(args, cla, ra);174// }175 }176 else {177 initAndRun(args, cla, ra);178 }179 }180181 private static void initAndRun(String[] args, CommandLineArgs cla, RemoteArgs ra) {182 RemoteTestNG remoteTestNg = new RemoteTestNG();183 if (m_debug) {184 // In debug mode, override the port and the XML file to a fixed location185 cla.port = Integer.parseInt(DEBUG_PORT);186 ra.serPort = cla.port;187 cla.suiteFiles = Arrays.asList(new String[] {188 DEBUG_SUITE_DIRECTORY + DEBUG_SUITE_FILE189 });190 }191 remoteTestNg.configure(cla);192 remoteTestNg.setHost(cla.host);193 m_serPort = ra.serPort;194 remoteTestNg.m_port = cla.port;195 if (isVerbose()) { ...

Full Screen

Full Screen

Source:TTestNGProxy.java Github

copy

Full Screen

1package com.time.ttest.proxy;2import com.beust.jcommander.JCommander;3import com.time.ttest.TTestApplication;4import lombok.extern.slf4j.Slf4j;5import org.testng.CommandLineArgs;6import org.testng.TestNG;7import org.testng.TestNGException;8import org.testng.TestRunner;9import org.testng.collections.Lists;10import org.testng.internal.ExitCode;11import java.util.Arrays;12import java.util.List;13import java.util.Properties;14import java.util.stream.Collectors;15/**16 * testng代理类17 * @Auther guoweijie18 * @Date 2020-03-22 23:0719 */20@Slf4j21public class TTestNGProxy<T> extends Proxy<T>{22 /**23 * 配置文件24 */25 private Properties properties;26 /**27 * 命令行参数28 */29 private String[] args;30 public TTestNGProxy(TestNG testNG) {31 setBeProxy((T) testNG);32 }33 public TTestNGProxy(TestNG testNG,Properties properties) {34 setBeProxy((T) testNG);35 this.properties = properties;36 }37 public TTestNGProxy(TestNG testNG,Properties properties,String[] args) {38 setBeProxy((T) testNG);39 this.properties = properties;40 this.args = args;41 }42 public TestNG getTestNG(){43 return (TestNG) getBeProxy();44 }45 public void run(){46 CommandLineArgs cla = commandLineArgs(properties,args);47 getTestNG().setTestSuites(cla.suiteFiles);48 //调用testng的方法,校验参数49 invokeMethod("validateCommandLineParameters",new Class[]{CommandLineArgs.class},cla);50 //调用testng的方法,配置参数51 invokeMethod("configure",new Class[]{CommandLineArgs.class},cla);52 try {53 getTestNG().run();54 }catch (TestNGException ex) {55 if (TestRunner.getVerbose() > 1) {56 ex.printStackTrace(System.out);57 } else {58 log.error(ex.getMessage());59 }60 modifyPrivateParam("exitCode", ExitCode.newExitCodeRepresentingFailure());61 }62 }63 private CommandLineArgs commandLineArgs(Properties properties,String... args){64 CommandLineArgs cla = new CommandLineArgs();65 JCommander jCommander = new JCommander(cla);66 jCommander.parse(args);67 //如果启动参数没有指定xml,则使用配置文件中的68 cla.suiteFiles = cla.suiteFiles.size()>0?cla.suiteFiles:getTestNgSuitFiles(properties);69 //添加 guice InjectorFactory70 cla.dependencyInjectoryFactoryClass = TTestApplication.class.getName();71 return cla;72 }73 /**74 * 获取配置文件的 suitefile75 * @param properties 配置文件参数76 * @return77 */78 private List<String> getTestNgSuitFiles(Properties properties){...

Full Screen

Full Screen

Source:CliTest.java Github

copy

Full Screen

2import static org.assertj.core.api.Assertions.assertThat;3import java.util.Collections;4import java.util.function.Supplier;5import org.testng.Assert;6import org.testng.CommandLineArgs;7import org.testng.TestNG;8import org.testng.annotations.BeforeMethod;9import org.testng.annotations.DataProvider;10import org.testng.annotations.Test;11import test.SimpleBaseTest;12import test.cli.github1517.TestClassWithConfigFailureSample;13import test.cli.github1517.TestClassWithConfigSkipAndFailureSample;14import test.cli.github1517.TestClassWithConfigSkipSample;15import test.cli.github2693.TestClassSample;16public class CliTest extends SimpleBaseTest {17 @BeforeMethod18 public void clearThreadsInformation() {19 TestClassSample.threads.clear();20 }21 @Test(dataProvider = "getScenarios", description = "GITHUB-2693")22 public void ensureDataProviderCountCanBeOverridden(String scenario, CommandLineArgs args) {23 CustomTestNG tng = new CustomTestNG();24 tng.configure(args);25 tng.run();26 assertThat(TestClassSample.threads).withFailMessage(scenario).hasSize(2);27 }28 @DataProvider(name = "getScenarios")29 public Object[][] getScenarios() {30 Supplier<CommandLineArgs> supplier =31 () -> {32 CommandLineArgs cli = new CommandLineArgs();33 cli.dataProviderThreadCount = 2;34 return cli;35 };36 CommandLineArgs cliSuites = supplier.get();37 cliSuites.suiteFiles = Collections.singletonList("src/test/resources/xml/issue2693.xml");38 String className = TestClassSample.class.getName();39 CommandLineArgs cliClasses = supplier.get();40 cliClasses.testClass = className;41 CommandLineArgs cliMethods = supplier.get();42 cliMethods.commandLineMethods = Collections.singletonList(className + ".test");43 return new Object[][] {44 new Object[] {"CLI With Suites", cliSuites},45 new Object[] {"CLI With Classes", cliClasses},46 new Object[] {"CLI With Methods", cliMethods}47 };48 }49 @Test(dataProvider = "getData", description = "GITHUB-1517")50 public void testExitCodeListenerBehavior(Class<?> clazz, int expectedStatus) {51 TestNG testNG = create(clazz);52 testNG.run();53 Assert.assertEquals(testNG.getStatus(), expectedStatus);54 }55 @DataProvider56 public Object[][] getData() {57 return new Object[][] {58 {TestClassWithConfigFailureSample.class, 3},59 {TestClassWithConfigSkipSample.class, 2},60 {TestClassWithConfigSkipAndFailureSample.class, 3}61 };62 }63 public static class CustomTestNG extends TestNG {64 @Override65 public void configure(CommandLineArgs cla) {66 super.configure(cla);67 }68 }69}...

Full Screen

Full Screen

CommandLineArgs

Using AI Code Generation

copy

Full Screen

1import org.testng.CommandLineArgs;2import org.testng.TestNG;3import org.testng.TestNGCommandLineArgs;4import org.testng.TestNGCommandLineArgs;5public class TestNGCommandLineArgsExample {6 public static void main(String[] args) {7 TestNGCommandLineArgs commandLineArgs = new TestNGCommandLineArgs();8 CommandLineArgs commandLineArgs2 = new CommandLineArgs();9 TestNG testNG = new TestNG();10 testNG.run();11 commandLineArgs.run();12 commandLineArgs2.run();13 }14}

Full Screen

Full Screen

CommandLineArgs

Using AI Code Generation

copy

Full Screen

1import org.testng.CommandLineArgs;2import org.testng.annotations.Test;3public class TestCommandLineArgs {4public void testCommandLineArgs() {5String[] args = CommandLineArgs.getArgs();6for (String arg : args) {7System.out.println(arg);8}9}10}11java -cp .;testng-7.4.0.jar; TestCommandLineArgs -testng -arg1 -arg2 -arg3 -arg4

Full Screen

Full Screen

CommandLineArgs

Using AI Code Generation

copy

Full Screen

1import org.testng.internal.CommandLineArgs;2public class CommandLineArgsExample {3 public static void main(String[] args) {4 CommandLineArgs cla = new CommandLineArgs(args);5 System.out.println("args: " + cla.getArgs());6 System.out.println("groups: " + cla.getGroups());7 System.out.println("junit: " + cla.isJUnit());8 System.out.println("parallel: " + cla.getParallelMode());9 System.out.println("threadCount: " + cla.getThreadCount());10 System.out.println("testNames: " + cla.getTestNames());11 System.out.println("verbose: " + cla.getVerbose());12 }13}

Full Screen

Full Screen

CommandLineArgs

Using AI Code Generation

copy

Full Screen

1import org.testng.internal.CommandLineArgs;2import org.testng.internal.CommandLineArgs.ParsedArgs;3import org.testng.internal.CommandLineArgs.ParsedArgs.TestNGArgs;4import org.testng.internal.CommandLineArgs.ParsedArgs.TestNGArgs.TestNGArg;5import java.util.List;6public class CommandLineArgsTest {7 public static void main(String[] args) {8 CommandLineArgs commandLineArgs = new CommandLineArgs();9 ParsedArgs parsedArgs = commandLineArgs.parse(args);10 TestNGArgs testNGArgs = parsedArgs.getTestNGArgs();11 System.out.println("TestNGArgs: " + testNGArgs);12 if (testNGArgs != null) {13 System.out.println("TestNGArgs.isHelp(): " + testNGArgs.isHelp());14 System.out.println("TestNGArgs.isVersion(): " + testNGArgs.isVersion());15 System.out.println("TestNGArgs.isListGroups(): " + testNGArgs.isListGroups());16 System.out.println("TestNGArgs.isListSuites(): " + testNGArgs.isListSuites());17 System.out.println("TestNGArgs.isListTests(): " + testNGArgs.isListTests());18 System.out.println("TestNGArgs.isListClasses(): " + testNGArgs.isListClasses());19 System.out.println("TestNGArgs.isListMethods(): " + testNGArgs.isListMethods());20 System.out.println("TestNGArgs.isListPackages(): " + testNGArgs.isListPackages());21 System.out.println("TestNGArgs.isListXml(): " + testNGArgs.isListXml());

Full Screen

Full Screen
copy
1jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));'2
Full Screen
copy
1<target name="build-java" depends="prepare-build">2 <echo message="Compiling java files"/>3 <javac ....4 target="1.5"...5 </javac>6</target>7
Full Screen

TestNG tutorial

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.

Chapters

  1. JUnit 5 vs. TestNG: Compare and explore the core differences between JUnit 5 and TestNG from the Selenium WebDriver viewpoint.
  2. Installing TestNG in Eclipse: Start installing the TestNG Plugin and learn how to set up TestNG in Eclipse to begin constructing a framework for your test project.
  3. Create TestNG Project in Eclipse: Get started with creating a TestNG project and write your first TestNG test script.
  4. Automation using TestNG: Dive into how to install TestNG in this Selenium TestNG tutorial, the fundamentals of developing an automation script for Selenium automation testing.
  5. Parallel Test Execution in TestNG: Here are some essential elements of parallel testing with TestNG in this Selenium TestNG tutorial.
  6. Creating TestNG XML File: Here is a step-by-step tutorial on creating a TestNG XML file to learn why and how it is created and discover how to run the TestNG XML file being executed in parallel.
  7. Automation with Selenium, Cucumber & TestNG: Explore for an in-depth tutorial on automation using Selenium, Cucumber, and TestNG, as TestNG offers simpler settings and more features.
  8. JUnit Selenium Tests using TestNG: Start running your regular and parallel tests by looking at how to run test cases in Selenium using JUnit and TestNG without having to rewrite the tests.
  9. Group Test Cases in TestNG: Along with the explanation and demonstration using relevant TestNG group examples, learn how to group test cases in TestNG.
  10. Prioritizing Tests in TestNG: Get started with how to prioritize test cases in TestNG for Selenium automation testing.
  11. Assertions in TestNG: Examine what TestNG assertions are, the various types of TestNG assertions, and situations that relate to Selenium automated testing.
  12. DataProviders in TestNG: Deep dive into learning more about TestNG's DataProvider and how to effectively use it in our test scripts for Selenium test automation.
  13. Parameterization in TestNG: Here are the several parameterization strategies used in TestNG tests and how to apply them in Selenium automation scripts.
  14. TestNG Listeners in Selenium WebDriver: Understand the various TestNG listeners to utilize them effectively for your next plan when working with TestNG and Selenium automation.
  15. TestNG Annotations: Learn more about the execution order and annotation attributes, and refer to the prerequisites required to set up TestNG.
  16. TestNG Reporter Log in Selenium: Find out how to use the TestNG Reporter Log and learn how to eliminate the need for external software with TestNG Reporter Class to boost productivity.
  17. TestNG Reports in Jenkins: Discover how to generate TestNG reports in Jenkins if you want to know how to create, install, and share TestNG reports in Jenkins.

Certification

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.

YouTube

Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful