Best Testng code snippet using org.testng.Interface ITestNGMethod.getInvocationCount
Source:Invoker.java
...770 isLast = current == tm.getParameterInvocationCount();771 }772 // If we have invocationCount > 1, set the boolean if we are about to773 // run the last invocation774 else if (tm.getInvocationCount() > 1) {775 isLast = current == tm.getInvocationCount();776 }777 if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && isLast)) {778 result.add(m);779 }780 }781 }782 return result.toArray(new ITestNGMethod[result.size()]);783 }784 /**785 * {@link #invokeTestMethods()} eventually converge here to invoke a single @Test method.786 * <p/>787 * This method is responsible for actually invoking the method. It decides if the invocation788 * must be done:789 * <ul>790 * <li>through an <code>IHookable</code></li>791 * <li>directly (through reflection)</li>792 * <li>in a separate thread (in case it needs to timeout)793 * </ul>794 *795 * <p/>796 * This method is also reponsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup797 * if it is the case for the passed in @Test method.798 */799 protected List<ITestResult> invokeTestMethod(Object[] instances,800 final ITestNGMethod tm,801 Object[] parameterValues,802 int parametersIndex,803 XmlSuite suite,804 Map<String, String> params,805 ITestClass testClass,806 ITestNGMethod[] beforeMethods,807 ITestNGMethod[] afterMethods,808 ConfigurationGroupMethods groupMethods)809 {810 List<ITestResult> results = Lists.newArrayList();811 // Mark this method with the current thread id812 tm.setId(ThreadUtil.currentThreadInfo());813 for(int i= 0; i < instances.length; i++) {814 results.add(invokeMethod(instances, i, tm, parameterValues, parametersIndex, suite, params,815 testClass, beforeMethods, afterMethods, groupMethods));816 }817 return results;818 }819 /**820 * Filter all the beforeGroups methods and invoke only those that apply821 * to the current test method822 */823 private void invokeBeforeGroupsConfigurations(ITestClass testClass,824 ITestNGMethod currentTestMethod,825 ConfigurationGroupMethods groupMethods,826 XmlSuite suite,827 Map<String, String> params,828 Object instance)829 {830 synchronized(groupMethods) {831 List<ITestNGMethod> filteredMethods = Lists.newArrayList();832 String[] groups = currentTestMethod.getGroups();833 Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();834 for (String group : groups) {835 List<ITestNGMethod> methods = beforeGroupMap.get(group);836 if (methods != null) {837 filteredMethods.addAll(methods);838 }839 }840 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);841 //842 // Invoke the right groups methods843 //844 if(beforeMethodsArray.length > 0) {845 // don't pass the IClass or the instance as the method may be external846 // the invocation must be similar to @BeforeTest/@BeforeSuite847 invokeConfigurations(null, beforeMethodsArray, suite, params,848 null, /* no parameter values */849 null);850 }851 //852 // Remove them so they don't get run again853 //854 groupMethods.removeBeforeGroups(groups);855 }856 }857 private void invokeAfterGroupsConfigurations(ITestClass testClass,858 ITestNGMethod currentTestMethod,859 ConfigurationGroupMethods groupMethods,860 XmlSuite suite,861 Map<String, String> params,862 Object instance)863 {864 // Skip this if the current method doesn't belong to any group865 // (only a method that belongs to a group can trigger the invocation866 // of afterGroups methods)867 if (currentTestMethod.getGroups().length == 0) {868 return;869 }870 // See if the currentMethod is the last method in any of the groups871 // it belongs to872 Map<String, String> filteredGroups = Maps.newHashMap();873 String[] groups = currentTestMethod.getGroups();874 synchronized(groupMethods) {875 for (String group : groups) {876 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {877 filteredGroups.put(group, group);878 }879 }880 if(filteredGroups.isEmpty()) {881 return;882 }883 // The list of afterMethods to run884 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();885 // Now filteredGroups contains all the groups for which we need to run the afterGroups886 // method. Find all the methods that correspond to these groups and invoke them.887 Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();888 for (String g : filteredGroups.values()) {889 List<ITestNGMethod> methods = map.get(g);890 // Note: should put them in a map if we want to make sure the same afterGroups891 // doesn't get run twice892 if (methods != null) {893 for (ITestNGMethod m : methods) {894 afterMethods.put(m, m);895 }896 }897 }898 // Got our afterMethods, invoke them899 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);900 // don't pass the IClass or the instance as the method may be external901 // the invocation must be similar to @BeforeTest/@BeforeSuite902 invokeConfigurations(null, afterMethodsArray, suite, params,903 null, /* no parameter values */904 null);905 // Remove the groups so they don't get run again906 groupMethods.removeAfterGroups(filteredGroups.keySet());907 }908 }909 private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {910 while (parametersValues.hasNext()) {911 Object[] parameters = parametersValues.next();912 if (index == 0) {913 return parameters;914 }915 index--;916 }917 return null;918 }919 int retryFailed(Object[] instances,920 int instanceIndex,921 final ITestNGMethod tm,922 XmlSuite suite,923 ITestClass testClass,924 ITestNGMethod[] beforeMethods,925 ITestNGMethod[] afterMethods,926 ConfigurationGroupMethods groupMethods,927 List<ITestResult> result,928 int failureCount,929 ExpectedExceptionsHolder expectedExceptionHolder,930 ITestContext testContext,931 Map<String, String> parameters,932 int parametersIndex) {933 List<Object> failedInstances;934 do {935 failedInstances = Lists.newArrayList();936 Map<String, String> allParameters = Maps.newHashMap();937 /**938 * TODO: This recreates all the parameters every time when we only need939 * one specific set. Should optimize it by only recreating the set needed.940 */941 ParameterBag bag = createParameters(tm, parameters,942 allParameters, null, suite, testContext, null /* fedInstance */, null /* testResult */);943 Object[] parameterValues =944 getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);945 result.add(invokeMethod(instances, instanceIndex, tm, parameterValues,parametersIndex, suite,946 allParameters, testClass, beforeMethods, afterMethods, groupMethods));947 failureCount = handleInvocationResults(tm, result, failedInstances,948 failureCount, expectedExceptionHolder, true, true /* collect results */);949 }950 while (!failedInstances.isEmpty());951 return failureCount;952 }953 private ParameterBag createParameters(ITestNGMethod testMethod,954 Map<String, String> parameters,955 Map<String, String> allParameterNames,956 Object[] parameterValues,957 XmlSuite suite,958 ITestContext testContext,959 Object fedInstance,960 ITestResult testResult)961 {962 Object instance;963 if (fedInstance != null) {964 instance = fedInstance;965 }966 else {967 instance = testMethod.getInstance();968 }969 ParameterBag bag= handleParameters(testMethod,970 instance, allParameterNames, parameters, parameterValues, suite, testContext, fedInstance,971 testResult);972 return bag;973 }974 /**975 * Invoke all the test methods. Note the plural: the method passed in976 * parameter might be invoked several times if the test class it belongs977 * to has more than one instance (i.e., if an @Factory method has been978 * declared somewhere that returns several instances of this TestClass).979 * If no @Factory method was specified, testMethod will only be invoked980 * once.981 * <p/>982 * Note that this method also takes care of invoking the beforeTestMethod983 * and afterTestMethod, if any.984 *985 * Note (alex): this method can be refactored to use a SingleTestMethodWorker that986 * directly invokes987 * {@link #invokeTestMethod(Object[], ITestNGMethod, Object[], XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods)}988 * and this would simplify the implementation (see how DataTestMethodWorker is used)989 */990 @Override991 public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,992 ITestNGMethod[] allTestMethods,993 int testMethodIndex,994 XmlSuite suite,995 Map<String, String> testParameters,996 ConfigurationGroupMethods groupMethods,997 Object[] instances,998 ITestContext testContext)999 {1000 // Potential bug here if the test method was declared on a parent class1001 assert null != testMethod.getTestClass()1002 : "COULDN'T FIND TESTCLASS FOR " + testMethod.getMethod().getDeclaringClass();1003 List<ITestResult> result = Lists.newArrayList();1004 if (!MethodHelper.isEnabled(testMethod.getMethod(), m_annotationFinder)) {1005 /*1006 * return if the method is not enabled. No need to do any more calculations1007 */1008 return result;1009 }1010 ITestClass testClass= testMethod.getTestClass();1011 long start = System.currentTimeMillis();1012 // For invocationCount > 1 and threadPoolSize > 1 the method will be invoked on a thread pool1013 long timeOutInvocationCount = testMethod.getInvocationTimeOut();1014 //FIXME: Is this correct?1015 boolean onlyOne = testMethod.getThreadPoolSize() > 1 ||1016 timeOutInvocationCount > 0;1017 int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();1018 int failureCount = 0;1019 ExpectedExceptionsHolder expectedExceptionHolder =1020 MethodHelper.findExpectedExceptions(m_annotationFinder, testMethod.getMethod());1021 while(invocationCount-- > 0) {1022 boolean okToProceed = checkDependencies(testMethod, allTestMethods);1023 if (!okToProceed) {1024 //1025 // Not okToProceed. Test is being skipped1026 //1027 ITestResult testResult = new TestResult(testClass, null /* instance */,1028 testMethod,1029 null /* cause */,1030 start,1031 System.currentTimeMillis(),1032 m_testContext);1033 String missingGroup = testMethod.getMissingGroup();1034 if (missingGroup != null) {1035 testResult.setThrowable(new Throwable("Method " + testMethod1036 + " depends on nonexistent group \"" + missingGroup + "\""));1037 }1038 testResult.setStatus(ITestResult.SKIP);1039 result.add(testResult);1040 m_notifier.addSkippedTest(testMethod, testResult);1041 runTestListeners(testResult);1042 return result;1043 }1044 //1045 // If threadPoolSize specified, run this method in its own pool thread.1046 //1047 Map<String, String> parameters =1048 testMethod.findMethodParameters(testContext.getCurrentXmlTest());1049 if (testMethod.getThreadPoolSize() > 1 && testMethod.getInvocationCount() > 1) {1050 return invokePooledTestMethods(testMethod, allTestMethods, suite,1051 parameters, groupMethods, testContext);1052 }1053 //1054 // No threads, regular invocation1055 //1056 else {1057 ITestNGMethod[] beforeMethods = filterMethods(testClass, testClass.getBeforeTestMethods(),1058 CAN_RUN_FROM_CLASS);1059 ITestNGMethod[] afterMethods = filterMethods(testClass, testClass.getAfterTestMethods(),1060 CAN_RUN_FROM_CLASS);1061 Map<String, String> allParameterNames = Maps.newHashMap();1062 ParameterBag bag = createParameters(testMethod,1063 parameters, allParameterNames, null, suite, testContext, instances[0],1064 null);1065 if (bag.hasErrors()) {1066 failureCount = handleInvocationResults(testMethod,1067 bag.errorResults, null, failureCount, expectedExceptionHolder, true,1068 true /* collect results */);1069 ITestResult tr = registerSkippedTestResult(testMethod, instances[0], start,1070 bag.errorResults.get(0).getThrowable());1071 result.add(tr);1072 continue;1073 }1074 Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;1075 int parametersIndex = 0;1076 try {1077 List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();1078 if (bag.parameterHolder.origin == ParameterOrigin.ORIGIN_DATA_PROVIDER &&1079 bag.parameterHolder.dataProviderHolder.annotation.isParallel()) {1080 while (allParameterValues.hasNext()) {1081 Object[] parameterValues = injectParameters(allParameterValues.next(),1082 testMethod.getMethod(), testContext, null /* test result */);1083 TestMethodWithDataProviderMethodWorker w =1084 new TestMethodWithDataProviderMethodWorker(this,1085 testMethod, parametersIndex,1086 parameterValues, instances, suite, parameters, testClass,1087 beforeMethods, afterMethods, groupMethods,1088 expectedExceptionHolder, testContext, m_skipFailedInvocationCounts,1089 invocationCount, failureCount, m_notifier);1090 workers.add(w);1091 // testng387: increment the param index in the bag.1092 parametersIndex++;1093 }1094 PoolService<List<ITestResult>> ps =1095 new PoolService<List<ITestResult>>(suite.getDataProviderThreadCount());1096 List<List<ITestResult>> r = ps.submitTasksAndWait(workers);1097 for (List<ITestResult> l2 : r) {1098 result.addAll(l2);1099 }1100 } else {1101 while (allParameterValues.hasNext()) {1102 Object[] parameterValues = injectParameters(allParameterValues.next(),1103 testMethod.getMethod(), testContext, null /* test result */);1104 List<ITestResult> tmpResults = Lists.newArrayList();1105 try {1106 tmpResults.addAll(invokeTestMethod(instances,1107 testMethod,1108 parameterValues,1109 parametersIndex,1110 suite,1111 parameters,1112 testClass,1113 beforeMethods,1114 afterMethods,1115 groupMethods));1116 }1117 finally {1118 List<Object> failedInstances = Lists.newArrayList();1119 failureCount = handleInvocationResults(testMethod, tmpResults,1120 failedInstances, failureCount, expectedExceptionHolder, true,1121 false /* don't collect results */);1122 if (failedInstances.isEmpty()) {1123 result.addAll(tmpResults);1124 } else {1125 for (int i = 0; i < failedInstances.size(); i++) {1126 List<ITestResult> retryResults = Lists.newArrayList();1127 failureCount =1128 retryFailed(failedInstances.toArray(),1129 i, testMethod, suite, testClass, beforeMethods,1130 afterMethods, groupMethods, retryResults,1131 failureCount, expectedExceptionHolder,1132 testContext, parameters, parametersIndex);1133 result.addAll(retryResults);1134 }1135 }1136 //1137 // If we have a failure, skip all the1138 // other invocationCounts1139 //1140 if (failureCount > 01141 && (m_skipFailedInvocationCounts1142 || testMethod.skipFailedInvocations())) {1143 while (invocationCount-- > 0) {1144 result.add(registerSkippedTestResult(testMethod, instances[0], start, null));1145 }1146 break;1147 }1148 }// end finally1149 parametersIndex++;1150 }1151 }1152 }1153 catch (Throwable cause) {1154 ITestResult r =1155 new TestResult(testMethod.getTestClass(),1156 instances[0],1157 testMethod,1158 cause,1159 start,1160 System.currentTimeMillis(),1161 m_testContext);1162 r.setStatus(TestResult.FAILURE);1163 result.add(r);1164 runTestListeners(r);1165 m_notifier.addFailedTest(testMethod, r);1166 } // catch1167 }1168 }1169 return result;1170 } // invokeTestMethod1171 private ITestResult registerSkippedTestResult(ITestNGMethod testMethod, Object instance,1172 long start, Throwable throwable) {1173 ITestResult result =1174 new TestResult(testMethod.getTestClass(),1175 instance,1176 testMethod,1177 throwable,1178 start,1179 System.currentTimeMillis(),1180 m_testContext);1181 result.setStatus(TestResult.SKIP);1182 runTestListeners(result);1183 return result;1184 }1185 /**1186 * Gets an array of parameter values returned by data provider or the ones that1187 * are injected based on parameter type. The method also checks for {@code NoInjection}1188 * annotation1189 * @param parameterValues parameter values from a data provider1190 * @param method method to be invoked1191 * @param context test context1192 * @param testResult test result1193 * @return1194 */1195 private Object[] injectParameters(Object[] parameterValues, Method method,1196 ITestContext context, ITestResult testResult)1197 throws TestNGException {1198 List<Object> vResult = Lists.newArrayList();1199 int i = 0;1200 int numValues = parameterValues.length;1201 int numParams = method.getParameterTypes().length;1202 if (numValues > numParams && ! method.isVarArgs()) {1203 throw new TestNGException("The data provider is trying to pass " + numValues1204 + " parameters but the method "1205 + method.getDeclaringClass().getName() + "#" + method.getName()1206 + " takes " + numParams);1207 }1208 // beyond this, numValues <= numParams1209 for (Class<?> cls : method.getParameterTypes()) {1210 Annotation[] annotations = method.getParameterAnnotations()[i];1211 boolean noInjection = false;1212 for (Annotation a : annotations) {1213 if (a instanceof NoInjection) {1214 noInjection = true;1215 break;1216 }1217 }1218 Object injected = Parameters.getInjectedParameter(cls, method, context, testResult);1219 if (injected != null && ! noInjection) {1220 vResult.add(injected);1221 } else {1222 try {1223 if (method.isVarArgs()) vResult.add(parameterValues);1224 else vResult.add(parameterValues[i++]);1225 } catch (ArrayIndexOutOfBoundsException ex) {1226 throw new TestNGException("The data provider is trying to pass " + numValues1227 + " parameters but the method "1228 + method.getDeclaringClass().getName() + "#" + method.getName()1229 + " takes " + numParams1230 + " and TestNG is unable in inject a suitable object", ex);1231 }1232 }1233 }1234 return vResult.toArray(new Object[vResult.size()]);1235 }1236 private ParameterBag handleParameters(ITestNGMethod testMethod,1237 Object instance,1238 Map<String, String> allParameterNames,1239 Map<String, String> parameters,1240 Object[] parameterValues,1241 XmlSuite suite,1242 ITestContext testContext,1243 Object fedInstance,1244 ITestResult testResult)1245 {1246 try {1247 return new ParameterBag(1248 Parameters.handleParameters(testMethod,1249 allParameterNames,1250 instance,1251 new Parameters.MethodParameters(parameters,1252 testMethod.findMethodParameters(testContext.getCurrentXmlTest()),1253 parameterValues,1254 testMethod.getMethod(), testContext, testResult),1255 suite,1256 m_annotationFinder,1257 fedInstance),1258 null /* TestResult */);1259 }1260// catch(TestNGException ex) {1261// throw ex;1262// }1263 catch(Throwable cause) {1264 return new ParameterBag(null /* ParameterHolder */,1265 new TestResult(1266 testMethod.getTestClass(),1267 instance,1268 testMethod,1269 cause,1270 System.currentTimeMillis(),1271 System.currentTimeMillis(),1272 m_testContext));1273 }1274 }1275 /**1276 * Invokes a method that has a specified threadPoolSize.1277 */1278 private List<ITestResult> invokePooledTestMethods(ITestNGMethod testMethod,1279 ITestNGMethod[] allTestMethods,1280 XmlSuite suite,1281 Map<String, String> parameters,1282 ConfigurationGroupMethods groupMethods,1283 ITestContext testContext)1284 {1285 //1286 // Create the workers1287 //1288 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1289 // Create one worker per invocationCount1290 for (int i = 0; i < testMethod.getInvocationCount(); i++) {1291 // we use clones for reporting purposes1292 ITestNGMethod clonedMethod= testMethod.clone();1293 clonedMethod.setInvocationCount(1);1294 clonedMethod.setThreadPoolSize(1);1295 MethodInstance mi = new MethodInstance(clonedMethod);1296 workers.add(new SingleTestMethodWorker(this,1297 mi,1298 suite,1299 parameters,1300 allTestMethods,1301 testContext));1302 }1303 return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite, parameters);1304 }1305 /**1306 * @param testMethod1307 * @param result1308 * @param failureCount1309 * @param expectedExceptionsHolder1310 * @return1311 */1312 int handleInvocationResults(ITestNGMethod testMethod,1313 List<ITestResult> result,1314 List<Object> failedInstances,1315 int failureCount,1316 ExpectedExceptionsHolder expectedExceptionsHolder,1317 boolean triggerListeners,1318 boolean collectResults)1319 {1320 //1321 // Go through all the results and create a TestResult for each of them1322 //1323 List<ITestResult> resultsToRetry = Lists.newArrayList();1324 for (int i = 0; i < result.size(); i++) {1325 ITestResult testResult = result.get(i);1326 Throwable ite= testResult.getThrowable();1327 int status= testResult.getStatus();1328 // Exception thrown?1329 if (ite != null) {1330 // Invocation caused an exception, see if the method was annotated with @ExpectedException1331 if (isExpectedException(ite, expectedExceptionsHolder)) {1332 if (messageRegExpMatches(expectedExceptionsHolder.messageRegExp, ite)) {1333 testResult.setStatus(ITestResult.SUCCESS);1334 status= ITestResult.SUCCESS;1335 }1336 else {1337 testResult.setThrowable(1338 new TestException("The exception was thrown with the wrong message:" +1339 " expected \"" + expectedExceptionsHolder.messageRegExp + "\"" +1340 " but got \"" + ite.getMessage() + "\"", ite));1341 status= ITestResult.FAILURE;1342 }1343 } else if (SkipException.class.isAssignableFrom(ite.getClass())){1344 SkipException skipEx= (SkipException) ite;1345 if(skipEx.isSkip()) {1346 status = ITestResult.SKIP;1347 }1348 else {1349 handleException(ite, testMethod, testResult, failureCount++);1350 status = ITestResult.FAILURE;1351 }1352 } else if (ite != null && expectedExceptionsHolder != null) {1353 testResult.setThrowable(1354 new TestException("Expected exception "1355 + expectedExceptionsHolder.expectedClasses[0].getName()1356 + " but got " + ite, ite));1357 status= ITestResult.FAILURE;1358 } else {1359 handleException(ite, testMethod, testResult, failureCount++);1360 status= testResult.getStatus();1361 }1362 }1363 // No exception thrown, make sure we weren't expecting one1364 else if(status != ITestResult.SKIP && expectedExceptionsHolder != null) {1365 Class<?>[] classes = expectedExceptionsHolder.expectedClasses;1366 if (classes != null && classes.length > 0) {1367 testResult.setThrowable(1368 new TestException("Method " + testMethod + " should have thrown an exception of "1369 + expectedExceptionsHolder.expectedClasses[0]));1370 status= ITestResult.FAILURE;1371 }1372 }1373 testResult.setStatus(status);1374 boolean retry = false;1375 if (testResult.getStatus() == ITestResult.FAILURE) {1376 IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1377 if (retryAnalyzer != null && failedInstances != null) {1378 retry = retryAnalyzer.retry(testResult);1379 }1380 if (retry) {1381 resultsToRetry.add(testResult);1382 if (failedInstances != null) {1383 failedInstances.add(testResult.getInstance());1384 }1385 }1386 }1387 if (collectResults) {1388 // Collect the results1389 if(ITestResult.SUCCESS == status) {1390 m_notifier.addPassedTest(testMethod, testResult);1391 }1392 else if(ITestResult.SKIP == status) {1393 m_notifier.addSkippedTest(testMethod, testResult);1394 }1395 else if(ITestResult.FAILURE == status) {1396 m_notifier.addFailedTest(testMethod, testResult);1397 }1398 else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {1399 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, testResult);1400 }1401 else {1402 assert false : "UNKNOWN STATUS:" + status;1403 }1404// if (triggerListeners && status != ITestResult.SUCCESS) {1405// runTestListeners(testResult);1406// }1407 }1408 } // for results1409 return removeResultsToRetryFromResult(resultsToRetry, result, failureCount);1410 }1411 /**1412 * message / regEx .* other1413 * null true false1414 * non-null true match1415 */1416 private boolean messageRegExpMatches(String messageRegExp, Throwable ite) {1417 if (".*".equals(messageRegExp)) {1418 return true;1419 } else {1420 if (ite.getMessage() == null) {1421 return false;1422 } else {1423 return Pattern.matches(messageRegExp, ite.getMessage());1424 }1425 }1426 }1427 private int removeResultsToRetryFromResult(List<ITestResult> resultsToRetry,1428 List<ITestResult> result, int failureCount) {1429 if (resultsToRetry != null) {1430 for (ITestResult res : resultsToRetry) {1431 result.remove(res);1432 failureCount--;1433 }1434 }1435 return failureCount;1436 }1437 /**1438 * To reduce thread contention and also to correctly handle thread-confinement1439 * this method invokes the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1440 */1441 private List<ITestResult> runWorkers(ITestNGMethod testMethod,1442 List<IWorker<ITestNGMethod>> workers,1443 int threadPoolSize,1444 ConfigurationGroupMethods groupMethods,1445 XmlSuite suite,1446 Map<String, String> parameters)1447 {1448 // Invoke @BeforeGroups on the original method (reduce thread contention,1449 // and also solve thread confinement)1450 ITestClass testClass= testMethod.getTestClass();1451 Object[] instances = testClass.getInstances(true);1452 for(Object instance: instances) {1453 invokeBeforeGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1454 }1455 long maxTimeOut= -1; // 10 seconds1456 for(IWorker<ITestNGMethod> tmw : workers) {1457 long mt= tmw.getTimeOut();1458 if(mt > maxTimeOut) {1459 maxTimeOut= mt;1460 }1461 }1462 ThreadUtil.execute(workers, threadPoolSize, maxTimeOut, true);1463 //1464 // Collect all the TestResults1465 //1466 List<ITestResult> result = Lists.newArrayList();1467 for (IWorker<ITestNGMethod> tmw : workers) {1468 if (tmw instanceof TestMethodWorker) {1469 result.addAll(((TestMethodWorker)tmw).getTestResults());1470 }1471 }1472 for(Object instance: instances) {1473 invokeAfterGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1474 }1475 return result;1476 }1477 /**1478 * Checks to see of the test method has certain dependencies that prevents1479 * TestNG from executing it1480 * @param testMethod test method being checked for1481 * @param testClass1482 * @return dependencies have been run successfully1483 */1484 private boolean checkDependencies(ITestNGMethod testMethod,1485 ITestNGMethod[] allTestMethods)1486 {1487 boolean result = true;1488 // If this method is marked alwaysRun, no need to check for its dependencies1489 if (testMethod.isAlwaysRun()) {1490 return true;1491 }1492 // Any missing group?1493 if (testMethod.getMissingGroup() != null1494 && !testMethod.ignoreMissingDependencies()) {1495 return false;1496 }1497 // If this method depends on groups, collect all the methods that1498 // belong to these groups and make sure they have been run successfully1499 if (dependsOnGroups(testMethod)) {1500 String[] groupsDependedUpon = testMethod.getGroupsDependedUpon();1501 // Get all the methods that belong to the group depended upon1502 for (String element : groupsDependedUpon) {1503 ITestNGMethod[] methods =1504 MethodGroupsHelper.findMethodsThatBelongToGroup(testMethod,1505 m_testContext.getAllTestMethods(),1506 element);1507 result = result && haveBeenRunSuccessfully(testMethod, methods);1508 }1509 } // depends on groups1510 // If this method depends on other methods, make sure all these other1511 // methods have been run successfully1512 if (result && dependsOnMethods(testMethod)) {1513 ITestNGMethod[] methods =1514 MethodHelper.findDependedUponMethods(testMethod, allTestMethods);1515 result = result && haveBeenRunSuccessfully(testMethod, methods);1516 }1517 return result;1518 }1519 /**1520 * @return the test results that apply to one of the instances of the testMethod.1521 */1522 private Set<ITestResult> keepSameInstances(ITestNGMethod method, Set<ITestResult> results) {1523 Set<ITestResult> result = Sets.newHashSet();1524 for (ITestResult r : results) {1525 for (Object o : method.getInstances()) {1526 // Keep this instance if 1) It's on a different class or 2) It's on the same class1527 // and on the same instance1528 Object instance = r.getInstance() != null1529 ? r.getInstance() : r.getMethod().getInstances()[0];1530 if (r.getTestClass() != method.getTestClass() || instance == o) result.add(r);1531 }1532 }1533 return result;1534 }1535 /**1536 * @return true if all the methods have been run successfully1537 */1538 private boolean haveBeenRunSuccessfully(ITestNGMethod testMethod, ITestNGMethod[] methods) {1539 // Make sure the method has been run successfully1540 for (ITestNGMethod method : methods) {1541 Set<ITestResult> results = keepSameInstances(testMethod, m_notifier.getPassedTests(method));1542 Set<ITestResult> failedAndSkippedMethods = Sets.newHashSet();1543 failedAndSkippedMethods.addAll(m_notifier.getFailedTests(method));1544 failedAndSkippedMethods.addAll(m_notifier.getSkippedTests(method));1545 Set<ITestResult> failedresults = keepSameInstances(testMethod, failedAndSkippedMethods);1546 // If failed results were returned on the same instance, then these tests didn't pass1547 if (failedresults != null && failedresults.size() > 0) {1548 return false;1549 }1550 for (ITestResult result : results) {1551 if(!result.isSuccess()) {1552 return false;1553 }1554 }1555 }1556 return true;1557 }1558// private boolean containsInstance(Set<ITestResult> failedresults, Object[] instances) {1559// for (ITestResult tr : failedresults) {1560// for (Object o : instances) {1561// if (o == tr.getInstance()) {1562// return true;1563// }1564// }1565// }1566// return false;1567// }1568 /**1569 * An exception was thrown by the test, determine if this method1570 * should be marked as a failure or as failure_but_within_successPercentage1571 */1572 private void handleException(Throwable throwable,1573 ITestNGMethod testMethod,1574 ITestResult testResult,1575 int failureCount) {1576 testResult.setThrowable(throwable);1577 int successPercentage= testMethod.getSuccessPercentage();1578 int invocationCount= testMethod.getInvocationCount();1579 float numberOfTestsThatCanFail= ((100 - successPercentage) * invocationCount) / 100f;1580 if(failureCount < numberOfTestsThatCanFail) {1581 testResult.setStatus(ITestResult.SUCCESS_PERCENTAGE_FAILURE);1582 }1583 else {1584 testResult.setStatus(ITestResult.FAILURE);1585 }1586 }1587 /**1588 * @param ite The exception that was just thrown1589 * @param expectedExceptions The list of expected exceptions for this1590 * test method1591 * @return true if the exception that was just thrown is part of the1592 * expected exceptions...
Source:BaseUnitTest.java
...210 when(iTestNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);211 when(iTestNGMethod.getMethodName()).thenReturn(methodName);212 when(iTestNGMethod.getRealClass()).thenReturn(clazz);213 when(iTestNGMethod.isTest()).thenReturn(true);214 when(iTestNGMethod.getInvocationCount()).thenReturn(1);215 return iTestNGMethod;216 }217 @SuppressWarnings("unchecked")218 protected static ITestClass getMockITestClass(Class aClass) {219 ITestClass iTestClass = mock(ITestClass.class);220 when(iTestClass.getRealClass()).thenReturn(aClass);221 return iTestClass;222 }223 protected static ITestResult getMockITestResult(Integer status) {224 Method method;225 try {226 method = TestNGTestClassWithSuite.class.getMethod("iTestResultMethodWithDetails");227 } catch (NoSuchMethodException e) {228 throw new RuntimeException(e);...
Source:ITestNGMethod.java
...160161 /**162 * @return the number of times this method needs to be invoked.163 */164 int getInvocationCount();165 void setInvocationCount(int count);166167 /**168 * @return the total number of thimes this method needs to be invoked, including possible169 * clones of this method - this is relevant when threadPoolSize is bigger than 1170 * where each clone of this method is only invoked once individually, i.e.171 * {@link org.testng.ITestNGMethod#getInvocationCount()} would always return 1.172 */173 int getTotalInvocationCount();174175 /**176 * @return the success percentage for this method (between 0 and 100).177 */178 int getSuccessPercentage();179180 /**181 * @return The id of the thread this method was run in.182 */183 String getId();184185 void setId(String id);
...
getInvocationCount
Using AI Code Generation
1import org.testng.ITestNGMethod;2import org.testng.ITestResult;3import org.testng.TestListenerAdapter;4public class TestNGListener extends TestListenerAdapter {5 public void onTestSuccess(ITestResult tr) {6 ITestNGMethod method = tr.getMethod();7 int invocationCount = method.getInvocationCount();8 System.out.println("Invocation Count: " + invocationCount);9 }10}11import org.testng.ITestNGMethod;12import org.testng.ITestResult;13import org.testng.TestListenerAdapter;14public class TestNGListener extends TestListenerAdapter {15 public void onTestSuccess(ITestResult tr) {16 ITestNGMethod method = tr.getMethod();17 int[] failedInvocationNumbers = method.getFailedInvocationNumbers();18 System.out.println("Failed Invocation Numbers: " + failedInvocationNumbers);19 }20}21import org.testng.ITestNGMethod;22import org.testng.ITestResult;23import org.testng.TestListenerAdapter;24public class TestNGListener extends TestListenerAdapter {25 public void onTestSuccess(ITestResult tr) {26 ITestNGMethod method = tr.getMethod();27 int[] successInvocationNumbers = method.getSuccessInvocationNumbers();28 System.out.println("Success Invocation Numbers: " + successInvocationNumbers);29 }30}31import org.testng.ITestNGMethod;32import org.testng.ITestResult;33import org.testng.TestListenerAdapter;34public class TestNGListener extends TestListenerAdapter {35 public void onTestSuccess(ITestResult tr) {36 ITestNGMethod method = tr.getMethod();37 int[] skippedInvocationNumbers = method.getSkippedInvocationNumbers();38 System.out.println("Skipped Invocation Numbers: " + skippedInvocationNumbers);39 }40}41import org.testng.ITestNGMethod;42import org.testng.ITestResult;43import org.testng.TestListenerAdapter;44public class TestNGListener extends TestListenerAdapter {45 public void onTestSuccess(ITestResult tr) {46 ITestNGMethod method = tr.getMethod();47 int[] invocationNumbers = method.getInvocationNumbers();
getInvocationCount
Using AI Code Generation
1package com.test;2import org.testng.ITestNGMethod;3import org.testng.annotations.Test;4public class TestNGTest {5 public void testMethod() {6 ITestNGMethod testMethod = new ITestNGMethod() {7 public int getInvocationCount() {8 return 10;9 }10 public void setInvocationCount(int invocationCount) {11 }12 public long[] getTimeOut() {13 return new long[0];14 }15 public void setTimeOut(long timeOut) {16 }17 public boolean isAlwaysRun() {18 return false;19 }20 public void setAlwaysRun(boolean alwaysRun) {21 }22 public long getThreadPoolSize() {23 return 0;24 }25 public void setThreadPoolSize(long threadPoolSize) {26 }27 public boolean isSequential() {28 return false;29 }30 public void setSequential(boolean sequential) {31 }32 public String getTestName() {33 return null;34 }35 public void setTestName(String testName) {36 }37 public String getXmlTestName() {38 return null;39 }40 public void setXmlTestName(String xmlTestName) {41 }42 public String getSuiteName() {43 return null;44 }45 public void setSuiteName(String suiteName) {46 }47 public String getMethodName() {48 return null;49 }50 public String getDescription() {51 return null;52 }53 public void setDescription(String description) {54 }55 public String getTestClassname() {56 return null;57 }58 public void setTestClassname(String testClassname) {59 }60 public String[] getGroups() {61 return new String[0];62 }63 public void setGroups(String[] groups) {64 }65 public String[] getGroupsDependedUpon() {66 return new String[0];67 }68 public void setGroupsDependedUpon(String[] groupsDependedUpon) {
getInvocationCount
Using AI Code Generation
1package com.test;2import org.testng.ITestNGMethod;3import org.testng.annotations.Test;4public class TestNGInvocationCount {5@Test(invocationCount = 5, invocationTimeOut = 1000)6public void testMethod() {7System.out.println("TestNGInvocationCount.testMethod()");8}9public static void main(String[] args) {10ITestNGMethod method = new TestNGInvocationCount().getClass().getMethods()[0];11System.out.println(method.getInvocationCount());12}13}
getInvocationCount
Using AI Code Generation
1package org.testng;2public interface ITestNGMethod extends ITestOrConfiguration {3 public int getInvocationCount();4}5package org.testng;6public interface ITestOrConfiguration extends ITestNGMethod {7 public int getInvocationCount();8}9package org.testng;10public interface ITestNGMethod extends ITestOrConfiguration {11 public int getInvocationCount();12}13package org.testng;14public interface ITestOrConfiguration extends ITestNGMethod {15 public int getInvocationCount();16}17package org.testng;18public interface ITestNGMethod extends ITestOrConfiguration {19 public int getInvocationCount();20}21package org.testng;22public interface ITestOrConfiguration extends ITestNGMethod {23 public int getInvocationCount();24}25package org.testng;26public interface ITestNGMethod extends ITestOrConfiguration {27 public int getInvocationCount();28}29package org.testng;30public interface ITestOrConfiguration extends ITestNGMethod {31 public int getInvocationCount();32}33package org.testng;34public interface ITestNGMethod extends ITestOrConfiguration {35 public int getInvocationCount();36}37package org.testng;38public interface ITestOrConfiguration extends ITestNGMethod {39 public int getInvocationCount();40}41package org.testng;42public interface ITestNGMethod extends ITestOrConfiguration {43 public int getInvocationCount();44}45package org.testng;46public interface ITestOrConfiguration extends ITestNGMethod {47 public int getInvocationCount();48}49package org.testng;50public interface ITestNGMethod extends ITestOrConfiguration {51 public int getInvocationCount();52}53package org.testng;54public interface ITestOrConfiguration extends ITestNGMethod {55 public int getInvocationCount();56}57package org.testng;58public interface ITestNGMethod extends ITestOrConfiguration {59 public int getInvocationCount();60}61package org.testng;62public interface ITestOrConfiguration extends ITestNGMethod {63 public int getInvocationCount();64}65package org.testng;66public interface ITestNGMethod extends ITestOrConfiguration {67 public int getInvocationCount();68}69package org.testng;70public interface ITestOrConfiguration extends ITestNGMethod {71 public int getInvocationCount();72}73package org.testng;74public interface ITestNGMethod extends ITestOrConfiguration {75 public int getInvocationCount();76}77package org.testng;78public interface ITestOrConfiguration extends ITestNGMethod {79 public int getInvocationCount();80}81package org.testng;82public interface ITestNGMethod extends ITestOrConfiguration {
getInvocationCount
Using AI Code Generation
1public class TestNG_GetInvocationCount {2 @Test(invocationCount = 3)3 public void test() {4 System.out.println("test");5 }6}
getInvocationCount
Using AI Code Generation
1public void testMethod() {2 ITestNGMethod[] methods = testNg.getTest().getAllTestMethods();3 for (ITestNGMethod method : methods) {4 System.out.println(method.getMethodName() + " has been invoked " + method.getInvocationCount() + " times");5 }6}
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!!