How to use getIncludedGroups method of org.testng.TestRunner class

Best Testng code snippet using org.testng.TestRunner.getIncludedGroups

Source:TestRunner.java Github

copy

Full Screen

...325 }326 }327 private void initRunInfo(final XmlTest xmlTest) {328 // Groups329 m_xmlMethodSelector.setIncludedGroups(createGroups(m_xmlTest.getIncludedGroups()));330 m_xmlMethodSelector.setExcludedGroups(createGroups(m_xmlTest.getExcludedGroups()));331 m_xmlMethodSelector.setScript(m_xmlTest.getScript());332 // Methods333 m_xmlMethodSelector.setXmlClasses(m_xmlTest.getXmlClasses());334 m_runInfo.addMethodSelector(m_xmlMethodSelector, 10);335 // Add user-specified method selectors (only class selectors, we can ignore336 // script selectors here)337 if (null != xmlTest.getMethodSelectors()) {338 for (org.testng.xml.XmlMethodSelector selector : xmlTest.getMethodSelectors()) {339 if (selector.getClassName() != null) {340 IMethodSelector s = ClassHelper.createSelector(selector);341 m_runInfo.addMethodSelector(s, selector.getPriority());342 }343 }344 }345 }346 private void initMethods() {347 //348 // Calculate all the methods we need to invoke349 //350 List<ITestNGMethod> beforeClassMethods = Lists.newArrayList();351 List<ITestNGMethod> testMethods = Lists.newArrayList();352 List<ITestNGMethod> afterClassMethods = Lists.newArrayList();353 List<ITestNGMethod> beforeSuiteMethods = Lists.newArrayList();354 List<ITestNGMethod> afterSuiteMethods = Lists.newArrayList();355 List<ITestNGMethod> beforeXmlTestMethods = Lists.newArrayList();356 List<ITestNGMethod> afterXmlTestMethods = Lists.newArrayList();357 ClassInfoMap classMap = new ClassInfoMap(m_testClassesFromXml);358 m_testClassFinder =359 new TestNGClassFinder(360 classMap, Maps.newHashMap(), m_configuration, this, m_dataProviderListeners);361 ITestMethodFinder testMethodFinder =362 new TestNGMethodFinder(m_runInfo, m_annotationFinder, comparator);363 m_runInfo.setTestMethods(testMethods);364 //365 // Initialize TestClasses366 //367 IClass[] classes = m_testClassFinder.findTestClasses();368 for (IClass ic : classes) {369 // Create TestClass370 ITestClass tc =371 new TestClass(372 ic,373 testMethodFinder,374 m_annotationFinder,375 m_xmlTest,376 classMap.getXmlClass(ic.getRealClass()));377 m_classMap.put(ic.getRealClass(), tc);378 }379 //380 // Calculate groups methods381 //382 Map<String, List<ITestNGMethod>> beforeGroupMethods =383 MethodGroupsHelper.findGroupsMethods(m_classMap.values(), true);384 Map<String, List<ITestNGMethod>> afterGroupMethods =385 MethodGroupsHelper.findGroupsMethods(m_classMap.values(), false);386 //387 // Walk through all the TestClasses, store their method388 // and initialize them with the correct ITestClass389 //390 for (ITestClass tc : m_classMap.values()) {391 fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);392 fixMethodsWithClass(tc.getBeforeClassMethods(), tc, beforeClassMethods);393 fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);394 fixMethodsWithClass(tc.getAfterTestMethods(), tc, null);395 fixMethodsWithClass(tc.getAfterClassMethods(), tc, afterClassMethods);396 fixMethodsWithClass(tc.getBeforeSuiteMethods(), tc, beforeSuiteMethods);397 fixMethodsWithClass(tc.getAfterSuiteMethods(), tc, afterSuiteMethods);398 fixMethodsWithClass(tc.getBeforeTestConfigurationMethods(), tc, beforeXmlTestMethods);399 fixMethodsWithClass(tc.getAfterTestConfigurationMethods(), tc, afterXmlTestMethods);400 fixMethodsWithClass(401 tc.getBeforeGroupsMethods(),402 tc,403 MethodHelper.uniqueMethodList(beforeGroupMethods.values()));404 fixMethodsWithClass(405 tc.getAfterGroupsMethods(),406 tc,407 MethodHelper.uniqueMethodList(afterGroupMethods.values()));408 }409 //410 // Sort the methods411 //412 m_beforeSuiteMethods =413 MethodHelper.collectAndOrderMethods(414 beforeSuiteMethods,415 false /* forTests */,416 m_runInfo,417 m_annotationFinder,418 true /* unique */,419 m_excludedMethods,420 comparator);421 m_beforeXmlTestMethods =422 MethodHelper.collectAndOrderMethods(423 beforeXmlTestMethods,424 false /* forTests */,425 m_runInfo,426 m_annotationFinder,427 true /* unique (CQ added by me)*/,428 m_excludedMethods,429 comparator);430 m_allTestMethods =431 MethodHelper.collectAndOrderMethods(432 testMethods,433 true /* forTest? */,434 m_runInfo,435 m_annotationFinder,436 false /* unique */,437 m_excludedMethods,438 comparator);439 m_classMethodMap = new ClassMethodMap(testMethods, m_xmlMethodSelector);440 m_afterXmlTestMethods =441 MethodHelper.collectAndOrderMethods(442 afterXmlTestMethods,443 false /* forTests */,444 m_runInfo,445 m_annotationFinder,446 true /* unique (CQ added by me)*/,447 m_excludedMethods,448 comparator);449 m_afterSuiteMethods =450 MethodHelper.collectAndOrderMethods(451 afterSuiteMethods,452 false /* forTests */,453 m_runInfo,454 m_annotationFinder,455 true /* unique */,456 m_excludedMethods,457 comparator);458 // shared group methods459 m_groupMethods =460 new ConfigurationGroupMethods(m_allTestMethods, beforeGroupMethods, afterGroupMethods);461 }462 public Collection<ITestClass> getTestClasses() {463 return m_classMap.values();464 }465 public void setTestName(String name) {466 m_testName = name;467 }468 public void setOutputDirectory(String od) {469 m_outputDirectory = od;470 }471 private void addMetaGroup(String name, List<String> groupNames) {472 m_metaGroups.put(name, groupNames);473 }474 private Map<String, String> createGroups(List<String> groups) {475 return GroupsHelper.createGroups(m_metaGroups, groups);476 }477 /**478 * The main entry method for TestRunner.479 *480 * <p>This is where all the hard work is done: - Invoke configuration methods - Invoke test481 * methods - Catch exceptions - Collect results - Invoke listeners - etc...482 */483 public void run() {484 beforeRun();485 try {486 XmlTest test = getTest();487 if (test.isJUnit()) {488 privateRunJUnit();489 } else {490 privateRun(test);491 }492 } finally {493 afterRun();494 }495 }496 /** Before run preparements. */497 private void beforeRun() {498 //499 // Log the start date500 //501 m_startDate = new Date(System.currentTimeMillis());502 // Log start503 logStart();504 // Invoke listeners505 fireEvent(true /*start*/);506 // invoke @BeforeTest507 ITestNGMethod[] testConfigurationMethods = getBeforeTestConfigurationMethods();508 if (null != testConfigurationMethods && testConfigurationMethods.length > 0) {509 m_invoker.invokeConfigurations(510 null,511 testConfigurationMethods,512 m_xmlTest.getSuite(),513 m_xmlTest.getAllParameters(),514 null, /* no parameter values */515 null /* instance */);516 }517 }518 private void privateRunJUnit() {519 final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false);520 final Set<Class<?>> classes = cim.getClasses();521 final List<ITestNGMethod> runMethods = Lists.newArrayList();522 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();523 // FIXME: directly referencing JUnitTestRunner which uses JUnit classes524 // may result in an class resolution exception under different JVMs525 // The resolution process is not specified in the JVM spec with a specific implementation,526 // so it can be eager => failure527 workers.add(528 new IWorker<ITestNGMethod>() {529 /** @see TestMethodWorker#getTimeOut() */530 @Override531 public long getTimeOut() {532 return 0;533 }534 /** @see java.lang.Runnable#run() */535 @Override536 public void run() {537 for (Class<?> tc : classes) {538 List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods();539 List<String> methods = Lists.newArrayList();540 for (XmlInclude inc : includedMethods) {541 methods.add(inc.getName());542 }543 IJUnitTestRunner tr = ClassHelper.createTestRunner(TestRunner.this);544 tr.setInvokedMethodListeners(m_invokedMethodListeners);545 try {546 tr.run(tc, methods.toArray(new String[0]));547 } catch (Exception ex) {548 LOGGER.error(ex.getMessage(), ex);549 } finally {550 runMethods.addAll(tr.getTestMethods());551 }552 }553 }554 @Override555 public List<ITestNGMethod> getTasks() {556 throw new TestNGException("JUnit not supported");557 }558 @Override559 public int getPriority() {560 if (m_allTestMethods.length == 1) {561 return m_allTestMethods[0].getPriority();562 } else {563 return 0;564 }565 }566 @Override567 public int compareTo(@Nonnull IWorker<ITestNGMethod> other) {568 return getPriority() - other.getPriority();569 }570 });571 runJUnitWorkers(workers);572 m_allTestMethods = runMethods.toArray(new ITestNGMethod[0]);573 }574 /**575 * Main method that create a graph of methods and then pass it to the graph executor to run them.576 */577 private void privateRun(XmlTest xmlTest) {578 boolean parallel = xmlTest.getParallel().isParallel();579 {580 // parallel581 int threadCount = parallel ? xmlTest.getThreadCount() : 1;582 // Make sure we create a graph based on the intercepted methods, otherwise an interceptor583 // removing methods would cause the graph never to terminate (because it would expect584 // termination from methods that never get invoked).585 DynamicGraph<ITestNGMethod> graph =586 DynamicGraphHelper.createDynamicGraph(intercept(m_allTestMethods), getCurrentXmlTest());587 graph.setVisualisers(this.visualisers);588 if (parallel) {589 if (graph.getNodeCount() > 0) {590 GraphThreadPoolExecutor<ITestNGMethod> executor =591 new GraphThreadPoolExecutor<>(592 "test=" + xmlTest.getName(),593 graph,594 this,595 threadCount,596 threadCount,597 0,598 TimeUnit.MILLISECONDS,599 new LinkedBlockingQueue<>());600 executor.run();601 try {602 long timeOut = m_xmlTest.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS);603 Utils.log(604 "TestRunner",605 2,606 "Starting executor for test "607 + m_xmlTest.getName()608 + " with time out:"609 + timeOut610 + " milliseconds.");611 executor.awaitTermination(timeOut, TimeUnit.MILLISECONDS);612 executor.shutdownNow();613 } catch (InterruptedException handled) {614 LOGGER.error(handled.getMessage(), handled);615 Thread.currentThread().interrupt();616 }617 }618 } else {619 List<ITestNGMethod> freeNodes = graph.getFreeNodes();620 if (graph.getNodeCount() > 0 && freeNodes.isEmpty()) {621 throw new TestNGException("No free nodes found in:" + graph);622 }623 while (!freeNodes.isEmpty()) {624 List<IWorker<ITestNGMethod>> runnables = createWorkers(freeNodes);625 for (IWorker<ITestNGMethod> r : runnables) {626 r.run();627 }628 graph.setStatus(freeNodes, Status.FINISHED);629 freeNodes = graph.getFreeNodes();630 }631 }632 }633 }634 /** Apply the method interceptor (if applicable) to the list of methods. */635 private ITestNGMethod[] intercept(ITestNGMethod[] methods) {636 List<IMethodInstance> methodInstances =637 MethodHelper.methodsToMethodInstances(Arrays.asList(methods));638 for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {639 methodInstances = m_methodInterceptor.intercept(methodInstances, this);640 }641 List<ITestNGMethod> result = MethodHelper.methodInstancesToMethods(methodInstances);642 // Since an interceptor is involved, we would need to ensure that the ClassMethodMap object is643 // in sync with the644 // output of the interceptor, else @AfterClass doesn't get executed at all when interceptors are645 // involved.646 // so let's update the current classMethodMap object with the list of methods obtained from the647 // interceptor.648 this.m_classMethodMap = new ClassMethodMap(result, null);649 ITestNGMethod[] resultArray = result.toArray(new ITestNGMethod[0]);650 // Check if an interceptor had altered the effective test method count. If yes, then we need to651 // update our configurationGroupMethod object with that information.652 if (resultArray.length != m_groupMethods.getAllTestMethods().length) {653 m_groupMethods =654 new ConfigurationGroupMethods(655 resultArray,656 m_groupMethods.getBeforeGroupsMethods(),657 m_groupMethods.getAfterGroupsMethods());658 }659 return resultArray;660 }661 /**662 * Create a list of workers to run the methods passed in parameter. Each test method is run in its663 * own worker except in the following cases: - The method belongs to a class that664 * has @Test(sequential=true) - The parallel attribute is set to "classes" In both these cases,665 * all the methods belonging to that class will then be put in the same worker in order to run in666 * the same thread.667 */668 @Override669 public List<IWorker<ITestNGMethod>> createWorkers(List<ITestNGMethod> methods) {670 AbstractParallelWorker.Arguments args =671 new AbstractParallelWorker.Arguments.Builder()672 .classMethodMap(this.m_classMethodMap)673 .configMethods(this.m_groupMethods)674 .finder(this.m_annotationFinder)675 .invoker(this.m_invoker)676 .methods(methods)677 .testContext(this)678 .listeners(this.m_classListeners.values())679 .build();680 return AbstractParallelWorker.newWorker(m_xmlTest.getParallel()).createWorkers(args);681 }682 //683 // Invoke the workers684 //685 private void runJUnitWorkers(List<? extends IWorker<ITestNGMethod>> workers) {686 //687 // Sequential run688 //689 for (IWorker<ITestNGMethod> tmw : workers) {690 tmw.run();691 }692 }693 private void afterRun() {694 // invoke @AfterTest695 ITestNGMethod[] testConfigurationMethods = getAfterTestConfigurationMethods();696 if (null != testConfigurationMethods && testConfigurationMethods.length > 0) {697 m_invoker.invokeConfigurations(698 null,699 testConfigurationMethods,700 m_xmlTest.getSuite(),701 m_xmlTest.getAllParameters(),702 null, /* no parameter values */703 null /* instance */);704 }705 //706 // Log the end date707 //708 m_endDate = new Date(System.currentTimeMillis());709 dumpInvokedMethods();710 // Invoke listeners711 fireEvent(false /*stop*/);712 }713 /** Logs the beginning of the {@link #beforeRun()} . */714 private void logStart() {715 log(716 3,717 "Running test "718 + m_testName719 + " on "720 + m_classMap.size()721 + " "722 + " classes, "723 + " included groups:["724 + Strings.valueOf(m_xmlMethodSelector.getIncludedGroups())725 + "] excluded groups:["726 + Strings.valueOf(m_xmlMethodSelector.getExcludedGroups())727 + "]");728 if (getVerbose() >= 3) {729 for (ITestClass tc : m_classMap.values()) {730 ((TestClass) tc).dump();731 }732 }733 }734 /**735 * Trigger the start/finish event.736 *737 * @param isStart <tt>true</tt> if the event is for start, <tt>false</tt> if the event is for738 * finish739 */740 private void fireEvent(boolean isStart) {741 for (ITestListener itl : m_testListeners) {742 if (isStart) {743 itl.onStart(this);744 } else {745 itl.onFinish(this);746 }747 }748 }749 /////750 // ITestContext751 //752 @Override753 public String getName() {754 return m_testName;755 }756 /** @return Returns the startDate. */757 @Override758 public Date getStartDate() {759 return m_startDate;760 }761 /** @return Returns the endDate. */762 @Override763 public Date getEndDate() {764 return m_endDate;765 }766 @Override767 public IResultMap getPassedTests() {768 return m_passedTests;769 }770 @Override771 public IResultMap getSkippedTests() {772 return m_skippedTests;773 }774 @Override775 public IResultMap getFailedTests() {776 return m_failedTests;777 }778 @Override779 public IResultMap getFailedButWithinSuccessPercentageTests() {780 return m_failedButWithinSuccessPercentageTests;781 }782 @Override783 public String[] getIncludedGroups() {784 Map<String, String> ig = m_xmlMethodSelector.getIncludedGroups();785 return ig.values().toArray(new String[0]);786 }787 @Override788 public String[] getExcludedGroups() {789 Map<String, String> eg = m_xmlMethodSelector.getExcludedGroups();790 return eg.values().toArray(new String[0]);791 }792 @Override793 public String getOutputDirectory() {794 return m_outputDirectory;795 }796 /** @return Returns the suite. */797 @Override798 public ISuite getSuite() {...

Full Screen

Full Screen

Source:BaseTest.java Github

copy

Full Screen

...162 xmlClass.getExcludedMethods().add(m);163 getTest().getXmlClasses().add(xmlClass);164 }165 public void addIncludedGroup(String g) {166 getTest().getIncludedGroups().add(g);167 }168 public void addExcludedGroup(String g) {169 getTest().getExcludedGroups().add(g);170 }171 public void addMetaGroup(String mg, List<String> l) {172 getTest().getMetaGroups().put(mg, l);173 }174 public void addMetaGroup(String mg, String n) {175 List<String> l = new ArrayList<String>();176 l.add(n);177 addMetaGroup(mg, l);178 }179 public void setParameter(String key, String value) {180 getTest().addParameter(key, value);...

Full Screen

Full Screen

Source:MethodGroupMappingHolder.java Github

copy

Full Screen

...44 Map<String, Set<String>> testnameGroupMapping = new HashMap<>();45 List<XmlTest> xmlTests = xmlSuite.getTests();46 for (XmlTest xmlTest : xmlTests) {47 String testname = xmlTest.getName();48 List<String> includeGroups = xmlTest.getIncludedGroups();49 if (CollectionUtils.isNotEmpty(includeGroups)) {50 testnameGroupMapping.put(testname, new HashSet<String>(includeGroups));51 }52 }53 methodGroupsMapping.clear();54 for (ITestNGMethod tMethod : allTestngMethods) {55 Method method = tMethod.getConstructorOrMethod().getMethod();56 String testname = tMethod.getXmlTest().getName();57 Set<String> groups = testnameGroupMapping.get(testname);58 if (CollectionUtils.isEmpty(groups)) {59 continue;60 }61 if (methodGroupsMapping.containsKey(method)) {62 methodGroupsMapping.get(method).addAll(groups);...

Full Screen

Full Screen

getIncludedGroups

Using AI Code Generation

copy

Full Screen

1import org.testng.TestRunner;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import org.testng.xml.XmlTest;5public class TestRunnerTest {6 public static void main(String[] args) {7 TestNG testng = new TestNG();8 XmlSuite suite = new XmlSuite();9 suite.setName("Suite");10 XmlTest test = new XmlTest(suite);11 test.setName("Test");12 test.setXmlClasses(new ArrayList<XmlClass>() {{13 add(new XmlClass("com.test.TestClass"));14 }});15 testng.setXmlSuites(new ArrayList<XmlSuite>() {{16 add(suite);17 }});18 TestRunner testRunner = new TestRunner(testng, test);19 System.out.println(testRunner.getIncludedGroups());20 }21}22import org.testng.annotations.Test;23@Test(groups = {"group1", "group2"})24public class TestClass {25 @Test(groups = {"group1"})26 public void testMethod1() {27 System.out.println("testMethod1");28 }29 @Test(groups = {"group2"})30 public void testMethod2() {31 System.out.println("testMethod2");32 }33}34import org.testng.TestRunner;35import org.testng.TestNG;36import org.testng.xml.XmlSuite;37import org.testng.xml.XmlTest;38public class TestRunnerTest {39 public static void main(String[] args) {40 TestNG testng = new TestNG();41 XmlSuite suite = new XmlSuite();42 suite.setName("Suite");43 XmlTest test = new XmlTest(suite);44 test.setName("Test");45 test.setXmlClasses(new ArrayList<XmlClass>() {{46 add(new XmlClass("com.test.TestClass"));47 }});48 testng.setXmlSuites(new ArrayList<XmlSuite>() {{49 add(suite);50 }});51 TestRunner testRunner = new TestRunner(testng, test);

Full Screen

Full Screen

getIncludedGroups

Using AI Code Generation

copy

Full Screen

1public class TestRunner {2 public static void main(String[] args) {3 TestNG testng = new TestNG();4 List<String> suites = new ArrayList<String>();5 suites.add("testng.xml");6 testng.setTestSuites(suites);7 testng.run();8 System.out.println("Included groups: " + testng.getTestRunner().getIncludedGroups());9 }10}11TestNG 6.9.10 by Cédric Beust (

Full Screen

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful