How to use setStatus method of org.testng.Interface ITestResult class

Best Testng code snippet using org.testng.Interface ITestResult.setStatus

Source:Invoker.java Github

copy

Full Screen

...148 testClass = tm.getTestClass();149 }150 long time = System.currentTimeMillis();151 ITestResult testResult = new TestResult(testClass, tm, null, time, 0L, m_testContext);152 testResult.setStatus(ITestResult.STARTED);153 IConfigurationAnnotation configurationAnnotation = null;154 try {155 Object inst = tm.getInstance();156 if (inst == null) {157 inst = instance;158 }159 Class<?> objectClass = inst.getClass();160 ConstructorOrMethod method = tm.getConstructorOrMethod();161 // Only run the configuration if162 // - the test is enabled and163 // - the Configuration method belongs to the same class or a parent164 configurationAnnotation = AnnotationHelper.findConfiguration(m_annotationFinder, method);165 boolean alwaysRun = MethodHelper.isAlwaysRun(configurationAnnotation);166 boolean canProcessMethod =167 MethodHelper.isEnabled(objectClass, m_annotationFinder) || alwaysRun;168 if (!canProcessMethod) {169 log(170 3,171 "Skipping "172 + Utils.detailedMethodName(tm, true)173 + " because "174 + objectClass.getName()175 + " is not enabled");176 continue;177 }178 if (MethodHelper.isDisabled(configurationAnnotation)) {179 log(3, "Skipping " + Utils.detailedMethodName(tm, true) + " because it is not enabled");180 continue;181 }182 if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {183 log(3, "Skipping " + Utils.detailedMethodName(tm, true));184 InvokedMethod invokedMethod =185 new InvokedMethod(instance, tm, System.currentTimeMillis(), testResult);186 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);187 testResult.setStatus(ITestResult.SKIP);188 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);189 handleConfigurationSkip(190 tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);191 continue;192 }193 log(3, "Invoking " + Utils.detailedMethodName(tm, true));194 if (testMethodResult != null) {195 ((TestResult) testMethodResult).setMethod(currentTestMethod);196 }197 parameters =198 Parameters.createConfigurationParameters(199 tm.getConstructorOrMethod().getMethod(),200 params,201 parameterValues,202 currentTestMethod,203 m_annotationFinder,204 suite,205 m_testContext,206 testMethodResult);207 testResult.setParameters(parameters);208 runConfigurationListeners(testResult, true /* before */);209 Object newInstance = computeInstance(instance, inst, tm);210 if (isConfigMethodEligibleForScrutiny(tm)) {211 if (m_executedConfigMethods.add(currentTestMethod)) {212 invokeConfigurationMethod(newInstance, tm, parameters, testResult);213 }214 } else {215 invokeConfigurationMethod(newInstance, tm, parameters, testResult);216 }217 copyAttributesFromNativelyInjectedTestResult(parameters, testMethodResult);218 runConfigurationListeners(testResult, false /* after */);219 } catch (Throwable ex) {220 handleConfigurationFailure(221 ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);222 copyAttributesFromNativelyInjectedTestResult(parameters, testMethodResult);223 }224 } // for methods225 }226 private static void copyAttributesFromNativelyInjectedTestResult(227 Object[] source, ITestResult target) {228 if (source == null || target == null) {229 return;230 }231 Arrays.stream(source)232 .filter(each -> each instanceof ITestResult)233 .findFirst()234 .ifPresent(eachSource -> copyAttributes((ITestResult) eachSource, target));235 }236 private static void copyAttributes(ITestResult source, ITestResult target) {237 source238 .getAttributeNames()239 .forEach(name -> target.setAttribute(name, source.getAttribute(name)));240 }241 private static Object computeInstance(Object instance, Object inst, ITestNGMethod tm) {242 if (instance == null243 || !tm.getConstructorOrMethod().getDeclaringClass().isAssignableFrom(instance.getClass())) {244 return inst;245 }246 return instance;247 }248 /** Marks the current <code>TestResult</code> as skipped and invokes the listeners. */249 private void handleConfigurationSkip(250 ITestNGMethod tm,251 ITestResult testResult,252 IConfigurationAnnotation annotation,253 ITestNGMethod currentTestMethod,254 Object instance,255 XmlSuite suite) {256 recordConfigurationInvocationFailed(257 tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);258 testResult.setStatus(ITestResult.SKIP);259 runConfigurationListeners(testResult, false /* after */);260 }261 private void handleConfigurationFailure(262 Throwable ite,263 ITestNGMethod tm,264 ITestResult testResult,265 IConfigurationAnnotation annotation,266 ITestNGMethod currentTestMethod,267 Object instance,268 XmlSuite suite) {269 Throwable cause = ite.getCause() != null ? ite.getCause() : ite;270 if (isSkipExceptionAndSkip(cause)) {271 testResult.setThrowable(cause);272 handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);273 return;274 }275 Utils.log(276 "",277 3,278 "Failed to invoke configuration method "279 + tm.getQualifiedName()280 + ":"281 + cause.getMessage());282 handleException(cause, tm, testResult, 1);283 testResult.setStatus(ITestResult.FAILURE);284 runConfigurationListeners(testResult, false /* after */);285 //286 // If in TestNG mode, need to take a look at the annotation to figure out287 // what kind of @Configuration method we're dealing with288 //289 if (null != annotation) {290 recordConfigurationInvocationFailed(291 tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);292 }293 }294 /**295 * Record internally the failure of a Configuration, so that we can determine later if @Test296 * should be skipped.297 */298 private void recordConfigurationInvocationFailed(299 ITestNGMethod tm,300 IClass testClass,301 IConfigurationAnnotation annotation,302 ITestNGMethod currentTestMethod,303 Object instance,304 XmlSuite suite) {305 // If beforeTestClass or afterTestClass failed, mark either the config method's306 // entire class as failed, or the class under tests as failed, depending on307 // the configuration failure policy308 if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {309 // tm is the configuration method, and currentTestMethod is null for BeforeClass310 // methods, so we need testClass311 if (m_continueOnFailedConfiguration) {312 setClassInvocationFailure(testClass.getRealClass(), instance);313 } else {314 setClassInvocationFailure(tm.getRealClass(), instance);315 }316 }317 // If before/afterTestMethod failed, mark either the config method's entire318 // class as failed, or just the current test method as failed, depending on319 // the configuration failure policy320 else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {321 if (m_continueOnFailedConfiguration) {322 setMethodInvocationFailure(currentTestMethod, instance);323 } else {324 setClassInvocationFailure(tm.getRealClass(), instance);325 }326 }327 // If beforeSuite or afterSuite failed, mark *all* the classes as failed328 // for configurations. At this point, the entire Suite is screwed329 else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {330 m_suiteState.failed();331 }332 // beforeTest or afterTest: mark all the classes in the same333 // <test> stanza as failed for configuration334 else if (annotation.getBeforeTest() || annotation.getAfterTest()) {335 setClassInvocationFailure(tm.getRealClass(), instance);336 XmlClass[] classes = ClassHelper.findClassesInSameTest(tm.getRealClass(), suite);337 for (XmlClass xmlClass : classes) {338 setClassInvocationFailure(xmlClass.getSupportClass(), instance);339 }340 }341 String[] beforeGroups = annotation.getBeforeGroups();342 for (String group : beforeGroups) {343 m_beforegroupsFailures.put(group, Boolean.FALSE);344 }345 }346 /** @return true if this class or a parent class failed to initialize. */347 private boolean classConfigurationFailed(Class<?> cls, Object instance) {348 return m_classInvocationResults349 .entrySet()350 .stream()351 .anyMatch(352 classSetEntry -> {353 Set<Object> obj = classSetEntry.getValue();354 Class<?> c = classSetEntry.getKey();355 boolean containsBeforeTestOrBeforeSuiteFailure = obj.contains(null);356 return c == cls357 || c.isAssignableFrom(cls)358 && (obj.contains(instance) || containsBeforeTestOrBeforeSuiteFailure);359 });360 }361 /**362 * @return true if this class has successfully run all its @Configuration method or false if at363 * least one of these methods failed.364 */365 private boolean confInvocationPassed(366 ITestNGMethod method, ITestNGMethod currentTestMethod, IClass testClass, Object instance) {367 boolean result = true;368 Class<?> cls = testClass.getRealClass();369 if (m_suiteState.isFailed()) {370 result = false;371 } else {372 boolean hasConfigurationFailures = classConfigurationFailed(cls, instance);373 if (hasConfigurationFailures) {374 if (!m_continueOnFailedConfiguration) {375 result = false;376 } else {377 Set<Object> set = getInvocationResults(testClass);378 result = !set.contains(instance);379 }380 return result;381 }382 // if method is BeforeClass, currentTestMethod will be null383 if (m_continueOnFailedConfiguration && hasConfigFailure(currentTestMethod)) {384 Object key = TestNgMethodUtils.getMethodInvocationToken(currentTestMethod, instance);385 result = !m_methodInvocationResults.get(currentTestMethod).contains(key);386 } else if (!m_continueOnFailedConfiguration) {387 for (Class<?> clazz : m_classInvocationResults.keySet()) {388 if (clazz.isAssignableFrom(cls)389 && m_classInvocationResults.get(clazz).contains(instance)) {390 result = false;391 break;392 }393 }394 }395 }396 // check if there are failed @BeforeGroups397 String[] groups = method.getGroups();398 for (String group : groups) {399 if (m_beforegroupsFailures.containsKey(group)) {400 result = false;401 break;402 }403 }404 return result;405 }406 private boolean hasConfigFailure(ITestNGMethod currentTestMethod) {407 return currentTestMethod != null && m_methodInvocationResults.containsKey(currentTestMethod);408 }409 private Set<Object> getInvocationResults(IClass testClass) {410 Class<?> cls = testClass.getRealClass();411 Set<Object> set = null;412 // We need to continuously search till either our Set is not null (or) till we reached413 // Object class because it is very much possible that the test method is residing in a child414 // class415 // and maybe the parent method has configuration methods which may have had a failure416 // So lets walk up the inheritance tree until either we find failures or till we417 // reached the Object class.418 while (!cls.equals(Object.class)) {419 set = m_classInvocationResults.get(cls);420 if (set != null) {421 break;422 }423 cls = cls.getSuperclass();424 }425 if (set == null) {426 // This should never happen because we have walked up all the way till Object class427 // and yet found no failures, but our logic indicates that there was a failure somewhere up428 // the429 // inheritance order. We don't know what to do at this point.430 throw new IllegalStateException("No failure logs for " + testClass.getRealClass());431 }432 return set;433 }434 private static boolean isConfigMethodEligibleForScrutiny(ITestNGMethod tm) {435 if (!tm.isBeforeMethodConfiguration()) {436 return false;437 }438 if (!(tm instanceof ConfigurationMethod)) {439 return false;440 }441 ConfigurationMethod cfg = (ConfigurationMethod) tm;442 return cfg.isFirstTimeOnly();443 }444 /** Effectively invokes a configuration method on all passed in instances. */445 // TODO: Change this method to be more like invokeMethod() so that we can handle calls to {@code446 // IInvokedMethodListener} better.447 private void invokeConfigurationMethod(448 Object targetInstance, ITestNGMethod tm, Object[] params, ITestResult testResult)449 throws InvocationTargetException, IllegalAccessException {450 // Mark this method with the current thread id451 tm.setId(ThreadUtil.currentThreadInfo());452 InvokedMethod invokedMethod =453 new InvokedMethod(targetInstance, tm, System.currentTimeMillis(), testResult);454 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);455 m_notifier.addInvokedMethod(invokedMethod);456 try {457 Reporter.setCurrentTestResult(testResult);458 ConstructorOrMethod method = tm.getConstructorOrMethod();459 IConfigurable configurableInstance = computeConfigurableInstance(method, targetInstance);460 if (RuntimeBehavior.isDryRun()) {461 testResult.setStatus(ITestResult.SUCCESS);462 return;463 }464 if (configurableInstance != null) {465 MethodInvocationHelper.invokeConfigurable(466 targetInstance, params, configurableInstance, method.getMethod(), testResult);467 } else {468 MethodInvocationHelper.invokeMethodConsideringTimeout(469 tm, method, targetInstance, params, testResult);470 }471 testResult.setStatus(ITestResult.SUCCESS);472 } catch (InvocationTargetException | IllegalAccessException ex) {473 throwConfigurationFailure(testResult, ex);474 testResult.setStatus(ITestResult.FAILURE);475 throw ex;476 } catch (Throwable ex) {477 throwConfigurationFailure(testResult, ex);478 testResult.setStatus(ITestResult.FAILURE);479 throw new TestNGException(ex);480 } finally {481 testResult.setEndMillis(System.currentTimeMillis());482 Reporter.setCurrentTestResult(testResult);483 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484 Reporter.setCurrentTestResult(null);485 }486 }487 private IConfigurable computeConfigurableInstance(488 ConstructorOrMethod method, Object targetInstance) {489 return IConfigurable.class.isAssignableFrom(method.getDeclaringClass())490 ? (IConfigurable) targetInstance491 : m_configuration.getConfigurable();492 }493 private void throwConfigurationFailure(ITestResult testResult, Throwable ex) {494 testResult.setStatus(ITestResult.FAILURE);495 testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());496 }497 private void runInvokedMethodListeners(498 InvokedMethodListenerMethod listenerMethod,499 IInvokedMethod invokedMethod,500 ITestResult testResult) {501 if (noListenersPresent()) {502 return;503 }504 InvokedMethodListenerInvoker invoker =505 new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);506 for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {507 invoker.invokeListener(currentListener, invokedMethod);508 }509 }510 private boolean noListenersPresent() {511 return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.isEmpty());512 }513 // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.514 private ITestResult invokeMethod(515 Object instance,516 ITestNGMethod tm,517 Object[] parameterValues,518 int parametersIndex,519 XmlSuite suite,520 Map<String, String> params,521 ITestClass testClass,522 ITestNGMethod[] beforeMethods,523 ITestNGMethod[] afterMethods,524 ConfigurationGroupMethods groupMethods,525 FailureContext failureContext) {526 TestResult testResult = new TestResult();527 invokeBeforeGroupsConfigurations(tm, groupMethods, suite, params, instance);528 ITestNGMethod[] setupConfigMethods =529 TestNgMethodUtils.filterSetupConfigurationMethods(tm, beforeMethods);530 invokeConfigurations(531 testClass, tm, setupConfigMethods, suite, params, parameterValues, instance, testResult);532 InvokedMethod invokedMethod =533 new InvokedMethod(instance, tm, System.currentTimeMillis(), testResult);534 if (!confInvocationPassed(tm, tm, testClass, instance)) {535 Throwable exception = ExceptionUtils.getExceptionDetails(m_testContext, instance);536 ITestResult result = registerSkippedTestResult(tm, System.currentTimeMillis(), exception);537 copyAttributes(testResult, result);538 m_notifier.addSkippedTest(tm, result);539 tm.incrementCurrentInvocationCount();540 testResult.setMethod(tm);541 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, result);542 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, result);543 ITestNGMethod[] teardownConfigMethods =544 TestNgMethodUtils.filterTeardownConfigurationMethods(tm, afterMethods);545 invokeConfigurations(546 testClass,547 tm,548 teardownConfigMethods,549 suite,550 params,551 parameterValues,552 instance,553 testResult);554 invokeAfterGroupsConfigurations(tm, groupMethods, suite, params, instance);555 return result;556 }557 //558 // Create the ExtraOutput for this method559 //560 try {561 testResult.init(testClass, tm, null, System.currentTimeMillis(), 0, m_testContext);562 testResult.setParameters(parameterValues);563 testResult.setParameterIndex(parametersIndex);564 testResult.setHost(m_testContext.getHost());565 testResult.setStatus(ITestResult.STARTED);566 Reporter.setCurrentTestResult(testResult);567 // Fix from ansgarkonermann568 // invokedMethod is used in the finally, which can be invoked if569 // any of the test listeners throws an exception, therefore,570 // invokedMethod must have a value before we get here571 if (!m_suiteState.isFailed()) {572 runTestListeners(testResult);573 }574 log(3, "Invoking " + tm.getQualifiedName());575 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);576 m_notifier.addInvokedMethod(invokedMethod);577 Method thisMethod = tm.getConstructorOrMethod().getMethod();578 if (RuntimeBehavior.isDryRun()) {579 setTestStatus(testResult, ITestResult.SUCCESS);580 return testResult;581 }582 // If this method is a IHookable, invoke its run() method583 IHookable hookableInstance =584 IHookable.class.isAssignableFrom(tm.getRealClass())585 ? (IHookable) instance586 : m_configuration.getHookable();587 if (MethodHelper.calculateTimeOut(tm) <= 0) {588 if (hookableInstance != null) {589 MethodInvocationHelper.invokeHookable(590 instance, parameterValues, hookableInstance, thisMethod, testResult);591 } else {592 // Not a IHookable, invoke directly593 MethodInvocationHelper.invokeMethod(thisMethod, instance, parameterValues);594 }595 setTestStatus(testResult, ITestResult.SUCCESS);596 } else {597 // Method with a timeout598 MethodInvocationHelper.invokeWithTimeout(599 tm, instance, parameterValues, testResult, hookableInstance);600 }601 } catch (InvocationTargetException ite) {602 testResult.setThrowable(ite.getCause());603 setTestStatus(testResult, ITestResult.FAILURE);604 } catch (ThreadExecutionException tee) { // wrapper for TestNGRuntimeException605 Throwable cause = tee.getCause();606 if (TestNGRuntimeException.class.equals(cause.getClass())) {607 testResult.setThrowable(cause.getCause());608 } else {609 testResult.setThrowable(cause);610 }611 setTestStatus(testResult, ITestResult.FAILURE);612 } catch (Throwable thr) { // covers the non-wrapper exceptions613 testResult.setThrowable(thr);614 setTestStatus(testResult, ITestResult.FAILURE);615 } finally {616 // Set end time ASAP617 testResult.setEndMillis(System.currentTimeMillis());618 ExpectedExceptionsHolder expectedExceptionClasses =619 new ExpectedExceptionsHolder(620 m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));621 StatusHolder holder =622 considerExceptions(tm, testResult, expectedExceptionClasses, failureContext);623 int statusBeforeListenerInvocation = testResult.getStatus();624 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);625 boolean wasResultUnaltered = statusBeforeListenerInvocation == testResult.getStatus();626 handleInvocationResults(tm, testResult, failureContext, holder, wasResultUnaltered);627 // If this method has a data provider and just failed, memorize the number628 // at which it failed.629 // Note: we're not exactly testing that this method has a data provider, just630 // that it has parameters, so might have to revisit this if bugs get reported631 // for the case where this method has parameters that don't come from a data632 // provider633 if (testResult.getThrowable() != null && parameterValues.length > 0) {634 tm.addFailedInvocationNumber(parametersIndex);635 }636 //637 // Increment the invocation count for this method638 //639 tm.incrementCurrentInvocationCount();640 runTestListeners(testResult);641 collectResults(tm, testResult);642 ITestNGMethod[] tearDownConfigMethods =643 TestNgMethodUtils.filterTeardownConfigurationMethods(tm, afterMethods);644 invokeConfigurations(645 testClass,646 tm,647 tearDownConfigMethods,648 suite,649 params,650 parameterValues,651 instance,652 testResult);653 //654 // Invoke afterGroups configurations655 //656 invokeAfterGroupsConfigurations(tm, groupMethods, suite, params, instance);657 // Reset the test result last. If we do this too early, Reporter.log()658 // invocations from listeners will be discarded659 Reporter.setCurrentTestResult(null);660 }661 return testResult;662 }663 private static void setTestStatus(ITestResult result, int status) {664 // set the test to success as long as the testResult hasn't been changed by the user via665 // Reporter.getCurrentTestResult666 if (result.getStatus() == ITestResult.STARTED) {667 result.setStatus(status);668 }669 }670 private void collectResults(ITestNGMethod testMethod, ITestResult result) {671 // Collect the results672 int status = result.getStatus();673 if (ITestResult.SUCCESS == status) {674 m_notifier.addPassedTest(testMethod, result);675 } else if (ITestResult.SKIP == status) {676 m_notifier.addSkippedTest(testMethod, result);677 } else if (ITestResult.FAILURE == status) {678 m_notifier.addFailedTest(testMethod, result);679 } else if (ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {680 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);681 } else {682 assert false : "UNKNOWN STATUS:" + status;683 }684 }685 /**686 * invokeTestMethods() eventually converge here to invoke a single @Test method.687 *688 * <p>This method is responsible for actually invoking the method. It decides if the invocation689 * must be done:690 *691 * <ul>692 * <li>through an <code>IHookable</code>693 * <li>directly (through reflection)694 * <li>in a separate thread (in case it needs to timeout)695 * </ul>696 *697 * <p>This method is also responsible for698 * invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup if it is the case for the699 * passed in @Test method.700 */701 ITestResult invokeTestMethod(702 Object instance,703 ITestNGMethod tm,704 Object[] parameterValues,705 int parametersIndex,706 XmlSuite suite,707 Map<String, String> params,708 ITestClass testClass,709 ITestNGMethod[] beforeMethods,710 ITestNGMethod[] afterMethods,711 ConfigurationGroupMethods groupMethods,712 FailureContext failureContext) {713 // Mark this method with the current thread id714 tm.setId(ThreadUtil.currentThreadInfo());715 return invokeMethod(716 instance,717 tm,718 parameterValues,719 parametersIndex,720 suite,721 params,722 testClass,723 beforeMethods,724 afterMethods,725 groupMethods,726 failureContext);727 }728 /**729 * Filter all the beforeGroups methods and invoke only those that apply to the current test method730 */731 private void invokeBeforeGroupsConfigurations(732 ITestNGMethod currentTestMethod,733 ConfigurationGroupMethods groupMethods,734 XmlSuite suite,735 Map<String, String> params,736 Object instance) {737 List<ITestNGMethod> filteredMethods = Lists.newArrayList();738 String[] groups = currentTestMethod.getGroups();739 for (String group : groups) {740 List<ITestNGMethod> methods = groupMethods.getBeforeGroupMethodsForGroup(group);741 if (methods != null) {742 filteredMethods.addAll(methods);743 }744 }745 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[0]);746 //747 // Invoke the right groups methods748 //749 if (beforeMethodsArray.length > 0) {750 // don't pass the IClass or the instance as the method may be external751 // the invocation must be similar to @BeforeTest/@BeforeSuite752 invokeConfigurations(753 null, beforeMethodsArray, suite, params, /* no parameter values */ null, instance);754 }755 //756 // Remove them so they don't get run again757 //758 groupMethods.removeBeforeGroups(groups);759 }760 private void invokeAfterGroupsConfigurations(761 ITestNGMethod currentTestMethod,762 ConfigurationGroupMethods groupMethods,763 XmlSuite suite,764 Map<String, String> params,765 Object instance) {766 // Skip this if the current method doesn't belong to any group767 // (only a method that belongs to a group can trigger the invocation768 // of afterGroups methods)769 if (currentTestMethod.getGroups().length == 0) {770 return;771 }772 // See if the currentMethod is the last method in any of the groups773 // it belongs to774 Map<String, String> filteredGroups = Maps.newHashMap();775 String[] groups = currentTestMethod.getGroups();776 for (String group : groups) {777 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {778 filteredGroups.put(group, group);779 }780 }781 if (filteredGroups.isEmpty()) {782 return;783 }784 // The list of afterMethods to run785 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();786 // Now filteredGroups contains all the groups for which we need to run the afterGroups787 // method. Find all the methods that correspond to these groups and invoke them.788 for (String g : filteredGroups.values()) {789 List<ITestNGMethod> methods = groupMethods.getAfterGroupMethodsForGroup(g);790 // Note: should put them in a map if we want to make sure the same afterGroups791 // doesn't get run twice792 if (methods != null) {793 for (ITestNGMethod m : methods) {794 afterMethods.put(m, m);795 }796 }797 }798 // Got our afterMethods, invoke them799 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[0]);800 // don't pass the IClass or the instance as the method may be external801 // the invocation must be similar to @BeforeTest/@BeforeSuite802 invokeConfigurations(803 null, afterMethodsArray, suite, params, /* no parameter values */ null, instance);804 // Remove the groups so they don't get run again805 groupMethods.removeAfterGroups(filteredGroups.keySet());806 }807 FailureContext retryFailed(808 Object instance,809 ITestNGMethod tm,810 Object[] paramValues,811 ITestClass testClass,812 ITestNGMethod[] beforeMethods,813 ITestNGMethod[] afterMethods,814 ConfigurationGroupMethods groupMethods,815 List<ITestResult> result,816 int failureCount,817 ITestContext testContext,818 Map<String, String> parameters,819 int parametersIndex) {820 FailureContext failure = new FailureContext();821 failure.count = failureCount;822 do {823 failure.instances = Lists.newArrayList();824 Map<String, String> allParameters = Maps.newHashMap();825 // TODO: This recreates all the parameters every time when we only need826 // one specific set. Should optimize it by only recreating the set needed.827 ParameterHandler handler = new ParameterHandler(m_annotationFinder, m_dataproviderListeners);828 ParameterBag bag = handler.createParameters(tm, parameters, allParameters, testContext);829 Object[] parameterValues =830 Parameters.getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);831 if (bag.parameterHolder.origin == ParameterHolder.ParameterOrigin.NATIVE) {832 parameterValues = paramValues;833 }834 result.add(835 invokeMethod(836 instance,837 tm,838 parameterValues,839 parametersIndex,840 testContext.getSuite().getXmlSuite(),841 allParameters,842 testClass,843 beforeMethods,844 afterMethods,845 groupMethods,846 failure));847 } while (!failure.instances.isEmpty());848 return failure;849 }850 /**851 * Invoke all the test methods. Note the plural: the method passed in parameter might be invoked852 * several times if the test class it belongs to has more than one instance (i.e., if an @Factory853 * method has been declared somewhere that returns several instances of this TestClass). If854 * no @Factory method was specified, testMethod will only be invoked once.855 *856 * <p>Note that this method also takes care of invoking the beforeTestMethod and afterTestMethod,857 * if any.858 *859 * <p>Note (alex): this method can be refactored to use a SingleTestMethodWorker that directly860 * invokes {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map,861 * ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)} and862 * this would simplify the implementation (see how DataTestMethodWorker is used)863 */864 @Override865 public List<ITestResult> invokeTestMethods(866 ITestNGMethod testMethod,867 Map<String, String> testParameters,868 ConfigurationGroupMethods groupMethods,869 Object instance,870 ITestContext testContext) {871 // Potential bug here if the test method was declared on a parent class872 if (testMethod.getTestClass() == null) {873 throw new IllegalArgumentException(874 "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass());875 }876 XmlSuite suite = testContext.getSuite().getXmlSuite();877 if (!MethodHelper.isEnabled(878 testMethod.getConstructorOrMethod().getMethod(), m_annotationFinder)) {879 // return if the method is not enabled. No need to do any more calculations880 return Collections.emptyList();881 }882 // By the time this testMethod to be invoked,883 // all dependencies should be already run or we need to skip this method,884 // so invocation count should not affect dependencies check885 String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());886 if (okToProceed != null) {887 //888 // Not okToProceed. Test is being skipped889 //890 ITestResult result =891 registerSkippedTestResult(892 testMethod, System.currentTimeMillis(), new Throwable(okToProceed));893 m_notifier.addSkippedTest(testMethod, result);894 return Collections.singletonList(result);895 }896 Map<String, String> parameters =897 testMethod.findMethodParameters(testContext.getCurrentXmlTest());898 // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.899 if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {900 return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);901 }902 long timeOutInvocationCount = testMethod.getInvocationTimeOut();903 // FIXME: Is this correct?904 boolean onlyOne = testMethod.getThreadPoolSize() > 1 || timeOutInvocationCount > 0;905 int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();906 ITestClass testClass = testMethod.getTestClass();907 List<ITestResult> result = Lists.newArrayList();908 FailureContext failure = new FailureContext();909 ITestNGMethod[] beforeMethods =910 TestNgMethodUtils.filterBeforeTestMethods(testClass, CAN_RUN_FROM_CLASS);911 ITestNGMethod[] afterMethods =912 TestNgMethodUtils.filterAfterTestMethods(testClass, CAN_RUN_FROM_CLASS);913 while (invocationCount-- > 0) {914 long start = System.currentTimeMillis();915 Map<String, String> allParameterNames = Maps.newHashMap();916 ParameterHandler handler = new ParameterHandler(m_annotationFinder, m_dataproviderListeners);917 ParameterBag bag =918 handler.createParameters(919 testMethod, parameters, allParameterNames, testContext, instance);920 if (bag.hasErrors()) {921 ITestResult tr = bag.errorResult;922 Throwable throwable = tr.getThrowable();923 if (throwable instanceof TestNGException) {924 tr.setStatus(ITestResult.FAILURE);925 m_notifier.addFailedTest(testMethod, tr);926 } else {927 tr.setStatus(ITestResult.SKIP);928 m_notifier.addSkippedTest(testMethod, tr);929 }930 runTestListeners(tr);931 result.add(tr);932 continue;933 }934 Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;935 int parametersIndex = 0;936 try {937 if (bag.runInParallel()) {938 List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();939 while (allParameterValues.hasNext()) {940 Object[] next = allParameterValues.next();941 if (next == null) {942 // skipped value943 parametersIndex++;944 continue;945 }946 Object[] parameterValues =947 Parameters.injectParameters(948 next, testMethod.getConstructorOrMethod().getMethod(), testContext);949 TestMethodWithDataProviderMethodWorker w =950 new TestMethodWithDataProviderMethodWorker(951 this,952 testMethod,953 parametersIndex,954 parameterValues,955 instance,956 parameters,957 testClass,958 beforeMethods,959 afterMethods,960 groupMethods,961 testContext,962 m_skipFailedInvocationCounts,963 invocationCount,964 failure.count,965 m_notifier);966 workers.add(w);967 // testng387: increment the param index in the bag.968 parametersIndex++;969 }970 PoolService<List<ITestResult>> ps = new PoolService<>(suite.getDataProviderThreadCount());971 List<List<ITestResult>> r = ps.submitTasksAndWait(workers);972 for (List<ITestResult> l2 : r) {973 result.addAll(l2);974 }975 } else {976 while (allParameterValues.hasNext()) {977 Object[] next = allParameterValues.next();978 if (next == null) {979 // skipped value980 parametersIndex++;981 continue;982 }983 Object[] parameterValues =984 Parameters.injectParameters(985 next, testMethod.getConstructorOrMethod().getMethod(), testContext);986 List<ITestResult> tmpResults = Lists.newArrayList();987 int tmpResultsIndex = -1;988 try {989 tmpResults.add(990 invokeTestMethod(991 instance,992 testMethod,993 parameterValues,994 parametersIndex,995 suite,996 parameters,997 testClass,998 beforeMethods,999 afterMethods,1000 groupMethods,1001 failure));1002 tmpResultsIndex++;1003 } finally {1004 boolean lastSucces = false;1005 if (tmpResultsIndex >= 0) {1006 lastSucces = (tmpResults.get(tmpResultsIndex).getStatus() == ITestResult.SUCCESS);1007 }1008 if (failure.instances.isEmpty() || lastSucces) {1009 result.addAll(tmpResults);1010 } else {1011 List<ITestResult> retryResults = Lists.newArrayList();1012 failure =1013 retryFailed(1014 instance,1015 testMethod,1016 parameterValues,1017 testClass,1018 beforeMethods,1019 afterMethods,1020 groupMethods,1021 retryResults,1022 failure.count,1023 testContext,1024 parameters,1025 parametersIndex);1026 result.addAll(retryResults);1027 }1028 // If we have a failure, skip all the1029 // other invocationCounts1030 if (failure.count > 01031 && (m_skipFailedInvocationCounts || testMethod.skipFailedInvocations())) {1032 while (invocationCount-- > 0) {1033 result.add(1034 registerSkippedTestResult(testMethod, System.currentTimeMillis(), null));1035 }1036 }1037 } // end finally1038 parametersIndex++;1039 }1040 }1041 } catch (Throwable cause) {1042 ITestResult r =1043 new TestResult(1044 testMethod.getTestClass(),1045 testMethod,1046 cause,1047 start,1048 System.currentTimeMillis(),1049 m_testContext);1050 r.setStatus(TestResult.FAILURE);1051 result.add(r);1052 runTestListeners(r);1053 m_notifier.addFailedTest(testMethod, r);1054 } // catch1055 }1056 return result;1057 }1058 private ITestResult registerSkippedTestResult(1059 ITestNGMethod testMethod, long start, Throwable throwable) {1060 ITestResult result =1061 new TestResult(1062 testMethod.getTestClass(),1063 testMethod,1064 throwable,1065 start,1066 System.currentTimeMillis(),1067 m_testContext);1068 if (RuntimeBehavior.invokeListenersForSkippedTests()) {1069 result.setStatus(ITestResult.STARTED);1070 runTestListeners(result);1071 }1072 result.setStatus(TestResult.SKIP);1073 Reporter.setCurrentTestResult(result);1074 runTestListeners(result);1075 return result;1076 }1077 /** Invokes a method that has a specified threadPoolSize. */1078 private List<ITestResult> invokePooledTestMethods(1079 ITestNGMethod testMethod,1080 XmlSuite suite,1081 Map<String, String> parameters,1082 ConfigurationGroupMethods groupMethods,1083 ITestContext testContext) {1084 //1085 // Create the workers1086 //1087 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1088 // Create one worker per invocationCount1089 for (int i = 0; i < testMethod.getInvocationCount(); i++) {1090 // we use clones for reporting purposes1091 ITestNGMethod clonedMethod = testMethod.clone();1092 clonedMethod.setInvocationCount(1);1093 clonedMethod.setThreadPoolSize(1);1094 MethodInstance mi = new MethodInstance(clonedMethod);1095 workers.add(new SingleTestMethodWorker(this, mi, parameters, testContext, m_classListeners));1096 }1097 return runWorkers(1098 testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite, parameters);1099 }1100 static class FailureContext {1101 int count = 0;1102 List<Object> instances = Lists.newArrayList();1103 }1104 private static class StatusHolder {1105 boolean handled = false;1106 int status;1107 }1108 private StatusHolder considerExceptions(1109 ITestNGMethod tm,1110 ITestResult testresult,1111 ExpectedExceptionsHolder exceptionsHolder,1112 FailureContext failure) {1113 StatusHolder holder = new StatusHolder();1114 holder.status = testresult.getStatus();1115 holder.handled = false;1116 Throwable ite = testresult.getThrowable();1117 if (holder.status == ITestResult.FAILURE && ite != null) {1118 // Invocation caused an exception, see if the method was annotated with @ExpectedException1119 if (exceptionsHolder != null) {1120 if (exceptionsHolder.isExpectedException(ite)) {1121 testresult.setStatus(ITestResult.SUCCESS);1122 holder.status = ITestResult.SUCCESS;1123 } else {1124 if (isSkipExceptionAndSkip(ite)) {1125 holder.status = ITestResult.SKIP;1126 } else {1127 testresult.setThrowable(exceptionsHolder.wrongException(ite));1128 holder.status = ITestResult.FAILURE;1129 }1130 }1131 } else {1132 handleException(ite, tm, testresult, failure.count++);1133 holder.handled = true;1134 holder.status = testresult.getStatus();1135 }1136 } else if (holder.status != ITestResult.SKIP && exceptionsHolder != null) {1137 TestException exception = exceptionsHolder.noException(tm);1138 if (exception != null) {1139 testresult.setThrowable(exception);1140 holder.status = ITestResult.FAILURE;1141 }1142 }1143 return holder;1144 }1145 private void handleInvocationResults(1146 ITestNGMethod testMethod,1147 ITestResult testResult,1148 FailureContext failure,1149 StatusHolder holder,1150 boolean wasResultUnaltered) {1151 //1152 // Go through all the results and create a TestResult for each of them1153 //1154 List<ITestResult> resultsToRetry = Lists.newArrayList();1155 Throwable ite = testResult.getThrowable();1156 int status =1157 computeTestStatusComparingTestResultAndStatusHolder(testResult, holder, wasResultUnaltered);1158 boolean handled = holder.handled;1159 IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1160 boolean willRetry =1161 retryAnalyzer != null1162 && status == ITestResult.FAILURE1163 && failure.instances != null1164 && retryAnalyzer.retry(testResult);1165 if (willRetry) {1166 resultsToRetry.add(testResult);1167 Object instance = testResult.getInstance();1168 if (!failure.instances.contains(instance)) {1169 failure.instances.add(instance);1170 }1171 testResult.setStatus(ITestResult.SKIP);1172 testResult.setWasRetried(true);1173 } else {1174 testResult.setStatus(status);1175 if (status == ITestResult.FAILURE && !handled) {1176 int count = failure.count++;1177 if (testMethod.isDataDriven()) {1178 count = 0;1179 }1180 handleException(ite, testMethod, testResult, count);1181 }1182 }1183 }1184 private static int computeTestStatusComparingTestResultAndStatusHolder(1185 ITestResult testResult, StatusHolder holder, boolean wasResultUnaltered) {1186 if (wasResultUnaltered) {1187 return holder.status;1188 }1189 return testResult.getStatus();1190 }1191 private boolean isSkipExceptionAndSkip(Throwable ite) {1192 return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip();1193 }1194 /**1195 * To reduce thread contention and also to correctly handle thread-confinement this method invokes1196 * the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1197 */1198 private List<ITestResult> runWorkers(1199 ITestNGMethod testMethod,1200 List<IWorker<ITestNGMethod>> workers,1201 int threadPoolSize,1202 ConfigurationGroupMethods groupMethods,1203 XmlSuite suite,1204 Map<String, String> parameters) {1205 // Invoke @BeforeGroups on the original method (reduce thread contention,1206 // and also solve thread confinement)1207 ITestClass testClass = testMethod.getTestClass();1208 Object[] instances = testClass.getInstances(true);1209 for (Object instance : instances) {1210 invokeBeforeGroupsConfigurations(testMethod, groupMethods, suite, parameters, instance);1211 }1212 long maxTimeOut = -1; // 10 seconds1213 for (IWorker<ITestNGMethod> tmw : workers) {1214 long mt = tmw.getTimeOut();1215 if (mt > maxTimeOut) {1216 maxTimeOut = mt;1217 }1218 }1219 ThreadUtil.execute("methods", workers, threadPoolSize, maxTimeOut, true);1220 //1221 // Collect all the TestResults1222 //1223 List<ITestResult> result = Lists.newArrayList();1224 for (IWorker<ITestNGMethod> tmw : workers) {1225 if (tmw instanceof TestMethodWorker) {1226 result.addAll(((TestMethodWorker) tmw).getTestResults());1227 }1228 }1229 for (Object instance : instances) {1230 invokeAfterGroupsConfigurations(testMethod, groupMethods, suite, parameters, instance);1231 }1232 return result;1233 }1234 /**1235 * Checks to see of the test method has certain dependencies that prevents TestNG from executing1236 * it1237 *1238 * @param testMethod test method being checked for1239 * @return error message or null if dependencies have been run successfully1240 */1241 private String checkDependencies(ITestNGMethod testMethod, ITestNGMethod[] allTestMethods) {1242 // If this method is marked alwaysRun, no need to check for its dependencies1243 if (testMethod.isAlwaysRun()) {1244 return null;1245 }1246 // Any missing group?1247 if (testMethod.getMissingGroup() != null && !testMethod.ignoreMissingDependencies()) {1248 return "Method "1249 + testMethod1250 + " depends on nonexistent group \""1251 + testMethod.getMissingGroup()1252 + "\"";1253 }1254 // If this method depends on groups, collect all the methods that1255 // belong to these groups and make sure they have been run successfully1256 String[] groups = testMethod.getGroupsDependedUpon();1257 if (null != groups && groups.length > 0) {1258 // Get all the methods that belong to the group depended upon1259 for (String element : groups) {1260 ITestNGMethod[] methods =1261 MethodGroupsHelper.findMethodsThatBelongToGroup(1262 testMethod, m_testContext.getAllTestMethods(), element);1263 if (methods.length == 0 && !testMethod.ignoreMissingDependencies()) {1264 // Group is missing1265 return "Method " + testMethod + " depends on nonexistent group \"" + element + "\"";1266 }1267 if (!haveBeenRunSuccessfully(testMethod, methods)) {1268 return "Method "1269 + testMethod1270 + " depends on not successfully finished methods in group \""1271 + element1272 + "\"";1273 }1274 }1275 } // depends on groups1276 // If this method depends on other methods, make sure all these other1277 // methods have been run successfully1278 if (TestNgMethodUtils.cannotRunMethodIndependently(testMethod)) {1279 ITestNGMethod[] methods = MethodHelper.findDependedUponMethods(testMethod, allTestMethods);1280 if (!haveBeenRunSuccessfully(testMethod, methods)) {1281 return "Method " + testMethod + " depends on not successfully finished methods";1282 }1283 }1284 return null;1285 }1286 /** @return the test results that apply to one of the instances of the testMethod. */1287 private Set<ITestResult> keepSameInstances(ITestNGMethod method, Set<ITestResult> results) {1288 Set<ITestResult> result = Sets.newHashSet();1289 for (ITestResult r : results) {1290 Object o = method.getInstance();1291 // Keep this instance if 1) It's on a different class or 2) It's on the same class1292 // and on the same instance1293 Object instance = r.getInstance() != null ? r.getInstance() : r.getMethod().getInstance();1294 if (r.getTestClass() != method.getTestClass() || instance == o) result.add(r);1295 }1296 return result;1297 }1298 /** @return true if all the methods have been run successfully */1299 private boolean haveBeenRunSuccessfully(ITestNGMethod testMethod, ITestNGMethod[] methods) {1300 // Make sure the method has been run successfully1301 for (ITestNGMethod method : methods) {1302 Set<ITestResult> results = keepSameInstances(testMethod, m_notifier.getPassedTests(method));1303 Set<ITestResult> failedAndSkippedMethods = Sets.newHashSet();1304 Set<ITestResult> skippedAttempts = m_notifier.getSkippedTests(method);1305 failedAndSkippedMethods.addAll(m_notifier.getFailedTests(method));1306 failedAndSkippedMethods.addAll(skippedAttempts);1307 Set<ITestResult> failedresults = keepSameInstances(testMethod, failedAndSkippedMethods);1308 boolean wasMethodRetried = !results.isEmpty() && !skippedAttempts.isEmpty();1309 if (!wasMethodRetried && !failedresults.isEmpty()) {1310 // If failed results were returned on the same instance, then these tests didn't pass1311 return false;1312 }1313 for (ITestResult result : results) {1314 if (!result.isSuccess()) {1315 return false;1316 }1317 }1318 }1319 return true;1320 }1321 /**1322 * An exception was thrown by the test, determine if this method should be marked as a failure or1323 * as failure_but_within_successPercentage1324 */1325 private void handleException(1326 Throwable throwable, ITestNGMethod testMethod, ITestResult testResult, int failureCount) {1327 if (throwable != null && testResult.getThrowable() == null) {1328 testResult.setThrowable(throwable);1329 }1330 int successPercentage = testMethod.getSuccessPercentage();1331 int invocationCount = testMethod.getInvocationCount();1332 float numberOfTestsThatCanFail = ((100 - successPercentage) * invocationCount) / 100f;1333 if (failureCount < numberOfTestsThatCanFail) {1334 testResult.setStatus(ITestResult.SUCCESS_PERCENTAGE_FAILURE);1335 } else {1336 testResult.setStatus(ITestResult.FAILURE);1337 }1338 }1339 interface Predicate<K, T> {1340 boolean isTrue(K k, T v);1341 }1342 static class CanRunFromClassPredicate implements Predicate<ITestNGMethod, IClass> {1343 @Override1344 public boolean isTrue(ITestNGMethod m, IClass v) {1345 return m.canRunFromClass(v);1346 }1347 }1348 static class SameClassNamePredicate implements Predicate<ITestNGMethod, IClass> {1349 @Override1350 public boolean isTrue(ITestNGMethod m, IClass c) {...

Full Screen

Full Screen

Source:FacileTestListener.java Github

copy

Full Screen

...141 int retryCount = getRetryCount(tr);142 if(retryCount > 0){143 if (methods.containsKey(hashCode)) {144 if (methods.get(hashCode) <= retryCount) {145 tr.setStatus(ITestResult.SKIP);146 tr.getTestContext().getFailedTests().removeResult(tr.getMethod());147 }148 } else {149 tr.setStatus(ITestResult.SKIP);150 tr.getTestContext().getFailedTests().removeResult(tr.getMethod());151 }152 }153 }154 /**155 * Gets the suite test name.156 * 157 * @param fullTestName158 * the full test name159 * @return the suite test name160 */161 protected String getSuiteTestName(String fullTestName) {162 fullTestName = fullTestName.replaceAll("com.ooyala.webdriver.tests.",163 "");...

Full Screen

Full Screen

Source:TestngListener.java Github

copy

Full Screen

...98 }99 100 public void onTestSuccess(ITestResult result) 101 {102 GenericLib.setStatus(result.getName().toString(), "Passed",sTestName,sStatus);103 qrLog.info("TEST STATUS = PASSED"+" ");104 qrLog.info("___________________________________________________");105 qrLog.info(" ");106 107 }108 public void onTestFailure(ITestResult result) 109 {110 Date date = new Date();111 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_hh_mm_ss");112 String sdate = sdf.format(date);113 114 String sClass=result.getTestClass().getRealClass().toString();115 sClass= sClass.replace("class ", "");116 117 String sPackage=result.getTestClass().getRealClass().getPackage().toString();118 sPackage = sPackage.replace("package ", "");119 120 String sImage = sClass.replace(sPackage+".","");121 sImage = sImage+"_"+result.getName().toString();122 123 Object obj=result.getInstance();124 BaseLib baseLib = (BaseLib)obj;125 qrLog.error("TEST STATUS = FAILED"+" ");126 qrLog.info("___________________________________________________");127 qrLog.info(" ");128 File imgFile = ((TakesScreenshot) baseLib.driver).getScreenshotAs(OutputType.FILE);129 System.out.println(imgFile.getAbsolutePath());130 try131 {132 FileUtils.copyFile(imgFile, new File(sScreenShots.getAbsolutePath()+"\\"+sImage+".png"));133 } catch (IOException e) {134 e.printStackTrace();135 }136 GenericLib.setStatus(result.getName().toString(), "Failed",sTestName,sStatus); 137 }138 public void onTestSkipped(ITestResult result) 139 {140 qrLog.error("TEST STATUS = SKIPPED"+" ");141 qrLog.info("___________________________________________________");142 qrLog.info(" ");143 GenericLib.setStatus(result.getName(), "Skipped",sTestName,sStatus);144 }145 146 public void onTestFailedButWithinSuccessPercentage(ITestResult result) 147 {148 qrLog.warn("");149 }150 public void onStart(ITestContext context) 151 {152 String suiteName = context.getCurrentXmlTest().toString();153 suiteName=suiteName.substring(suiteName.lastIndexOf("scripts"), suiteName.lastIndexOf("Test] ")).replace("scripts.","");154 qrLog.info("###################### START OF THE -"+ suiteName+"Test ######################");155 }156 157 public void onFinish(ITestContext context) ...

