Best Testng code snippet using org.testng.Interface ITestNGMethod.getGroups
Source:Invoker.java
...396 }397 }398 }399 // check if there are failed @BeforeGroups400 String[] groups= method.getGroups();401 if(null != groups && groups.length > 0) {402 for(String group: groups) {403 if(m_beforegroupsFailures.containsKey(group)) {404 result= false;405 break;406 }407 }408 }409 return result;410 }411 // Creates a token for tracking a unique invocation of a method on an instance.412 // Is used when configFailurePolicy=continue.413 private Object getMethodInvocationToken(ITestNGMethod method, Object instance) {414 return String.format("%s+%d", instance.toString(), method.getCurrentInvocationCount());415 }416 /**417 * Effectively invokes a configuration method on all passed in instances.418 * TODO: Should change this method to be more like invokeMethod() so that we can419 * handle calls to {@code IInvokedMethodListener} better.420 *421 * @param targetInstance the instance to invoke the configuration method on422 * @param tm the configuration method423 * @param params the parameters needed for method invocation424 * @param testResult425 * @throws InvocationTargetException426 * @throws IllegalAccessException427 */428 private void invokeConfigurationMethod(Object targetInstance,429 ITestNGMethod tm,430 Object[] params,431 ITestResult testResult)432 throws InvocationTargetException, IllegalAccessException433 {434 // Mark this method with the current thread id435 tm.setId(ThreadUtil.currentThreadInfo());436 {437 InvokedMethod invokedMethod= new InvokedMethod(targetInstance,438 tm,439 params,440 System.currentTimeMillis(),441 testResult);442 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);443 m_notifier.addInvokedMethod(invokedMethod);444 try {445 Reporter.setCurrentTestResult(testResult);446 Method method = tm.getMethod();447 //448 // If this method is a IConfigurable, invoke its run() method449 //450 IConfigurable configurableInstance =451 IConfigurable.class.isAssignableFrom(tm.getMethod().getDeclaringClass()) ?452 (IConfigurable) targetInstance : m_configuration.getConfigurable();453 if (configurableInstance != null) {454 MethodInvocationHelper.invokeConfigurable(targetInstance, params, configurableInstance, method,455 testResult);456 }457 else {458 //459 // Not a IConfigurable, invoke directly460 //461 if (MethodHelper.calculateTimeOut(tm) <= 0) {462 MethodInvocationHelper.invokeMethod(method, targetInstance, params);463 }464 else {465 MethodInvocationHelper.invokeWithTimeout(tm, targetInstance, params, testResult);466 if (!testResult.isSuccess()) {467 // A time out happened468 throwConfigurationFailure(testResult, testResult.getThrowable());469 throw testResult.getThrowable();470 }471 }472 }473 }474 catch (InvocationTargetException | IllegalAccessException ex) {475 throwConfigurationFailure(testResult, ex);476 throw ex;477 } catch (Throwable ex) {478 throwConfigurationFailure(testResult, ex);479 throw new TestNGException(ex);480 }481 finally {482 Reporter.setCurrentTestResult(testResult);483 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484 Reporter.setCurrentTestResult(null);485 }486 }487 }488 private void throwConfigurationFailure(ITestResult testResult, Throwable ex)489 {490 testResult.setStatus(ITestResult.FAILURE);;491 testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());492 }493 private void runInvokedMethodListeners(InvokedMethodListenerMethod listenerMethod, IInvokedMethod invokedMethod,494 ITestResult testResult)495 {496 if ( noListenersPresent() ) {497 return;498 }499 InvokedMethodListenerInvoker invoker = new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);500 for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {501 invoker.invokeListener(currentListener, invokedMethod);502 }503 }504 private boolean noListenersPresent() {505 return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.size() == 0);506 }507 // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.508 private ITestResult invokeMethod(Object instance,509 final ITestNGMethod tm,510 Object[] parameterValues,511 int parametersIndex,512 XmlSuite suite,513 Map<String, String> params,514 ITestClass testClass,515 ITestNGMethod[] beforeMethods,516 ITestNGMethod[] afterMethods,517 ConfigurationGroupMethods groupMethods,518 FailureContext failureContext) {519 TestResult testResult = new TestResult();520 //521 // Invoke beforeGroups configurations522 //523 invokeBeforeGroupsConfigurations(testClass, tm, groupMethods, suite, params,524 instance);525 //526 // Invoke beforeMethods only if527 // - firstTimeOnly is not set528 // - firstTimeOnly is set, and we are reaching at the first invocationCount529 //530 invokeConfigurations(testClass, tm,531 filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),532 suite, params, parameterValues,533 instance, testResult);534 //535 // Create the ExtraOutput for this method536 //537 InvokedMethod invokedMethod = null;538 try {539 testResult.init(testClass, instance,540 tm,541 null,542 System.currentTimeMillis(),543 0,544 m_testContext);545 testResult.setParameters(parameterValues);546 testResult.setHost(m_testContext.getHost());547 testResult.setStatus(ITestResult.STARTED);548 invokedMethod= new InvokedMethod(instance,549 tm,550 parameterValues,551 System.currentTimeMillis(),552 testResult);553 // Fix from ansgarkonermann554 // invokedMethod is used in the finally, which can be invoked if555 // any of the test listeners throws an exception, therefore,556 // invokedMethod must have a value before we get here557 runTestListeners(testResult);558 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);559 m_notifier.addInvokedMethod(invokedMethod);560 Method thisMethod = tm.getConstructorOrMethod().getMethod();561 if(confInvocationPassed(tm, tm, testClass, instance)) {562 log(3, "Invoking " + tm.getRealClass().getName() + "." + tm.getMethodName());563 Reporter.setCurrentTestResult(testResult);564 // If this method is a IHookable, invoke its run() method565 IHookable hookableInstance =566 IHookable.class.isAssignableFrom(tm.getRealClass()) ?567 (IHookable) instance : m_configuration.getHookable();568 if (MethodHelper.calculateTimeOut(tm) <= 0) {569 if (hookableInstance != null) {570 MethodInvocationHelper.invokeHookable(instance,571 parameterValues, hookableInstance, thisMethod, testResult);572 } else {573 // Not a IHookable, invoke directly574 MethodInvocationHelper.invokeMethod(thisMethod, instance,575 parameterValues);576 }577 testResult.setStatus(ITestResult.SUCCESS);578 } else {579 // Method with a timeout580 MethodInvocationHelper.invokeWithTimeout(tm, instance, parameterValues, testResult, hookableInstance);581 }582 }583 else {584 testResult.setStatus(ITestResult.SKIP);585 }586 }587 catch(InvocationTargetException ite) {588 testResult.setThrowable(ite.getCause());589 testResult.setStatus(ITestResult.FAILURE);590 }591 catch(ThreadExecutionException tee) { // wrapper for TestNGRuntimeException592 Throwable cause= tee.getCause();593 if(TestNGRuntimeException.class.equals(cause.getClass())) {594 testResult.setThrowable(cause.getCause());595 }596 else {597 testResult.setThrowable(cause);598 }599 testResult.setStatus(ITestResult.FAILURE);600 }601 catch(Throwable thr) { // covers the non-wrapper exceptions602 testResult.setThrowable(thr);603 testResult.setStatus(ITestResult.FAILURE);604 }605 finally {606 // Set end time ASAP607 testResult.setEndMillis(System.currentTimeMillis());608 ExpectedExceptionsHolder expectedExceptionClasses609 = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));610 List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);611 handleInvocationResults(tm, results, expectedExceptionClasses, failureContext);612 // If this method has a data provider and just failed, memorize the number613 // at which it failed.614 // Note: we're not exactly testing that this method has a data provider, just615 // that it has parameters, so might have to revisit this if bugs get reported616 // for the case where this method has parameters that don't come from a data617 // provider618 if (testResult.getThrowable() != null && parameterValues.length > 0) {619 tm.addFailedInvocationNumber(parametersIndex);620 }621 //622 // Increment the invocation count for this method623 //624 tm.incrementCurrentInvocationCount();625 // Run invokedMethodListeners after updating TestResult626 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);627 runTestListeners(testResult);628 //629 // Invoke afterMethods only if630 // - lastTimeOnly is not set631 // - lastTimeOnly is set, and we are reaching the last invocationCount632 //633 invokeConfigurations(testClass, tm,634 filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),635 suite, params, parameterValues,636 instance,637 testResult);638 //639 // Invoke afterGroups configurations640 //641 invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite,642 params, instance);643 // Reset the test result last. If we do this too early, Reporter.log()644 // invocations from listeners will be discarded645 Reporter.setCurrentTestResult(null);646 }647 return testResult;648 }649 void collectResults(ITestNGMethod testMethod, Collection<ITestResult> results) {650 for (ITestResult result : results) {651 // Collect the results652 final int status = result.getStatus();653 if(ITestResult.SUCCESS == status) {654 m_notifier.addPassedTest(testMethod, result);655 }656 else if(ITestResult.SKIP == status) {657 m_notifier.addSkippedTest(testMethod, result);658 }659 else if(ITestResult.FAILURE == status) {660 m_notifier.addFailedTest(testMethod, result);661 }662 else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {663 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);664 }665 else {666 assert false : "UNKNOWN STATUS:" + status;667 }668 }669 }670 /**671 * The array of methods contains @BeforeMethods if isBefore if true, @AfterMethods672 * otherwise. This function removes all the methods that should not be run at this673 * point because they are either firstTimeOnly or lastTimeOnly and we haven't reached674 * the current invocationCount yet675 */676 private ITestNGMethod[] filterConfigurationMethods(ITestNGMethod tm,677 ITestNGMethod[] methods, boolean isBefore)678 {679 List<ITestNGMethod> result = Lists.newArrayList();680 for (ITestNGMethod m : methods) {681 ConfigurationMethod cm = (ConfigurationMethod) m;682 if (isBefore) {683 if (! cm.isFirstTimeOnly() ||684 (cm.isFirstTimeOnly() && tm.getCurrentInvocationCount() == 0))685 {686 result.add(m);687 }688 }689 else {690 int current = tm.getCurrentInvocationCount();691 boolean isLast = false;692 // If we have parameters, set the boolean if we are about to run693 // the last invocation694 if (tm.getParameterInvocationCount() > 0) {695 isLast = current == tm.getParameterInvocationCount() * tm.getTotalInvocationCount();696 }697 // If we have invocationCount > 1, set the boolean if we are about to698 // run the last invocation699 else if (tm.getTotalInvocationCount() > 1) {700 isLast = current == tm.getTotalInvocationCount();701 }702 if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && isLast)) {703 result.add(m);704 }705 }706 }707 return result.toArray(new ITestNGMethod[result.size()]);708 }709 /**710 * invokeTestMethods() eventually converge here to invoke a single @Test method.711 * <p/>712 * This method is responsible for actually invoking the method. It decides if the invocation713 * must be done:714 * <ul>715 * <li>through an <code>IHookable</code></li>716 * <li>directly (through reflection)</li>717 * <li>in a separate thread (in case it needs to timeout)718 * </ul>719 *720 * <p/>721 * This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup722 * if it is the case for the passed in @Test method.723 */724 protected ITestResult invokeTestMethod(Object instance,725 final ITestNGMethod tm,726 Object[] parameterValues,727 int parametersIndex,728 XmlSuite suite,729 Map<String, String> params,730 ITestClass testClass,731 ITestNGMethod[] beforeMethods,732 ITestNGMethod[] afterMethods,733 ConfigurationGroupMethods groupMethods,734 FailureContext failureContext)735 {736 // Mark this method with the current thread id737 tm.setId(ThreadUtil.currentThreadInfo());738 ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,739 testClass, beforeMethods, afterMethods, groupMethods,740 failureContext);741 return result;742 }743 /**744 * Filter all the beforeGroups methods and invoke only those that apply745 * to the current test method746 */747 private void invokeBeforeGroupsConfigurations(ITestClass testClass,748 ITestNGMethod currentTestMethod,749 ConfigurationGroupMethods groupMethods,750 XmlSuite suite,751 Map<String, String> params,752 Object instance)753 {754 synchronized(groupMethods) {755 List<ITestNGMethod> filteredMethods = Lists.newArrayList();756 String[] groups = currentTestMethod.getGroups();757 Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();758 for (String group : groups) {759 List<ITestNGMethod> methods = beforeGroupMap.get(group);760 if (methods != null) {761 filteredMethods.addAll(methods);762 }763 }764 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);765 //766 // Invoke the right groups methods767 //768 if(beforeMethodsArray.length > 0) {769 // don't pass the IClass or the instance as the method may be external770 // the invocation must be similar to @BeforeTest/@BeforeSuite771 invokeConfigurations(null, beforeMethodsArray, suite, params,772 null, /* no parameter values */773 null);774 }775 //776 // Remove them so they don't get run again777 //778 groupMethods.removeBeforeGroups(groups);779 }780 }781 private void invokeAfterGroupsConfigurations(ITestClass testClass,782 ITestNGMethod currentTestMethod,783 ConfigurationGroupMethods groupMethods,784 XmlSuite suite,785 Map<String, String> params,786 Object instance)787 {788 // Skip this if the current method doesn't belong to any group789 // (only a method that belongs to a group can trigger the invocation790 // of afterGroups methods)791 if (currentTestMethod.getGroups().length == 0) {792 return;793 }794 // See if the currentMethod is the last method in any of the groups795 // it belongs to796 Map<String, String> filteredGroups = Maps.newHashMap();797 String[] groups = currentTestMethod.getGroups();798 synchronized(groupMethods) {799 for (String group : groups) {800 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {801 filteredGroups.put(group, group);802 }803 }804 if(filteredGroups.isEmpty()) {805 return;806 }807 // The list of afterMethods to run808 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();809 // Now filteredGroups contains all the groups for which we need to run the afterGroups810 // method. Find all the methods that correspond to these groups and invoke them.811 Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();812 for (String g : filteredGroups.values()) {813 List<ITestNGMethod> methods = map.get(g);814 // Note: should put them in a map if we want to make sure the same afterGroups815 // doesn't get run twice816 if (methods != null) {817 for (ITestNGMethod m : methods) {818 afterMethods.put(m, m);819 }820 }821 }822 // Got our afterMethods, invoke them823 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);824 // don't pass the IClass or the instance as the method may be external825 // the invocation must be similar to @BeforeTest/@BeforeSuite826 invokeConfigurations(null, afterMethodsArray, suite, params,827 null, /* no parameter values */828 null);829 // Remove the groups so they don't get run again830 groupMethods.removeAfterGroups(filteredGroups.keySet());831 }832 }833 private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {834 while (parametersValues.hasNext()) {835 Object[] parameters = parametersValues.next();836 if (index == 0) {837 return parameters;838 }839 index--;840 }841 return null;842 }843 int retryFailed(Object instance,844 final ITestNGMethod tm,845 XmlSuite suite,846 ITestClass testClass,847 ITestNGMethod[] beforeMethods,848 ITestNGMethod[] afterMethods,849 ConfigurationGroupMethods groupMethods,850 List<ITestResult> result,851 int failureCount,852 ExpectedExceptionsHolder expectedExceptionHolder,853 ITestContext testContext,854 Map<String, String> parameters,855 int parametersIndex) {856 final FailureContext failure = new FailureContext();857 failure.count = failureCount;858 do {859 failure.instances = Lists.newArrayList ();860 Map<String, String> allParameters = Maps.newHashMap();861 /**862 * TODO: This recreates all the parameters every time when we only need863 * one specific set. Should optimize it by only recreating the set needed.864 */865 ParameterBag bag = createParameters(tm, parameters,866 allParameters, suite, testContext, null /* fedInstance */);867 Object[] parameterValues =868 getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);869 result.add(invokeMethod(instance, tm, parameterValues, parametersIndex, suite,870 allParameters, testClass, beforeMethods, afterMethods, groupMethods, failure));871 }872 while (!failure.instances.isEmpty());873 return failure.count;874 }875 private ParameterBag createParameters(ITestNGMethod testMethod,876 Map<String, String> parameters,877 Map<String, String> allParameterNames,878 XmlSuite suite,879 ITestContext testContext,880 Object fedInstance)881 {882 Object instance;883 if (fedInstance != null) {884 instance = fedInstance;885 }886 else {887 instance = testMethod.getInstance();888 }889 ParameterBag bag = handleParameters(testMethod,890 instance, allParameterNames, parameters, null, suite, testContext, fedInstance, null);891 return bag;892 }893 /**894 * Invoke all the test methods. Note the plural: the method passed in895 * parameter might be invoked several times if the test class it belongs896 * to has more than one instance (i.e., if an @Factory method has been897 * declared somewhere that returns several instances of this TestClass).898 * If no @Factory method was specified, testMethod will only be invoked899 * once.900 * <p/>901 * Note that this method also takes care of invoking the beforeTestMethod902 * and afterTestMethod, if any.903 *904 * Note (alex): this method can be refactored to use a SingleTestMethodWorker that905 * directly invokes906 * {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)}907 * and this would simplify the implementation (see how DataTestMethodWorker is used)908 */909 @Override910 public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,911 XmlSuite suite,912 Map<String, String> testParameters,913 ConfigurationGroupMethods groupMethods,914 Object instance,915 ITestContext testContext)916 {917 // Potential bug here if the test method was declared on a parent class918 assert null != testMethod.getTestClass()919 : "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass();920 if (!MethodHelper.isEnabled(testMethod.getMethod(), m_annotationFinder)) {921 // return if the method is not enabled. No need to do any more calculations922 return Collections.emptyList();923 }924 // By the time this testMethod to be invoked,925 // all dependencies should be already run or we need to skip this method,926 // so invocation count should not affect dependencies check927 final String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());928 if (okToProceed != null) {929 //930 // Not okToProceed. Test is being skipped931 //932 ITestResult result = registerSkippedTestResult(testMethod, null, System.currentTimeMillis(),933 new Throwable(okToProceed));934 m_notifier.addSkippedTest(testMethod, result);935 return Collections.singletonList(result);936 }937 final Map<String, String> parameters =938 testMethod.findMethodParameters(testContext.getCurrentXmlTest());939 // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.940 if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {941 return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);942 }943 long timeOutInvocationCount = testMethod.getInvocationTimeOut();944 //FIXME: Is this correct?945 boolean onlyOne = testMethod.getThreadPoolSize() > 1 ||946 timeOutInvocationCount > 0;947 int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();948 ExpectedExceptionsHolder expectedExceptionHolder =949 new ExpectedExceptionsHolder(m_annotationFinder, testMethod,950 new RegexpExpectedExceptionsHolder(m_annotationFinder, testMethod));951 final ITestClass testClass= testMethod.getTestClass();952 final List<ITestResult> result = Lists.newArrayList();953 final FailureContext failure = new FailureContext();954 final ITestNGMethod[] beforeMethods = filterMethods(testClass, testClass.getBeforeTestMethods(), CAN_RUN_FROM_CLASS);955 final ITestNGMethod[] afterMethods = filterMethods(testClass, testClass.getAfterTestMethods(), CAN_RUN_FROM_CLASS);956 while(invocationCount-- > 0) {957 if(false) {958 // Prevent code formatting959 }960 //961 // No threads, regular invocation962 //963 else {964 // Used in catch statement965 long start = System.currentTimeMillis();966 Map<String, String> allParameterNames = Maps.newHashMap();967 ParameterBag bag = createParameters(testMethod,968 parameters, allParameterNames, suite, testContext, instance);969 if (bag.hasErrors()) {970 final ITestResult tr = bag.errorResult;971 tr.setStatus(ITestResult.SKIP);972 runTestListeners(tr);973 m_notifier.addSkippedTest(testMethod, tr);974 result.add(tr);975 continue;976 }977 Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;978 int parametersIndex = 0;979 try {980 List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();981 if (bag.parameterHolder.origin == ParameterOrigin.ORIGIN_DATA_PROVIDER &&982 bag.parameterHolder.dataProviderHolder.annotation.isParallel()) {983 while (allParameterValues.hasNext()) {984 Object[] parameterValues = injectParameters(allParameterValues.next(),985 testMethod.getMethod(), testContext, null /* test result */);986 TestMethodWithDataProviderMethodWorker w =987 new TestMethodWithDataProviderMethodWorker(this,988 testMethod, parametersIndex,989 parameterValues, instance, suite, parameters, testClass,990 beforeMethods, afterMethods, groupMethods,991 expectedExceptionHolder, testContext, m_skipFailedInvocationCounts,992 invocationCount, failure.count, m_notifier);993 workers.add(w);994 // testng387: increment the param index in the bag.995 parametersIndex++;996 }997 PoolService<List<ITestResult>> ps =998 new PoolService<>(suite.getDataProviderThreadCount());999 List<List<ITestResult>> r = ps.submitTasksAndWait(workers);1000 for (List<ITestResult> l2 : r) {1001 result.addAll(l2);1002 }1003 } else {1004 while (allParameterValues.hasNext()) {1005 Object[] parameterValues = injectParameters(allParameterValues.next(),1006 testMethod.getMethod(), testContext, null /* test result */);1007 List<ITestResult> tmpResults = Lists.newArrayList();1008 try {1009 tmpResults.add(invokeTestMethod(instance,1010 testMethod,1011 parameterValues,1012 parametersIndex,1013 suite,1014 parameters,1015 testClass,1016 beforeMethods,1017 afterMethods,1018 groupMethods, failure));1019 }1020 finally {1021 if (failure.instances.isEmpty()) {1022 result.addAll(tmpResults);1023 } else {1024 for (Object failedInstance : failure.instances) {1025 List<ITestResult> retryResults = Lists.newArrayList();1026 failure.count = retryFailed(1027 failedInstance, testMethod, suite, testClass, beforeMethods,1028 afterMethods, groupMethods, retryResults,1029 failure.count, expectedExceptionHolder,1030 testContext, parameters, parametersIndex);1031 result.addAll(retryResults);1032 }1033 }1034 //1035 // If we have a failure, skip all the1036 // other invocationCounts1037 //1038 if (failure.count > 01039 && (m_skipFailedInvocationCounts1040 || testMethod.skipFailedInvocations())) {1041 while (invocationCount-- > 0) {1042 result.add(registerSkippedTestResult(testMethod, instance, System.currentTimeMillis(), null));1043 }1044 break;1045 }1046 }// end finally1047 parametersIndex++;1048 }1049 }1050 }1051 catch (Throwable cause) {1052 ITestResult r =1053 new TestResult(testMethod.getTestClass(),1054 instance,1055 testMethod,1056 cause,1057 start,1058 System.currentTimeMillis(),1059 m_testContext);1060 r.setStatus(TestResult.FAILURE);1061 result.add(r);1062 runTestListeners(r);1063 m_notifier.addFailedTest(testMethod, r);1064 } // catch1065 }1066 }1067 return result;1068 } // invokeTestMethod1069 private ITestResult registerSkippedTestResult(ITestNGMethod testMethod, Object instance,1070 long start, Throwable throwable) {1071 ITestResult result =1072 new TestResult(testMethod.getTestClass(),1073 instance,1074 testMethod,1075 throwable,1076 start,1077 System.currentTimeMillis(),1078 m_testContext);1079 result.setStatus(TestResult.SKIP);1080 runTestListeners(result);1081 return result;1082 }1083 /**1084 * Gets an array of parameter values returned by data provider or the ones that1085 * are injected based on parameter type. The method also checks for {@code NoInjection}1086 * annotation1087 * @param parameterValues parameter values from a data provider1088 * @param method method to be invoked1089 * @param context test context1090 * @param testResult test result1091 */1092 private Object[] injectParameters(Object[] parameterValues, Method method,1093 ITestContext context, ITestResult testResult)1094 throws TestNGException {1095 List<Object> vResult = Lists.newArrayList();1096 int i = 0;1097 int numValues = parameterValues.length;1098 int numParams = method.getParameterTypes().length;1099 if (numValues > numParams && ! method.isVarArgs()) {1100 throw new TestNGException("The data provider is trying to pass " + numValues1101 + " parameters but the method "1102 + method.getDeclaringClass().getName() + "#" + method.getName()1103 + " takes " + numParams);1104 }1105 // beyond this, numValues <= numParams1106 for (Class<?> cls : method.getParameterTypes()) {1107 Annotation[] annotations = method.getParameterAnnotations()[i];1108 boolean noInjection = false;1109 for (Annotation a : annotations) {1110 if (a instanceof NoInjection) {1111 noInjection = true;1112 break;1113 }1114 }1115 Object injected = Parameters.getInjectedParameter(cls, method, context, testResult);1116 if (injected != null && ! noInjection) {1117 vResult.add(injected);1118 } else {1119 try {1120 if (method.isVarArgs()) vResult.add(parameterValues);1121 else vResult.add(parameterValues[i++]);1122 } catch (ArrayIndexOutOfBoundsException ex) {1123 throw new TestNGException("The data provider is trying to pass " + numValues1124 + " parameters but the method "1125 + method.getDeclaringClass().getName() + "#" + method.getName()1126 + " takes " + numParams1127 + " and TestNG is unable in inject a suitable object", ex);1128 }1129 }1130 }1131 return vResult.toArray(new Object[vResult.size()]);1132 }1133 private ParameterBag handleParameters(ITestNGMethod testMethod,1134 Object instance,1135 Map<String, String> allParameterNames,1136 Map<String, String> parameters,1137 Object[] parameterValues,1138 XmlSuite suite,1139 ITestContext testContext,1140 Object fedInstance,1141 ITestResult testResult)1142 {1143 try {1144 return new ParameterBag(1145 Parameters.handleParameters(testMethod,1146 allParameterNames,1147 instance,1148 new Parameters.MethodParameters(parameters,1149 testMethod.findMethodParameters(testContext.getCurrentXmlTest()),1150 parameterValues,1151 testMethod.getMethod(), testContext, testResult),1152 suite,1153 m_annotationFinder,1154 fedInstance));1155 }1156// catch(TestNGException ex) {1157// throw ex;1158// }1159 catch(Throwable cause) {1160 return new ParameterBag(1161 new TestResult(1162 testMethod.getTestClass(),1163 instance,1164 testMethod,1165 cause,1166 System.currentTimeMillis(),1167 System.currentTimeMillis(),1168 m_testContext));1169 }1170 }1171 /**1172 * Invokes a method that has a specified threadPoolSize.1173 */1174 private List<ITestResult> invokePooledTestMethods(ITestNGMethod testMethod,1175 XmlSuite suite,1176 Map<String, String> parameters,1177 ConfigurationGroupMethods groupMethods,1178 ITestContext testContext)1179 {1180 //1181 // Create the workers1182 //1183 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1184 // Create one worker per invocationCount1185 for (int i = 0; i < testMethod.getInvocationCount(); i++) {1186 // we use clones for reporting purposes1187 ITestNGMethod clonedMethod= testMethod.clone();1188 clonedMethod.setInvocationCount(1);1189 clonedMethod.setThreadPoolSize(1);1190 MethodInstance mi = new MethodInstance(clonedMethod);1191 workers.add(new SingleTestMethodWorker(this,1192 mi,1193 suite,1194 parameters,1195 testContext,1196 m_classListeners));1197 }1198 return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite,1199 parameters);1200 }1201 static class FailureContext {1202 int count = 0;1203 List<Object> instances = Lists.newArrayList();1204 }1205 void handleInvocationResults(ITestNGMethod testMethod,1206 List<ITestResult> result,1207 ExpectedExceptionsHolder expectedExceptionsHolder,1208 FailureContext failure)1209 {1210 //1211 // Go through all the results and create a TestResult for each of them1212 //1213 List<ITestResult> resultsToRetry = Lists.newArrayList();1214 for (ITestResult testResult : result) {1215 Throwable ite= testResult.getThrowable();1216 int status= testResult.getStatus();1217 boolean handled = false;1218 // Exception thrown?1219 if (ite != null) {1220 // Invocation caused an exception, see if the method was annotated with @ExpectedException1221 if (expectedExceptionsHolder != null) {1222 if (expectedExceptionsHolder.isExpectedException(ite)) {1223 testResult.setStatus(ITestResult.SUCCESS);1224 status = ITestResult.SUCCESS;1225 } else {1226 if (isSkipExceptionAndSkip(ite)){1227 status = ITestResult.SKIP;1228 } else {1229 testResult.setThrowable(expectedExceptionsHolder.wrongException(ite));1230 status = ITestResult.FAILURE;1231 }1232 }1233 } else {1234 handleException(ite, testMethod, testResult, failure.count++);1235 handled = true;1236 status = testResult.getStatus();1237 }1238 }1239 // No exception thrown, make sure we weren't expecting one1240 else if(status != ITestResult.SKIP && expectedExceptionsHolder != null) {1241 TestException exception = expectedExceptionsHolder.noException(testMethod);1242 if (exception != null) {1243 testResult.setThrowable(exception);1244 status= ITestResult.FAILURE;1245 }1246 }1247 IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1248 boolean willRetry = retryAnalyzer != null && status == ITestResult.FAILURE && failure.instances != null && retryAnalyzer.retry(testResult);1249 if (willRetry) {1250 resultsToRetry.add(testResult);1251 failure.count++;1252 failure.instances.add(testResult.getInstance());1253 testResult.setStatus(ITestResult.SKIP);1254 } else {1255 testResult.setStatus(status);1256 if (status == ITestResult.FAILURE && !handled) {1257 handleException(ite, testMethod, testResult, failure.count++);1258 }1259 }1260 collectResults(testMethod, Collections.singleton(testResult));1261 } // for results1262 removeResultsToRetryFromResult(resultsToRetry, result, failure);1263 }1264 private boolean isSkipExceptionAndSkip(Throwable ite) {1265 return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip();1266 }1267 private void removeResultsToRetryFromResult(List<ITestResult> resultsToRetry,1268 List<ITestResult> result, FailureContext failure) {1269 if (resultsToRetry != null) {1270 for (ITestResult res : resultsToRetry) {1271 result.remove(res);1272 failure.count--;1273 }1274 }1275 }1276 /**1277 * To reduce thread contention and also to correctly handle thread-confinement1278 * this method invokes the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1279 */1280 private List<ITestResult> runWorkers(ITestNGMethod testMethod,1281 List<IWorker<ITestNGMethod>> workers,1282 int threadPoolSize,1283 ConfigurationGroupMethods groupMethods,1284 XmlSuite suite,1285 Map<String, String> parameters)1286 {1287 // Invoke @BeforeGroups on the original method (reduce thread contention,1288 // and also solve thread confinement)1289 ITestClass testClass= testMethod.getTestClass();1290 Object[] instances = testClass.getInstances(true);1291 for(Object instance: instances) {1292 invokeBeforeGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1293 }1294 long maxTimeOut= -1; // 10 seconds1295 for(IWorker<ITestNGMethod> tmw : workers) {1296 long mt= tmw.getTimeOut();1297 if(mt > maxTimeOut) {1298 maxTimeOut= mt;1299 }1300 }1301 ThreadUtil.execute(workers, threadPoolSize, maxTimeOut, true);1302 //1303 // Collect all the TestResults1304 //1305 List<ITestResult> result = Lists.newArrayList();1306 for (IWorker<ITestNGMethod> tmw : workers) {1307 if (tmw instanceof TestMethodWorker) {1308 result.addAll(((TestMethodWorker)tmw).getTestResults());1309 }1310 }1311 for(Object instance: instances) {1312 invokeAfterGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1313 }1314 return result;1315 }1316 /**1317 * Checks to see of the test method has certain dependencies that prevents1318 * TestNG from executing it1319 * @param testMethod test method being checked for1320 * @return error message or null if dependencies have been run successfully1321 */1322 private String checkDependencies(ITestNGMethod testMethod,1323 ITestNGMethod[] allTestMethods)1324 {1325 // If this method is marked alwaysRun, no need to check for its dependencies1326 if (testMethod.isAlwaysRun()) {1327 return null;1328 }1329 // Any missing group?1330 if (testMethod.getMissingGroup() != null1331 && !testMethod.ignoreMissingDependencies()) {1332 return "Method " + testMethod + " depends on nonexistent group \"" + testMethod.getMissingGroup() + "\"";1333 }1334 // If this method depends on groups, collect all the methods that1335 // belong to these groups and make sure they have been run successfully1336 final String[] groups = testMethod.getGroupsDependedUpon();1337 if (null != groups && groups.length > 0) {1338 // Get all the methods that belong to the group depended upon1339 for (String element : groups) {1340 ITestNGMethod[] methods =1341 MethodGroupsHelper.findMethodsThatBelongToGroup(testMethod,1342 m_testContext.getAllTestMethods(),1343 element);1344 if (methods.length == 0 && !testMethod.ignoreMissingDependencies()) {1345 // Group is missing1346 return "Method " + testMethod + " depends on nonexistent group \"" + element + "\"";1347 }1348 if (!haveBeenRunSuccessfully(testMethod, methods)) {1349 return "Method " + testMethod +1350 " depends on not successfully finished methods in group \"" + element + "\"";...
Source:MyReporterListener.java
...286 }287 private String qualifiedName(ITestNGMethod method) 288 {289 StringBuilder addon = new StringBuilder();290 String[] groups = method.getGroups();291 int length = groups.length;292 if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {293 addon.append("(");294 for (int i = 0; i < length; i++) {295 if (i > 0) {296 addon.append(", ");297 }298 addon.append(groups[i]);299 }300 addon.append(")");301 }302 return "<b>" + method.getMethodName() + "</b> " + addon;303 }304 ...
Source:ITestNGMethod.java
...69 /**70 * @return The groups this method belongs to, possibly added to the groups71 * declared on the class.72 */73 String[] getGroups();7475 /**76 * @return The groups this method depends on, possibly added to the groups77 * declared on the class.78 */79 String[] getGroupsDependedUpon();8081 /**82 * If a group was not found.83 */84 String getMissingGroup();85 public void setMissingGroup(String group);8687 /**88 * Before and After groups89 */90 public String[] getBeforeGroups();91 public String[] getAfterGroups();9293 /**
...
Source:MethodInheritance.java
...124 for (int i = 0; i < l.size() - 1; i++) {125 ITestNGMethod m1 = l.get(i);126 for (int j = i + 1; j < l.size(); j++) {127 ITestNGMethod m2 = l.get(j);128 String[] groups = Optional.ofNullable(m2.getGroups()).orElse(new String[] {});129 if (groups.length != 0) {130 //Do not resort to adding implicit depends-on if there are groups131 continue;132 }133 if (!equalsEffectiveClass(m1, m2) && !dependencyExists(m1, m2, methods)) {134 Utils.log("MethodInheritance", 4, m2 + " DEPENDS ON " + m1);135 m2.addMethodDependedUpon(MethodHelper.calculateMethodCanonicalName(m1));136 }137 }138 }139 });140 }141 private static boolean dependencyExists(142 ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {143 return internalDependencyExists(m1, m2, methods) || internalDependencyExists(m2, m1, methods);144 }145 private static boolean internalDependencyExists(146 ITestNGMethod m1, ITestNGMethod m2, ITestNGMethod[] methods) {147 ITestNGMethod[] methodsNamed = MethodHelper.findDependedUponMethods(m1, methods);148 boolean match = Arrays.stream(methodsNamed)149 .parallel()150 .anyMatch(method -> method.equals(m2));151 if (match) {152 return true;153 }154 return Arrays.stream(m1.getGroupsDependedUpon())155 .parallel()156 .anyMatch(group -> {157 ITestNGMethod[] methodsThatBelongToGroup =158 MethodGroupsHelper.findMethodsThatBelongToGroup(m1, methods, group);159 return Arrays.stream(methodsThatBelongToGroup)160 .parallel()161 .anyMatch(method -> method.equals(m2));162 }163 );164 }165 private static boolean equalsEffectiveClass(ITestNGMethod m1, ITestNGMethod m2) {166 try {167 Class<?> c1 = m1.getRealClass();168 Class<?> c2 = m2.getRealClass();...
Source:RetryEnabledListener.java
...31 @Override32 public void onStart(final ITestContext ctx) {33 super.onStart(ctx);34 for (final ITestNGMethod method : ctx.getAllTestMethods()) {35 final List<String> groupList = DeviceUtil.arrayToList(method.getGroups());36 if (groupList == null || !groupList.contains("stub")) {37 method.setRetryAnalyzer(new MobileRetryAnalyzer());38 }39 }40 }41}
getGroups
Using AI Code Generation
1public class TestNGTest {2 public static void main(String[] args) {3 TestNG testng = new TestNG();4 testng.setTestClasses(new Class[] { TestClass.class });5 testng.run();6 }7}8public class TestClass {9 @Test(groups = { "group1" })10 public void testMethod1() {11 System.out.println("TestNGTest.testMethod1()");12 }13 @Test(groups = { "group2" })14 public void testMethod2() {15 System.out.println("TestNGTest.testMethod2()");16 }17 @Test(groups = { "group1", "group2" })18 public void testMethod3() {19 System.out.println("TestNGTest.testMethod3()");20 }21}
getGroups
Using AI Code Generation
1package com.qa.testscripts;2import java.lang.reflect.Method;3import org.testng.ITestNGMethod;4import org.testng.annotations.Test;5public class TestNGDemo {6@Test(groups={"regression"})7public void test1() {8 System.out.println("Test1");9}10@Test(groups={"regression"})11public void test2() {12 System.out.println("Test2");13}14@Test(groups={"smoke"})15public void test3() {16 System.out.println("Test3");17}18@Test(groups={"smoke"})19public void test4() {20 System.out.println("Test4");21}22@Test(groups={"smoke"})23public void test5() {24 System.out.println("Test5");25}26@Test(groups={"smoke"})27public void test6() {28 System.out.println("Test6");29}30@Test(groups={"smoke"})31public void test7() {32 System.out.println("Test7");33}34@Test(groups={"smoke"})35public void test8() {36 System.out.println("Test8");37}38@Test(groups={"smoke"})39public void test9() {40 System.out.println("Test9");41}42@Test(groups={"smoke"})43public void test10() {44 System.out.println("Test10");45}46@Test(groups={"smoke"})47public void test11() {48 System.out.println("Test11");49}50@Test(groups={"smoke"})51public void test12() {52 System.out.println("Test12");53}54@Test(groups={"smoke"})55public void test13() {56 System.out.println("Test13");57}58@Test(groups={"smoke"})59public void test14() {60 System.out.println("Test14");61}62@Test(groups={"smoke"})63public void test15() {64 System.out.println("Test15");65}66@Test(groups={"smoke"})67public void test16() {68 System.out.println("Test16");69}70@Test(groups={"smoke"})71public void test17() {72 System.out.println("Test17");73}74@Test(groups={"smoke"})75public void test18() {76 System.out.println("Test18");77}78@Test(groups={"smoke"})79public void test19() {80 System.out.println("Test19");81}82@Test(groups={"smoke"})83public void test20() {84 System.out.println("Test20");85}86@Test(groups={"smoke"})87public void test21() {88 System.out.println("Test21");89}90@Test(groups={"smoke"})91public void test22() {92 System.out.println("Test22");93}94@Test(groups={"
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!!