Best Testng code snippet using org.testng.TestRunner.getExcludedGroups
Source:TestRunner.java
...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() {799 return m_suite;800 }801 @Override802 public ITestNGMethod[] getAllTestMethods() {803 return m_allTestMethods;...
Source:BaseTest.java
...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);181 }182 private void setTest(XmlTest test) {183 XmlTest t = m_tests.get(getId());...
Source:TestNgRunner.java
...71 core.run();72 return eventTranslator.getTestResults();73 }74 private void applyConfig(TestNG core, TestNGConfiguration config) {75 core.setExcludedGroups(config.getExcludedGroups());76 core.setGroups(config.getGroups());77 if (config.getListeners() != null) {78 for (Object listener : config.getListeners()) {79 core.addListener(listener);80 }81 }82 }83 84 private static class TestNGEventTranslator implements ITestListener {85 private final List<TestEvent> events = new ArrayList<TestEvent>();86 @Override87 public void onTestFailure(ITestResult failure) {88 events.add(createEventFrom(failure));89 }...
getExcludedGroups
Using AI Code Generation
1public void testGetExcludedGroups() {2 TestRunner runner = new TestRunner();3 String[] excludedGroups = runner.getExcludedGroups();4 for (String group : excludedGroups) {5 System.out.println(group);6 }7}
getExcludedGroups
Using AI Code Generation
1import org.testng.TestRunner;2TestRunner testRunner = new TestRunner();3String[] excludedGroups = testRunner.getExcludedGroups();4for (String group : excludedGroups) {5 System.out.println(group);6}
getExcludedGroups
Using AI Code Generation
1import org.testng.TestRunner;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import java.util.*;5public class TestRunnerDemo {6 public static void main(String[] args) {7 XmlSuite suite = new XmlSuite();8 suite.setName("TestNG Suite");9 Map<String,String> params = new HashMap<String,String>();10 params.put("browser", "chrome");11 params.put("env", "dev");12 suite.setParameters(params);13 List<String> files = new ArrayList<String>();14 files.add("C:\\Users\\MyUser\\Desktop\\TestNG\\src\\testng.xml");15 suite.setSuiteFiles(files);16 List<XmlSuite> suites = new ArrayList<XmlSuite>();17 suites.add(suite);18 TestNG tng = new TestNG();19 tng.setXmlSuites(suites);20 tng.run();21 TestRunner runner = new TestRunner(suite, 1);22 System.out.println(runner.getExcludedGroups());23 }24}
getExcludedGroups
Using AI Code Generation
1@Test(groups = "group1")2public class Test1 {3 @Test(groups = "group2")4 public void test2() {5 }6}7@Test(groups = "group1")8public class Test1 {9 @Test(groups = "group2")10 public void test2() {11 }12}13@Test(groups = "group1")14public class Test1 {15 @Test(groups = "group2")16 public void test2() {17 }18}19@Test(groups = "group1")20public class Test1 {21 @Test(groups = "group2")22 public void test2() {23 }24}25@Test(groups = "group1")26public class Test1 {27 @Test(groups = "group2")28 public void test2() {29 }30}31@Test(groups = "group1")32public class Test1 {33 @Test(groups = "group2")34 public void test2() {35 }36}37@Test(groups = "group1")38public class Test1 {39 @Test(groups = "group2")40 public void test2() {41 }42}43@Test(groups = "group1")44public class Test1 {45 @Test(groups = "group2")46 public void test2() {47 }48}49@Test(groups = "group1")50public class Test1 {51 @Test(groups = "group2")52 public void test2() {53 }54}55@Test(groups = "group1")56public class Test1 {57 @Test(groups = "group2")58 public void test2() {59 }60}61@Test(groups = "group1")62public class Test1 {63 @Test(groups = "group2")64 public void test2() {65 }66}
getExcludedGroups
Using AI Code Generation
1import org.testng.TestRunner;2import org.testng.TestNG;3import org.testng.xml.XmlSuite;4import org.testng.xml.XmlTest;5import java.util.List;6import java.util.ArrayList;7import java.util.Arrays;8public class TestRunnerExample {9 public static void main(String[] args) {10 TestNG testng = new TestNG();11 XmlSuite suite = new XmlSuite();12 suite.setName("TestNG Suite");13 XmlTest test = new XmlTest(suite);14 test.setName("TestNG Test");15 List<String> files = new ArrayList<String>();16 files.add("testng.xml");17 test.setSuiteFiles(files);18 testng.setXmlSuites(Arrays.asList(suite));19 testng.run();20 TestRunner runner = testng.getTestRunner();21 String[] excludedGroups = runner.getExcludedGroups();22 System.out.println("Excluded groups: " + Arrays.toString(excludedGroups));23 }24}
getExcludedGroups
Using AI Code Generation
1package com.java2blog.testng.examples;2import org.testng.TestNG;3import org.testng.TestRunner;4public class TestNGExcludedGroupsExample {5public static void main(String[] args) {6 TestNG testNG = new TestNG();7 TestRunner testRunner = new TestRunner();8 testRunner.setExcludedGroups("excludedGroup1");9 System.out.println(testRunner.getExcludedGroups());10 testNG.setTestClasses(new Class[] { TestNGExcludedGroupsExample.class });11 testNG.run();12 }13}
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!!