Full Screen

Full Screen

Source:HouzifyTestngListener.java Github

copy

Full Screen

...76 }77 78 public void onTestSuccess(ITestResult result) 79 {80 GenericLib.setStatus(result.getName().toString(), "Passed",sTestName,sStatus);81 qrLog.info("TEST STATUS = PASSED"+" ");82 qrLog.info("___________________________________________________");83 84 }85 public void onTestFailure(ITestResult result) 86 {87 Date date = new Date();88 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy_hh_mm_ss");89 String sdate = sdf.format(date);90 String sImage=result.getName().toString().replace("test","");91 Object obj=result.getInstance();92 HouzifyBaseLib baseLib = (HouzifyBaseLib)obj;93 qrLog.error("TEST STATUS = FAILED"+" ");94 qrLog.info("___________________________________________________");95 96 File imgFile = ((TakesScreenshot) baseLib.driver).getScreenshotAs(OutputType.FILE);97 System.out.println(imgFile.getAbsolutePath());98 try {99 100 FileUtils.copyFile(imgFile, new File(HouzifyBaseLib.sDirPath+"\\Reports\\ScreenShots\\"+sImage+"__"+sdate+".png"));101 } catch (IOException e) {102 e.printStackTrace();103 }104 105 GenericLib.setStatus(result.getName().toString(), "Failed",sTestName,sStatus);106 107 }108 public void onTestSkipped(ITestResult result) 109 {110 qrLog.error("TEST STATUS = SKIPPED"+" ");111 qrLog.info("___________________________________________________");112 GenericLib.setStatus(result.getName(), "Skipped",sTestName,sStatus);113 114 }115 116 public void onTestFailedButWithinSuccessPercentage(ITestResult result) 117 {118 qrLog.warn("");119 120 }121 public void onStart(ITestContext context) 122 {123 qrLog.info("###################### START OF THE TEST ######################");124 }125 126 public void onFinish(ITestContext context) ...

Full Screen

Full Screen

Source:MyTestNGAnnotationListener.java Github

copy

Full Screen

...43 if(method.getClass().isAnnotationPresent(MyTestNGAnnotation.class)) {44 System.out.println("This gets invoked after every TestNG Test that has @MyTestNGAnnotation Annotation...");45 }46 if( !testSuccess ) {47 testResult.setStatus(ITestResult.FAILURE);48 }49 }50 }51 public void onTestStart(ITestResult result) {52 // TODO Auto-generated method stub53 54 }55 public void onTestSuccess(ITestResult result) {56 // TODO Auto-generated method stub57 58 }59 public void onTestFailure(ITestResult result) {60 // TODO Auto-generated method stub61 ...

Full Screen

Full Screen

Source:XrayListener.java Github

copy

Full Screen

...43 */44 public void afterInvocation(IInvokedMethod method, ITestResult testResult) {45 if(method.isTestMethod()) {46 if( !testSuccess ) {47 testResult.setStatus(ITestResult.FAILURE);48 }49 }50 }51 public void onTestStart(ITestResult result) {52 // TODO Auto-generated method stub53 54 }55 public void onTestSuccess(ITestResult result) {56 // TODO Auto-generated method stub57 58 }59 public void onTestFailure(ITestResult result) {60 // TODO Auto-generated method stub61 ...

Full Screen

Full Screen

Source:RetryAnalyzer.java Github

copy

Full Screen

...20 if (!testResult.isSuccess()) {21 if (retryCount < maxRetryCount) {22 retryCount++;23 // override status24 testResult.setStatus(ITestResult.SUCCESS);25 String message = Thread.currentThread().getName() + ": Error in " + testResult.getName() + 26 " Retrying " + (maxRetryCount + 1 - retryCount) + " more times";27 System.out.println(message);28 Reporter.log("message");29 return true;30 } else {31 testResult.setStatus(ITestResult.FAILURE);32 }33 }34 return false;35 }36 */37 @Override38 public boolean retry(ITestResult testResult) {39 if (testResult.getAttributeNames().contains("retry") == false) {40 System.out.println("retry count = " + retryCount + "\n"41 + "max retry count = " + retryMaxCount);42 if (retryCount < retryMaxCount) {43 // override status44 testResult.setStatus(ITestResult.SUCCESS);45 System.out.println("Retrying " + testResult.getName() + " with status "46 + testResult.getStatus() + " for the try " + (retryCount + 1)47 + " of " + retryMaxCount + " max times.");48 retryCount++;49 return true;50 } else if (retryCount == retryMaxCount) {51 // last retry to fail loud52 String stackTrace = testResult.getThrowable().fillInStackTrace()53 .toString();54 System.out.println(stackTrace);55 ReportCreator.addTestInfo(testResult.getName(),56 testResult.getTestClass().toString(), resultOfTest(testResult),57 stackTrace);58 testResult.setStatus(ITestResult.FAILURE);59 return false;60 }61 }62 return false;63 }64 public String resultOfTest(ITestResult testResult) {65 int status = testResult.getStatus();66 if (status == 1) {67 return "Success";68 }69 if (status == 2) {70 return "Failure";71 } else {72 return "Unknown";...

Full Screen

Full Screen

Source:ConnsTestListener.java Github

copy

Full Screen

...34 int size = verificationFailures.size();35 // if there are verification failures...36 if (size > 0) {37 // set the test to failed38 testResult.setStatus(ITestResult.FAILURE);39 testResult.setAttribute("ErrorMsg ", verificationFailures.toString());40 Reporter.log(verificationFailures.toString());41 // if there is an assertion failure add it to42 // verificationFailures43 if (testResult.getThrowable() != null) {44 verificationFailures.add(testResult.getThrowable().getMessage());45 }46 StringBuffer failureMsg = new StringBuffer();47 for (int i = 0; i < size; i++) {48 failureMsg.append(verificationFailures.get(i)).append("\n");49 }50 // set merged throwable51 Throwable merged = new Throwable(failureMsg.toString());52 testResult.setThrowable(merged); ...

Full Screen

Full Screen

setStatus

Using AI Code Generation

copy

Full Screen

1public class TestStatusListener implements ITestListener {2 public void onTestStart(ITestResult result) {3 System.out.println("Test started: " + result.getName());4 }5 public void onTestSuccess(ITestResult result) {6 System.out.println("Test success: " + result.getName());7 }8 public void onTestFailure(ITestResult result) {9 System.out.println("Test failed: " + result.getName());10 result.setStatus(ITestResult.FAILURE);11 }12 public void onTestSkipped(ITestResult result) {13 System.out.println("Test skipped: " + result.getName());14 }15 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {16 System.out.println("Test failed but within success percentage: " + result.getName());17 }18 public void onStart(ITestContext context) {19 System.out.println("Test started: " + context.getName());20 }21 public void onFinish(ITestContext context) {22 System.out.println("Test finished: " + context.getName());23 }24}25public class Test {26 public void testMethod() {27 System.out.println("Test method");28 Assert.assertTrue(false);29 }30}31public class Test {32 public void testMethod() {33 System.out.println("Test method");34 Assert.assertTrue(false);35 }36}37public class TestStatusListener implements ITestListener {38 public void onTestStart(ITestResult result) {39 System.out.println("Test started:

Full Screen

Full Screen

setStatus

Using AI Code Generation

copy

Full Screen

1testResult.setStatus(ITestResult.SUCCESS);2testResult.setStatus(ITestResult.FAILURE);3testResult.setStatus(ITestResult.SKIP);4testResult.setThrowable(new Throwable("error message"));5testResult.setParameters(new Object[]{"parameter1"});6testResult.setTestName("test case name");7testResult.setStartMillis(System.currentTimeMillis());8testResult.setEndMillis(System.currentTimeMillis());9testResult.setHost("host name");10testResult.setInvocationCount(1);11testResult.setParameters(new Object[]{"parameter1"});12testResult.setTestName("test case name");13testResult.setStartMillis(System.currentTimeMillis());14testResult.setEndMillis(System.currentTimeMillis());15testResult.setHost("host name");

Full Screen

Full Screen

TestNG tutorial

TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.

Chapters

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

Certification

You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.

YouTube

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful