How to use getTestClass method of org.testng.Interface ITestNGMethod class

Best Testng code snippet using org.testng.Interface ITestNGMethod.getTestClass

Source:Invoker.java Github

copy

Full Screen

...145 }146 ITestNGMethod[] methods= filterMethods(testClass, allMethods, SAME_CLASS);147 for(ITestNGMethod tm : methods) {148 if(null == testClass) {149 testClass= tm.getTestClass();150 }151 ITestResult testResult= new TestResult(testClass,152 instance,153 tm,154 null,155 System.currentTimeMillis(),156 System.currentTimeMillis(),157 m_testContext);158 IConfigurationAnnotation configurationAnnotation= null;159 try {160 Object inst = tm.getInstance();161 if (inst == null) {162 inst = instance;163 }164 Class<?> objectClass= inst.getClass();165 Method method= tm.getMethod();166 // Only run the configuration if167 // - the test is enabled and168 // - the Configuration method belongs to the same class or a parent169 configurationAnnotation = AnnotationHelper.findConfiguration(m_annotationFinder, method);170 boolean alwaysRun= isAlwaysRun(configurationAnnotation);171 if(MethodHelper.isEnabled(objectClass, m_annotationFinder) || alwaysRun) {172 if (MethodHelper.isEnabled(configurationAnnotation)) {173 if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {174 handleConfigurationSkip(tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);175 continue;176 }177 log(3, "Invoking " + Utils.detailedMethodName(tm, true));178 Object[] parameters = Parameters.createConfigurationParameters(tm.getMethod(),179 params,180 parameterValues,181 currentTestMethod,182 m_annotationFinder,183 suite,184 m_testContext,185 testMethodResult);186 testResult.setParameters(parameters);187 Object newInstance = null != instance ? instance: inst;188 runConfigurationListeners(testResult, true /* before */);189 invokeConfigurationMethod(newInstance, tm,190 parameters, testResult);191 // TODO: probably we should trigger the event for each instance???192 testResult.setEndMillis(System.currentTimeMillis());193 runConfigurationListeners(testResult, false /* after */);194 }195 else {196 log(3,197 "Skipping "198 + Utils.detailedMethodName(tm, true)199 + " because it is not enabled");200 }201 } // if is enabled202 else {203 log(3,204 "Skipping "205 + Utils.detailedMethodName(tm, true)206 + " because "207 + objectClass.getName()208 + " is not enabled");209 }210 }211 catch(InvocationTargetException ex) {212 handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);213 } catch(Throwable ex) { // covers the non-wrapper exceptions214 handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);215 }216 } // for methods217 }218 /**219 * Marks the current <code>TestResult</code> as skipped and invokes the listeners.220 */221 private void handleConfigurationSkip(ITestNGMethod tm,222 ITestResult testResult,223 IConfigurationAnnotation annotation,224 ITestNGMethod currentTestMethod,225 Object instance,226 XmlSuite suite) {227 recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);228 testResult.setStatus(ITestResult.SKIP);229 runConfigurationListeners(testResult, false /* after */);230 }231 /**232 * Is the <code>IConfiguration</code> marked as alwaysRun.233 */234 private boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) {235 if(null == configurationAnnotation) {236 return false;237 }238 boolean alwaysRun= false;239 if ((configurationAnnotation.getAfterSuite()240 || configurationAnnotation.getAfterTest()241 || configurationAnnotation.getAfterTestClass()242 || configurationAnnotation.getAfterTestMethod()243 || configurationAnnotation.getBeforeTestMethod()244 || configurationAnnotation.getBeforeTestClass()245 || configurationAnnotation.getBeforeTest()246 || configurationAnnotation.getBeforeSuite())247 && configurationAnnotation.getAlwaysRun())248 {249 alwaysRun= true;250 }251 return alwaysRun;252 }253 private void handleConfigurationFailure(Throwable ite,254 ITestNGMethod tm,255 ITestResult testResult,256 IConfigurationAnnotation annotation,257 ITestNGMethod currentTestMethod,258 Object instance,259 XmlSuite suite)260 {261 Throwable cause= ite.getCause() != null ? ite.getCause() : ite;262 if(isSkipExceptionAndSkip(cause)) {263 testResult.setThrowable(cause);264 handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);265 return;266 }267 Utils.log("", 3, "Failed to invoke configuration method "268 + tm.getRealClass().getName() + "." + tm.getMethodName() + ":" + cause.getMessage());269 handleException(cause, tm, testResult, 1);270 runConfigurationListeners(testResult, false /* after */);271 //272 // If in TestNG mode, need to take a look at the annotation to figure out273 // what kind of @Configuration method we're dealing with274 //275 if (null != annotation) {276 recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);277 }278 }279 /**280 * @return All the classes that belong to the same <test> tag as @param cls281 */282 private XmlClass[] findClassesInSameTest(Class<?> cls, XmlSuite suite) {283 Map<String, XmlClass> vResult= Maps.newHashMap();284 String className= cls.getName();285 for(XmlTest test : suite.getTests()) {286 for(XmlClass testClass : test.getXmlClasses()) {287 if(testClass.getName().equals(className)) {288 // Found it, add all the classes in this test in the result289 for(XmlClass thisClass : test.getXmlClasses()) {290 vResult.put(thisClass.getName(), thisClass);291 }292 // Note: we need to iterate through the entire suite since the same293 // class might appear in several <test> tags294 }295 }296 }297 XmlClass[] result= vResult.values().toArray(new XmlClass[vResult.size()]);298 return result;299 }300 /**301 * Record internally the failure of a Configuration, so that we can determine302 * later if @Test should be skipped.303 */304 private void recordConfigurationInvocationFailed(ITestNGMethod tm,305 IClass testClass,306 IConfigurationAnnotation annotation,307 ITestNGMethod currentTestMethod,308 Object instance,309 XmlSuite suite) {310 // If beforeTestClass or afterTestClass failed, mark either the config method's311 // entire class as failed, or the class under tests as failed, depending on312 // the configuration failure policy313 if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {314 // tm is the configuration method, and currentTestMethod is null for BeforeClass315 // methods, so we need testClass316 if (m_continueOnFailedConfiguration) {317 setClassInvocationFailure(testClass.getRealClass(), instance);318 } else {319 setClassInvocationFailure(tm.getRealClass(), instance);320 }321 }322 // If before/afterTestMethod failed, mark either the config method's entire323 // class as failed, or just the current test method as failed, depending on324 // the configuration failure policy325 else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {326 if (m_continueOnFailedConfiguration) {327 setMethodInvocationFailure(currentTestMethod, instance);328 } else {329 setClassInvocationFailure(tm.getRealClass(), instance);330 }331 }332 // If beforeSuite or afterSuite failed, mark *all* the classes as failed333 // for configurations. At this point, the entire Suite is screwed334 else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {335 m_suiteState.failed();336 }337 // beforeTest or afterTest: mark all the classes in the same338 // <test> stanza as failed for configuration339 else if (annotation.getBeforeTest() || annotation.getAfterTest()) {340 setClassInvocationFailure(tm.getRealClass(), instance);341 XmlClass[] classes= findClassesInSameTest(tm.getRealClass(), suite);342 for(XmlClass xmlClass : classes) {343 setClassInvocationFailure(xmlClass.getSupportClass(), instance);344 }345 }346 String[] beforeGroups= annotation.getBeforeGroups();347 if(null != beforeGroups && beforeGroups.length > 0) {348 for(String group: beforeGroups) {349 m_beforegroupsFailures.put(group, Boolean.FALSE);350 }351 }352 }353 /**354 * @return true if this class or a parent class failed to initialize.355 */356 private boolean classConfigurationFailed(Class<?> cls) {357 for (Class<?> c : m_classInvocationResults.keySet()) {358 if (c == cls || c.isAssignableFrom(cls)) {359 return true;360 }361 }362 return false;363 }364 /**365 * @return true if this class has successfully run all its @Configuration366 * method or false if at least one of these methods failed.367 */368 private boolean confInvocationPassed(ITestNGMethod method, ITestNGMethod currentTestMethod,369 IClass testClass, Object instance) {370 boolean result= true;371 Class<?> cls = testClass.getRealClass();372 if(m_suiteState.isFailed()) {373 result= false;374 }375 else {376 if (classConfigurationFailed(cls)) {377 if (! m_continueOnFailedConfiguration) {378 result = !classConfigurationFailed(cls);379 } else {380 result = !m_classInvocationResults.get(cls).contains(instance);381 }382 }383 // if method is BeforeClass, currentTestMethod will be null384 else if (m_continueOnFailedConfiguration &&385 currentTestMethod != null &&386 m_methodInvocationResults.containsKey(currentTestMethod)) {387 result = !m_methodInvocationResults.get(currentTestMethod).contains(getMethodInvocationToken(currentTestMethod, instance));388 }389 else if (! m_continueOnFailedConfiguration) {390 for(Class<?> clazz: m_classInvocationResults.keySet()) {391// if (clazz == cls) {392 if(clazz.isAssignableFrom(cls)) {393 result= false;394 break;395 }396 }397 }398 }399 // check if there are failed @BeforeGroups400 String[] groups= method.getGroups();401 if(null != groups && groups.length > 0) {402 for(String group: groups) {403 if(m_beforegroupsFailures.containsKey(group)) {404 result= false;405 break;406 }407 }408 }409 return result;410 }411 // Creates a token for tracking a unique invocation of a method on an instance.412 // Is used when configFailurePolicy=continue.413 private Object getMethodInvocationToken(ITestNGMethod method, Object instance) {414 return String.format("%s+%d", instance.toString(), method.getCurrentInvocationCount());415 }416 /**417 * Effectively invokes a configuration method on all passed in instances.418 * TODO: Should change this method to be more like invokeMethod() so that we can419 * handle calls to {@code IInvokedMethodListener} better.420 *421 * @param targetInstance the instance to invoke the configuration method on422 * @param tm the configuration method423 * @param params the parameters needed for method invocation424 * @param testResult425 * @throws InvocationTargetException426 * @throws IllegalAccessException427 */428 private void invokeConfigurationMethod(Object targetInstance,429 ITestNGMethod tm,430 Object[] params,431 ITestResult testResult)432 throws InvocationTargetException, IllegalAccessException433 {434 // Mark this method with the current thread id435 tm.setId(ThreadUtil.currentThreadInfo());436 {437 InvokedMethod invokedMethod= new InvokedMethod(targetInstance,438 tm,439 params,440 System.currentTimeMillis(),441 testResult);442 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);443 m_notifier.addInvokedMethod(invokedMethod);444 try {445 Reporter.setCurrentTestResult(testResult);446 Method method = tm.getMethod();447 //448 // If this method is a IConfigurable, invoke its run() method449 //450 IConfigurable configurableInstance =451 IConfigurable.class.isAssignableFrom(tm.getMethod().getDeclaringClass()) ?452 (IConfigurable) targetInstance : m_configuration.getConfigurable();453 if (configurableInstance != null) {454 MethodInvocationHelper.invokeConfigurable(targetInstance, params, configurableInstance, method,455 testResult);456 }457 else {458 //459 // Not a IConfigurable, invoke directly460 //461 if (MethodHelper.calculateTimeOut(tm) <= 0) {462 MethodInvocationHelper.invokeMethod(method, targetInstance, params);463 }464 else {465 MethodInvocationHelper.invokeWithTimeout(tm, targetInstance, params, testResult);466 if (!testResult.isSuccess()) {467 // A time out happened468 throwConfigurationFailure(testResult, testResult.getThrowable());469 throw testResult.getThrowable();470 }471 }472 }473 }474 catch (InvocationTargetException | IllegalAccessException ex) {475 throwConfigurationFailure(testResult, ex);476 throw ex;477 } catch (Throwable ex) {478 throwConfigurationFailure(testResult, ex);479 throw new TestNGException(ex);480 }481 finally {482 Reporter.setCurrentTestResult(testResult);483 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484 Reporter.setCurrentTestResult(null);485 }486 }487 }488 private void throwConfigurationFailure(ITestResult testResult, Throwable ex)489 {490 testResult.setStatus(ITestResult.FAILURE);;491 testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());492 }493 private void runInvokedMethodListeners(InvokedMethodListenerMethod listenerMethod, IInvokedMethod invokedMethod,494 ITestResult testResult)495 {496 if ( noListenersPresent() ) {497 return;498 }499 InvokedMethodListenerInvoker invoker = new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);500 for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {501 invoker.invokeListener(currentListener, invokedMethod);502 }503 }504 private boolean noListenersPresent() {505 return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.size() == 0);506 }507 // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.508 private ITestResult invokeMethod(Object instance,509 final ITestNGMethod tm,510 Object[] parameterValues,511 int parametersIndex,512 XmlSuite suite,513 Map<String, String> params,514 ITestClass testClass,515 ITestNGMethod[] beforeMethods,516 ITestNGMethod[] afterMethods,517 ConfigurationGroupMethods groupMethods,518 FailureContext failureContext) {519 TestResult testResult = new TestResult();520 //521 // Invoke beforeGroups configurations522 //523 invokeBeforeGroupsConfigurations(testClass, tm, groupMethods, suite, params,524 instance);525 //526 // Invoke beforeMethods only if527 // - firstTimeOnly is not set528 // - firstTimeOnly is set, and we are reaching at the first invocationCount529 //530 invokeConfigurations(testClass, tm,531 filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),532 suite, params, parameterValues,533 instance, testResult);534 //535 // Create the ExtraOutput for this method536 //537 InvokedMethod invokedMethod = null;538 try {539 testResult.init(testClass, instance,540 tm,541 null,542 System.currentTimeMillis(),543 0,544 m_testContext);545 testResult.setParameters(parameterValues);546 testResult.setHost(m_testContext.getHost());547 testResult.setStatus(ITestResult.STARTED);548 invokedMethod= new InvokedMethod(instance,549 tm,550 parameterValues,551 System.currentTimeMillis(),552 testResult);553 // Fix from ansgarkonermann554 // invokedMethod is used in the finally, which can be invoked if555 // any of the test listeners throws an exception, therefore,556 // invokedMethod must have a value before we get here557 runTestListeners(testResult);558 runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);559 m_notifier.addInvokedMethod(invokedMethod);560 Method thisMethod = tm.getConstructorOrMethod().getMethod();561 if(confInvocationPassed(tm, tm, testClass, instance)) {562 log(3, "Invoking " + tm.getRealClass().getName() + "." + tm.getMethodName());563 Reporter.setCurrentTestResult(testResult);564 // If this method is a IHookable, invoke its run() method565 IHookable hookableInstance =566 IHookable.class.isAssignableFrom(tm.getRealClass()) ?567 (IHookable) instance : m_configuration.getHookable();568 if (MethodHelper.calculateTimeOut(tm) <= 0) {569 if (hookableInstance != null) {570 MethodInvocationHelper.invokeHookable(instance,571 parameterValues, hookableInstance, thisMethod, testResult);572 } else {573 // Not a IHookable, invoke directly574 MethodInvocationHelper.invokeMethod(thisMethod, instance,575 parameterValues);576 }577 testResult.setStatus(ITestResult.SUCCESS);578 } else {579 // Method with a timeout580 MethodInvocationHelper.invokeWithTimeout(tm, instance, parameterValues, testResult, hookableInstance);581 }582 }583 else {584 testResult.setStatus(ITestResult.SKIP);585 }586 }587 catch(InvocationTargetException ite) {588 testResult.setThrowable(ite.getCause());589 testResult.setStatus(ITestResult.FAILURE);590 }591 catch(ThreadExecutionException tee) { // wrapper for TestNGRuntimeException592 Throwable cause= tee.getCause();593 if(TestNGRuntimeException.class.equals(cause.getClass())) {594 testResult.setThrowable(cause.getCause());595 }596 else {597 testResult.setThrowable(cause);598 }599 testResult.setStatus(ITestResult.FAILURE);600 }601 catch(Throwable thr) { // covers the non-wrapper exceptions602 testResult.setThrowable(thr);603 testResult.setStatus(ITestResult.FAILURE);604 }605 finally {606 // Set end time ASAP607 testResult.setEndMillis(System.currentTimeMillis());608 ExpectedExceptionsHolder expectedExceptionClasses609 = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));610 List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);611 handleInvocationResults(tm, results, expectedExceptionClasses, failureContext);612 // If this method has a data provider and just failed, memorize the number613 // at which it failed.614 // Note: we're not exactly testing that this method has a data provider, just615 // that it has parameters, so might have to revisit this if bugs get reported616 // for the case where this method has parameters that don't come from a data617 // provider618 if (testResult.getThrowable() != null && parameterValues.length > 0) {619 tm.addFailedInvocationNumber(parametersIndex);620 }621 //622 // Increment the invocation count for this method623 //624 tm.incrementCurrentInvocationCount();625 // Run invokedMethodListeners after updating TestResult626 runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);627 runTestListeners(testResult);628 //629 // Invoke afterMethods only if630 // - lastTimeOnly is not set631 // - lastTimeOnly is set, and we are reaching the last invocationCount632 //633 invokeConfigurations(testClass, tm,634 filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),635 suite, params, parameterValues,636 instance,637 testResult);638 //639 // Invoke afterGroups configurations640 //641 invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite,642 params, instance);643 // Reset the test result last. If we do this too early, Reporter.log()644 // invocations from listeners will be discarded645 Reporter.setCurrentTestResult(null);646 }647 return testResult;648 }649 void collectResults(ITestNGMethod testMethod, Collection<ITestResult> results) {650 for (ITestResult result : results) {651 // Collect the results652 final int status = result.getStatus();653 if(ITestResult.SUCCESS == status) {654 m_notifier.addPassedTest(testMethod, result);655 }656 else if(ITestResult.SKIP == status) {657 m_notifier.addSkippedTest(testMethod, result);658 }659 else if(ITestResult.FAILURE == status) {660 m_notifier.addFailedTest(testMethod, result);661 }662 else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {663 m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);664 }665 else {666 assert false : "UNKNOWN STATUS:" + status;667 }668 }669 }670 /**671 * The array of methods contains @BeforeMethods if isBefore if true, @AfterMethods672 * otherwise. This function removes all the methods that should not be run at this673 * point because they are either firstTimeOnly or lastTimeOnly and we haven't reached674 * the current invocationCount yet675 */676 private ITestNGMethod[] filterConfigurationMethods(ITestNGMethod tm,677 ITestNGMethod[] methods, boolean isBefore)678 {679 List<ITestNGMethod> result = Lists.newArrayList();680 for (ITestNGMethod m : methods) {681 ConfigurationMethod cm = (ConfigurationMethod) m;682 if (isBefore) {683 if (! cm.isFirstTimeOnly() ||684 (cm.isFirstTimeOnly() && tm.getCurrentInvocationCount() == 0))685 {686 result.add(m);687 }688 }689 else {690 int current = tm.getCurrentInvocationCount();691 boolean isLast = false;692 // If we have parameters, set the boolean if we are about to run693 // the last invocation694 if (tm.getParameterInvocationCount() > 0) {695 isLast = current == tm.getParameterInvocationCount() * tm.getTotalInvocationCount();696 }697 // If we have invocationCount > 1, set the boolean if we are about to698 // run the last invocation699 else if (tm.getTotalInvocationCount() > 1) {700 isLast = current == tm.getTotalInvocationCount();701 }702 if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && isLast)) {703 result.add(m);704 }705 }706 }707 return result.toArray(new ITestNGMethod[result.size()]);708 }709 /**710 * invokeTestMethods() eventually converge here to invoke a single @Test method.711 * <p/>712 * This method is responsible for actually invoking the method. It decides if the invocation713 * must be done:714 * <ul>715 * <li>through an <code>IHookable</code></li>716 * <li>directly (through reflection)</li>717 * <li>in a separate thread (in case it needs to timeout)718 * </ul>719 *720 * <p/>721 * This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup722 * if it is the case for the passed in @Test method.723 */724 protected ITestResult invokeTestMethod(Object instance,725 final ITestNGMethod tm,726 Object[] parameterValues,727 int parametersIndex,728 XmlSuite suite,729 Map<String, String> params,730 ITestClass testClass,731 ITestNGMethod[] beforeMethods,732 ITestNGMethod[] afterMethods,733 ConfigurationGroupMethods groupMethods,734 FailureContext failureContext)735 {736 // Mark this method with the current thread id737 tm.setId(ThreadUtil.currentThreadInfo());738 ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,739 testClass, beforeMethods, afterMethods, groupMethods,740 failureContext);741 return result;742 }743 /**744 * Filter all the beforeGroups methods and invoke only those that apply745 * to the current test method746 */747 private void invokeBeforeGroupsConfigurations(ITestClass testClass,748 ITestNGMethod currentTestMethod,749 ConfigurationGroupMethods groupMethods,750 XmlSuite suite,751 Map<String, String> params,752 Object instance)753 {754 synchronized(groupMethods) {755 List<ITestNGMethod> filteredMethods = Lists.newArrayList();756 String[] groups = currentTestMethod.getGroups();757 Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();758 for (String group : groups) {759 List<ITestNGMethod> methods = beforeGroupMap.get(group);760 if (methods != null) {761 filteredMethods.addAll(methods);762 }763 }764 ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);765 //766 // Invoke the right groups methods767 //768 if(beforeMethodsArray.length > 0) {769 // don't pass the IClass or the instance as the method may be external770 // the invocation must be similar to @BeforeTest/@BeforeSuite771 invokeConfigurations(null, beforeMethodsArray, suite, params,772 null, /* no parameter values */773 null);774 }775 //776 // Remove them so they don't get run again777 //778 groupMethods.removeBeforeGroups(groups);779 }780 }781 private void invokeAfterGroupsConfigurations(ITestClass testClass,782 ITestNGMethod currentTestMethod,783 ConfigurationGroupMethods groupMethods,784 XmlSuite suite,785 Map<String, String> params,786 Object instance)787 {788 // Skip this if the current method doesn't belong to any group789 // (only a method that belongs to a group can trigger the invocation790 // of afterGroups methods)791 if (currentTestMethod.getGroups().length == 0) {792 return;793 }794 // See if the currentMethod is the last method in any of the groups795 // it belongs to796 Map<String, String> filteredGroups = Maps.newHashMap();797 String[] groups = currentTestMethod.getGroups();798 synchronized(groupMethods) {799 for (String group : groups) {800 if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {801 filteredGroups.put(group, group);802 }803 }804 if(filteredGroups.isEmpty()) {805 return;806 }807 // The list of afterMethods to run808 Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();809 // Now filteredGroups contains all the groups for which we need to run the afterGroups810 // method. Find all the methods that correspond to these groups and invoke them.811 Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();812 for (String g : filteredGroups.values()) {813 List<ITestNGMethod> methods = map.get(g);814 // Note: should put them in a map if we want to make sure the same afterGroups815 // doesn't get run twice816 if (methods != null) {817 for (ITestNGMethod m : methods) {818 afterMethods.put(m, m);819 }820 }821 }822 // Got our afterMethods, invoke them823 ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);824 // don't pass the IClass or the instance as the method may be external825 // the invocation must be similar to @BeforeTest/@BeforeSuite826 invokeConfigurations(null, afterMethodsArray, suite, params,827 null, /* no parameter values */828 null);829 // Remove the groups so they don't get run again830 groupMethods.removeAfterGroups(filteredGroups.keySet());831 }832 }833 private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {834 while (parametersValues.hasNext()) {835 Object[] parameters = parametersValues.next();836 if (index == 0) {837 return parameters;838 }839 index--;840 }841 return null;842 }843 int retryFailed(Object instance,844 final ITestNGMethod tm,845 XmlSuite suite,846 ITestClass testClass,847 ITestNGMethod[] beforeMethods,848 ITestNGMethod[] afterMethods,849 ConfigurationGroupMethods groupMethods,850 List<ITestResult> result,851 int failureCount,852 ExpectedExceptionsHolder expectedExceptionHolder,853 ITestContext testContext,854 Map<String, String> parameters,855 int parametersIndex) {856 final FailureContext failure = new FailureContext();857 failure.count = failureCount;858 do {859 failure.instances = Lists.newArrayList ();860 Map<String, String> allParameters = Maps.newHashMap();861 /**862 * TODO: This recreates all the parameters every time when we only need863 * one specific set. Should optimize it by only recreating the set needed.864 */865 ParameterBag bag = createParameters(tm, parameters,866 allParameters, suite, testContext, null /* fedInstance */);867 Object[] parameterValues =868 getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);869 result.add(invokeMethod(instance, tm, parameterValues, parametersIndex, suite,870 allParameters, testClass, beforeMethods, afterMethods, groupMethods, failure));871 }872 while (!failure.instances.isEmpty());873 return failure.count;874 }875 private ParameterBag createParameters(ITestNGMethod testMethod,876 Map<String, String> parameters,877 Map<String, String> allParameterNames,878 XmlSuite suite,879 ITestContext testContext,880 Object fedInstance)881 {882 Object instance;883 if (fedInstance != null) {884 instance = fedInstance;885 }886 else {887 instance = testMethod.getInstance();888 }889 ParameterBag bag = handleParameters(testMethod,890 instance, allParameterNames, parameters, null, suite, testContext, fedInstance, null);891 return bag;892 }893 /**894 * Invoke all the test methods. Note the plural: the method passed in895 * parameter might be invoked several times if the test class it belongs896 * to has more than one instance (i.e., if an @Factory method has been897 * declared somewhere that returns several instances of this TestClass).898 * If no @Factory method was specified, testMethod will only be invoked899 * once.900 * <p/>901 * Note that this method also takes care of invoking the beforeTestMethod902 * and afterTestMethod, if any.903 *904 * Note (alex): this method can be refactored to use a SingleTestMethodWorker that905 * directly invokes906 * {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)}907 * and this would simplify the implementation (see how DataTestMethodWorker is used)908 */909 @Override910 public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,911 XmlSuite suite,912 Map<String, String> testParameters,913 ConfigurationGroupMethods groupMethods,914 Object instance,915 ITestContext testContext)916 {917 // Potential bug here if the test method was declared on a parent class918 assert null != testMethod.getTestClass()919 : "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass();920 if (!MethodHelper.isEnabled(testMethod.getMethod(), m_annotationFinder)) {921 // return if the method is not enabled. No need to do any more calculations922 return Collections.emptyList();923 }924 // By the time this testMethod to be invoked,925 // all dependencies should be already run or we need to skip this method,926 // so invocation count should not affect dependencies check927 final String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());928 if (okToProceed != null) {929 //930 // Not okToProceed. Test is being skipped931 //932 ITestResult result = registerSkippedTestResult(testMethod, null, System.currentTimeMillis(),933 new Throwable(okToProceed));934 m_notifier.addSkippedTest(testMethod, result);935 return Collections.singletonList(result);936 }937 final Map<String, String> parameters =938 testMethod.findMethodParameters(testContext.getCurrentXmlTest());939 // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.940 if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {941 return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);942 }943 long timeOutInvocationCount = testMethod.getInvocationTimeOut();944 //FIXME: Is this correct?945 boolean onlyOne = testMethod.getThreadPoolSize() > 1 ||946 timeOutInvocationCount > 0;947 int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();948 ExpectedExceptionsHolder expectedExceptionHolder =949 new ExpectedExceptionsHolder(m_annotationFinder, testMethod,950 new RegexpExpectedExceptionsHolder(m_annotationFinder, testMethod));951 final ITestClass testClass= testMethod.getTestClass();952 final List<ITestResult> result = Lists.newArrayList();953 final FailureContext failure = new FailureContext();954 final ITestNGMethod[] beforeMethods = filterMethods(testClass, testClass.getBeforeTestMethods(), CAN_RUN_FROM_CLASS);955 final ITestNGMethod[] afterMethods = filterMethods(testClass, testClass.getAfterTestMethods(), CAN_RUN_FROM_CLASS);956 while(invocationCount-- > 0) {957 if(false) {958 // Prevent code formatting959 }960 //961 // No threads, regular invocation962 //963 else {964 // Used in catch statement965 long start = System.currentTimeMillis();966 Map<String, String> allParameterNames = Maps.newHashMap();967 ParameterBag bag = createParameters(testMethod,968 parameters, allParameterNames, suite, testContext, instance);969 if (bag.hasErrors()) {970 final ITestResult tr = bag.errorResult;971 tr.setStatus(ITestResult.SKIP);972 runTestListeners(tr);973 m_notifier.addSkippedTest(testMethod, tr);974 result.add(tr);975 continue;976 }977 Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;978 int parametersIndex = 0;979 try {980 List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();981 if (bag.parameterHolder.origin == ParameterOrigin.ORIGIN_DATA_PROVIDER &&982 bag.parameterHolder.dataProviderHolder.annotation.isParallel()) {983 while (allParameterValues.hasNext()) {984 Object[] parameterValues = injectParameters(allParameterValues.next(),985 testMethod.getMethod(), testContext, null /* test result */);986 TestMethodWithDataProviderMethodWorker w =987 new TestMethodWithDataProviderMethodWorker(this,988 testMethod, parametersIndex,989 parameterValues, instance, suite, parameters, testClass,990 beforeMethods, afterMethods, groupMethods,991 expectedExceptionHolder, testContext, m_skipFailedInvocationCounts,992 invocationCount, failure.count, m_notifier);993 workers.add(w);994 // testng387: increment the param index in the bag.995 parametersIndex++;996 }997 PoolService<List<ITestResult>> ps =998 new PoolService<>(suite.getDataProviderThreadCount());999 List<List<ITestResult>> r = ps.submitTasksAndWait(workers);1000 for (List<ITestResult> l2 : r) {1001 result.addAll(l2);1002 }1003 } else {1004 while (allParameterValues.hasNext()) {1005 Object[] parameterValues = injectParameters(allParameterValues.next(),1006 testMethod.getMethod(), testContext, null /* test result */);1007 List<ITestResult> tmpResults = Lists.newArrayList();1008 try {1009 tmpResults.add(invokeTestMethod(instance,1010 testMethod,1011 parameterValues,1012 parametersIndex,1013 suite,1014 parameters,1015 testClass,1016 beforeMethods,1017 afterMethods,1018 groupMethods, failure));1019 }1020 finally {1021 if (failure.instances.isEmpty()) {1022 result.addAll(tmpResults);1023 } else {1024 for (Object failedInstance : failure.instances) {1025 List<ITestResult> retryResults = Lists.newArrayList();1026 failure.count = retryFailed(1027 failedInstance, testMethod, suite, testClass, beforeMethods,1028 afterMethods, groupMethods, retryResults,1029 failure.count, expectedExceptionHolder,1030 testContext, parameters, parametersIndex);1031 result.addAll(retryResults);1032 }1033 }1034 //1035 // If we have a failure, skip all the1036 // other invocationCounts1037 //1038 if (failure.count > 01039 && (m_skipFailedInvocationCounts1040 || testMethod.skipFailedInvocations())) {1041 while (invocationCount-- > 0) {1042 result.add(registerSkippedTestResult(testMethod, instance, System.currentTimeMillis(), null));1043 }1044 break;1045 }1046 }// end finally1047 parametersIndex++;1048 }1049 }1050 }1051 catch (Throwable cause) {1052 ITestResult r =1053 new TestResult(testMethod.getTestClass(),1054 instance,1055 testMethod,1056 cause,1057 start,1058 System.currentTimeMillis(),1059 m_testContext);1060 r.setStatus(TestResult.FAILURE);1061 result.add(r);1062 runTestListeners(r);1063 m_notifier.addFailedTest(testMethod, r);1064 } // catch1065 }1066 }1067 return result;1068 } // invokeTestMethod1069 private ITestResult registerSkippedTestResult(ITestNGMethod testMethod, Object instance,1070 long start, Throwable throwable) {1071 ITestResult result =1072 new TestResult(testMethod.getTestClass(),1073 instance,1074 testMethod,1075 throwable,1076 start,1077 System.currentTimeMillis(),1078 m_testContext);1079 result.setStatus(TestResult.SKIP);1080 runTestListeners(result);1081 return result;1082 }1083 /**1084 * Gets an array of parameter values returned by data provider or the ones that1085 * are injected based on parameter type. The method also checks for {@code NoInjection}1086 * annotation1087 * @param parameterValues parameter values from a data provider1088 * @param method method to be invoked1089 * @param context test context1090 * @param testResult test result1091 */1092 private Object[] injectParameters(Object[] parameterValues, Method method,1093 ITestContext context, ITestResult testResult)1094 throws TestNGException {1095 List<Object> vResult = Lists.newArrayList();1096 int i = 0;1097 int numValues = parameterValues.length;1098 int numParams = method.getParameterTypes().length;1099 if (numValues > numParams && ! method.isVarArgs()) {1100 throw new TestNGException("The data provider is trying to pass " + numValues1101 + " parameters but the method "1102 + method.getDeclaringClass().getName() + "#" + method.getName()1103 + " takes " + numParams);1104 }1105 // beyond this, numValues <= numParams1106 for (Class<?> cls : method.getParameterTypes()) {1107 Annotation[] annotations = method.getParameterAnnotations()[i];1108 boolean noInjection = false;1109 for (Annotation a : annotations) {1110 if (a instanceof NoInjection) {1111 noInjection = true;1112 break;1113 }1114 }1115 Object injected = Parameters.getInjectedParameter(cls, method, context, testResult);1116 if (injected != null && ! noInjection) {1117 vResult.add(injected);1118 } else {1119 try {1120 if (method.isVarArgs()) vResult.add(parameterValues);1121 else vResult.add(parameterValues[i++]);1122 } catch (ArrayIndexOutOfBoundsException ex) {1123 throw new TestNGException("The data provider is trying to pass " + numValues1124 + " parameters but the method "1125 + method.getDeclaringClass().getName() + "#" + method.getName()1126 + " takes " + numParams1127 + " and TestNG is unable in inject a suitable object", ex);1128 }1129 }1130 }1131 return vResult.toArray(new Object[vResult.size()]);1132 }1133 private ParameterBag handleParameters(ITestNGMethod testMethod,1134 Object instance,1135 Map<String, String> allParameterNames,1136 Map<String, String> parameters,1137 Object[] parameterValues,1138 XmlSuite suite,1139 ITestContext testContext,1140 Object fedInstance,1141 ITestResult testResult)1142 {1143 try {1144 return new ParameterBag(1145 Parameters.handleParameters(testMethod,1146 allParameterNames,1147 instance,1148 new Parameters.MethodParameters(parameters,1149 testMethod.findMethodParameters(testContext.getCurrentXmlTest()),1150 parameterValues,1151 testMethod.getMethod(), testContext, testResult),1152 suite,1153 m_annotationFinder,1154 fedInstance));1155 }1156// catch(TestNGException ex) {1157// throw ex;1158// }1159 catch(Throwable cause) {1160 return new ParameterBag(1161 new TestResult(1162 testMethod.getTestClass(),1163 instance,1164 testMethod,1165 cause,1166 System.currentTimeMillis(),1167 System.currentTimeMillis(),1168 m_testContext));1169 }1170 }1171 /**1172 * Invokes a method that has a specified threadPoolSize.1173 */1174 private List<ITestResult> invokePooledTestMethods(ITestNGMethod testMethod,1175 XmlSuite suite,1176 Map<String, String> parameters,1177 ConfigurationGroupMethods groupMethods,1178 ITestContext testContext)1179 {1180 //1181 // Create the workers1182 //1183 List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1184 // Create one worker per invocationCount1185 for (int i = 0; i < testMethod.getInvocationCount(); i++) {1186 // we use clones for reporting purposes1187 ITestNGMethod clonedMethod= testMethod.clone();1188 clonedMethod.setInvocationCount(1);1189 clonedMethod.setThreadPoolSize(1);1190 MethodInstance mi = new MethodInstance(clonedMethod);1191 workers.add(new SingleTestMethodWorker(this,1192 mi,1193 suite,1194 parameters,1195 testContext,1196 m_classListeners));1197 }1198 return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite,1199 parameters);1200 }1201 static class FailureContext {1202 int count = 0;1203 List<Object> instances = Lists.newArrayList();1204 }1205 void handleInvocationResults(ITestNGMethod testMethod,1206 List<ITestResult> result,1207 ExpectedExceptionsHolder expectedExceptionsHolder,1208 FailureContext failure)1209 {1210 //1211 // Go through all the results and create a TestResult for each of them1212 //1213 List<ITestResult> resultsToRetry = Lists.newArrayList();1214 for (ITestResult testResult : result) {1215 Throwable ite= testResult.getThrowable();1216 int status= testResult.getStatus();1217 boolean handled = false;1218 // Exception thrown?1219 if (ite != null) {1220 // Invocation caused an exception, see if the method was annotated with @ExpectedException1221 if (expectedExceptionsHolder != null) {1222 if (expectedExceptionsHolder.isExpectedException(ite)) {1223 testResult.setStatus(ITestResult.SUCCESS);1224 status = ITestResult.SUCCESS;1225 } else {1226 if (isSkipExceptionAndSkip(ite)){1227 status = ITestResult.SKIP;1228 } else {1229 testResult.setThrowable(expectedExceptionsHolder.wrongException(ite));1230 status = ITestResult.FAILURE;1231 }1232 }1233 } else {1234 handleException(ite, testMethod, testResult, failure.count++);1235 handled = true;1236 status = testResult.getStatus();1237 }1238 }1239 // No exception thrown, make sure we weren't expecting one1240 else if(status != ITestResult.SKIP && expectedExceptionsHolder != null) {1241 TestException exception = expectedExceptionsHolder.noException(testMethod);1242 if (exception != null) {1243 testResult.setThrowable(exception);1244 status= ITestResult.FAILURE;1245 }1246 }1247 IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1248 boolean willRetry = retryAnalyzer != null && status == ITestResult.FAILURE && failure.instances != null && retryAnalyzer.retry(testResult);1249 if (willRetry) {1250 resultsToRetry.add(testResult);1251 failure.count++;1252 failure.instances.add(testResult.getInstance());1253 testResult.setStatus(ITestResult.SKIP);1254 } else {1255 testResult.setStatus(status);1256 if (status == ITestResult.FAILURE && !handled) {1257 handleException(ite, testMethod, testResult, failure.count++);1258 }1259 }1260 collectResults(testMethod, Collections.singleton(testResult));1261 } // for results1262 removeResultsToRetryFromResult(resultsToRetry, result, failure);1263 }1264 private boolean isSkipExceptionAndSkip(Throwable ite) {1265 return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip();1266 }1267 private void removeResultsToRetryFromResult(List<ITestResult> resultsToRetry,1268 List<ITestResult> result, FailureContext failure) {1269 if (resultsToRetry != null) {1270 for (ITestResult res : resultsToRetry) {1271 result.remove(res);1272 failure.count--;1273 }1274 }1275 }1276 /**1277 * To reduce thread contention and also to correctly handle thread-confinement1278 * this method invokes the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1279 */1280 private List<ITestResult> runWorkers(ITestNGMethod testMethod,1281 List<IWorker<ITestNGMethod>> workers,1282 int threadPoolSize,1283 ConfigurationGroupMethods groupMethods,1284 XmlSuite suite,1285 Map<String, String> parameters)1286 {1287 // Invoke @BeforeGroups on the original method (reduce thread contention,1288 // and also solve thread confinement)1289 ITestClass testClass= testMethod.getTestClass();1290 Object[] instances = testClass.getInstances(true);1291 for(Object instance: instances) {1292 invokeBeforeGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1293 }1294 long maxTimeOut= -1; // 10 seconds1295 for(IWorker<ITestNGMethod> tmw : workers) {1296 long mt= tmw.getTimeOut();1297 if(mt > maxTimeOut) {1298 maxTimeOut= mt;1299 }1300 }1301 ThreadUtil.execute(workers, threadPoolSize, maxTimeOut, true);1302 //1303 // Collect all the TestResults1304 //1305 List<ITestResult> result = Lists.newArrayList();1306 for (IWorker<ITestNGMethod> tmw : workers) {1307 if (tmw instanceof TestMethodWorker) {1308 result.addAll(((TestMethodWorker)tmw).getTestResults());1309 }1310 }1311 for(Object instance: instances) {1312 invokeAfterGroupsConfigurations(testClass, testMethod, groupMethods, suite, parameters, instance);1313 }1314 return result;1315 }1316 /**1317 * Checks to see of the test method has certain dependencies that prevents1318 * TestNG from executing it1319 * @param testMethod test method being checked for1320 * @return error message or null if dependencies have been run successfully1321 */1322 private String checkDependencies(ITestNGMethod testMethod,1323 ITestNGMethod[] allTestMethods)1324 {1325 // If this method is marked alwaysRun, no need to check for its dependencies1326 if (testMethod.isAlwaysRun()) {1327 return null;1328 }1329 // Any missing group?1330 if (testMethod.getMissingGroup() != null1331 && !testMethod.ignoreMissingDependencies()) {1332 return "Method " + testMethod + " depends on nonexistent group \"" + testMethod.getMissingGroup() + "\"";1333 }1334 // If this method depends on groups, collect all the methods that1335 // belong to these groups and make sure they have been run successfully1336 final String[] groups = testMethod.getGroupsDependedUpon();1337 if (null != groups && groups.length > 0) {1338 // Get all the methods that belong to the group depended upon1339 for (String element : groups) {1340 ITestNGMethod[] methods =1341 MethodGroupsHelper.findMethodsThatBelongToGroup(testMethod,1342 m_testContext.getAllTestMethods(),1343 element);1344 if (methods.length == 0 && !testMethod.ignoreMissingDependencies()) {1345 // Group is missing1346 return "Method " + testMethod + " depends on nonexistent group \"" + element + "\"";1347 }1348 if (!haveBeenRunSuccessfully(testMethod, methods)) {1349 return "Method " + testMethod +1350 " depends on not successfully finished methods in group \"" + element + "\"";1351 }1352 }1353 } // depends on groups1354 // If this method depends on other methods, make sure all these other1355 // methods have been run successfully1356 if (dependsOnMethods(testMethod)) {1357 ITestNGMethod[] methods =1358 MethodHelper.findDependedUponMethods(testMethod, allTestMethods);1359 if (!haveBeenRunSuccessfully(testMethod, methods)) {1360 return "Method " + testMethod + " depends on not successfully finished methods";1361 }1362 }1363 return null;1364 }1365 /**1366 * @return the test results that apply to one of the instances of the testMethod.1367 */1368 private Set<ITestResult> keepSameInstances(ITestNGMethod method, Set<ITestResult> results) {1369 Set<ITestResult> result = Sets.newHashSet();1370 for (ITestResult r : results) {1371 final Object o = method.getInstance();1372 // Keep this instance if 1) It's on a different class or 2) It's on the same class1373 // and on the same instance1374 Object instance = r.getInstance() != null1375 ? r.getInstance() : r.getMethod().getInstance();1376 if (r.getTestClass() != method.getTestClass() || instance == o) result.add(r);1377 }1378 return result;1379 }1380 /**1381 * @return true if all the methods have been run successfully1382 */1383 private boolean haveBeenRunSuccessfully(ITestNGMethod testMethod, ITestNGMethod[] methods) {1384 // Make sure the method has been run successfully1385 for (ITestNGMethod method : methods) {1386 Set<ITestResult> results = keepSameInstances(testMethod, m_notifier.getPassedTests(method));1387 Set<ITestResult> failedAndSkippedMethods = Sets.newHashSet();1388 failedAndSkippedMethods.addAll(m_notifier.getFailedTests(method));1389 failedAndSkippedMethods.addAll(m_notifier.getSkippedTests(method));1390 Set<ITestResult> failedresults = keepSameInstances(testMethod, failedAndSkippedMethods);1391 // If failed results were returned on the same instance, then these tests didn't pass1392 if (failedresults != null && failedresults.size() > 0) {1393 return false;1394 }1395 for (ITestResult result : results) {1396 if(!result.isSuccess()) {1397 return false;1398 }1399 }1400 }1401 return true;1402 }1403// private boolean containsInstance(Set<ITestResult> failedresults, Object[] instances) {1404// for (ITestResult tr : failedresults) {1405// for (Object o : instances) {1406// if (o == tr.getInstance()) {1407// return true;1408// }1409// }1410// }1411// return false;1412// }1413 /**1414 * An exception was thrown by the test, determine if this method1415 * should be marked as a failure or as failure_but_within_successPercentage1416 */1417 private void handleException(Throwable throwable,1418 ITestNGMethod testMethod,1419 ITestResult testResult,1420 int failureCount) {1421 if (throwable != null) {1422 testResult.setThrowable(throwable);1423 }1424 int successPercentage= testMethod.getSuccessPercentage();1425 int invocationCount= testMethod.getInvocationCount();1426 float numberOfTestsThatCanFail= ((100 - successPercentage) * invocationCount) / 100f;1427 if(failureCount < numberOfTestsThatCanFail) {1428 testResult.setStatus(ITestResult.SUCCESS_PERCENTAGE_FAILURE);1429 }1430 else {1431 testResult.setStatus(ITestResult.FAILURE);1432 }1433 }1434 static interface Predicate<K, T> {1435 boolean isTrue(K k, T v);1436 }1437 static class CanRunFromClassPredicate implements Predicate <ITestNGMethod, IClass> {1438 @Override1439 public boolean isTrue(ITestNGMethod m, IClass v) {1440 return m.canRunFromClass(v);1441 }1442 }1443 static class SameClassNamePredicate implements Predicate<ITestNGMethod, IClass> {1444 @Override1445 public boolean isTrue(ITestNGMethod m, IClass c) {1446 return c == null || m.getTestClass().getName().equals(c.getName());1447 }1448 }1449 /**1450 * @return Only the ITestNGMethods applicable for this testClass1451 */1452 private ITestNGMethod[] filterMethods(IClass testClass, ITestNGMethod[] methods,1453 Predicate<ITestNGMethod, IClass> predicate) {1454 List<ITestNGMethod> vResult= Lists.newArrayList();1455 for(ITestNGMethod tm : methods) {1456 if (predicate.isTrue(tm, testClass)) {1457 log(10, "Keeping method " + tm + " for class " + testClass);1458 vResult.add(tm);1459 } else {1460 log(10, "Filtering out method " + tm + " for class " + testClass);...

Full Screen

Full Screen

Source:MyReporterListener.java Github

copy

Full Screen

...201 int cq = 0;202 for (ITestNGMethod method : getMethodSet(tests, suite)) {203 m_row += 1;204 m_methodIndex += 1;205 ITestClass testClass = method.getTestClass();206 String className = testClass.getName();207 if (mq == 0) {208 String id = (m_testIndex == null ? null : "t"209 + Integer.toString(m_testIndex));210 titleRow(testname + " &#8212; " + style + details, 5, id);211 m_testIndex = null;212 }213 if (!className.equalsIgnoreCase(lastClassName)) {214 if (mq > 0) {215 cq += 1;216 m_out.print("<tr class=\"" + style217 + (cq % 2 == 0 ? "even" : "odd") + "\">"218 + "<td");219 if (mq > 1) {220 m_out.print(" rowspan=\"" + mq + "\"");221 }222 m_out.println(">" + lastClassName + "</td>" + buff);223 }224 mq = 0;225 buff.setLength(0);226 lastClassName = className;227 }228 Set<ITestResult> resultSet = tests.getResults(method);229 long end = Long.MIN_VALUE;230 long start = Long.MAX_VALUE;231 for (ITestResult testResult : tests.getResults(method))232 {233 if (testResult.getEndMillis() > end) 234 {235 end = testResult.getEndMillis();236 }237 if (testResult.getStartMillis() < start) 238 {239 start = testResult.getStartMillis();240 }241 }242 mq += 1;243 if (mq > 1) 244 {245 buff.append("<tr class=\"" + style246 + (cq % 2 == 0 ? "odd" : "even") + "\">");247 }248 String description = method.getDescription();249 String testInstanceName = resultSet250 .toArray(new ITestResult[] {})[0].getTestName();251 buff.append("<td><a href=\"#m"252 + m_methodIndex253 + "\">"254 + qualifiedName(method)255 + " "256 + (description != null && description.length() > 0 ? "(\""257 + description + "\")"258 : "")259 + "</a>"260 + (null == testInstanceName ? "" : "<br>("261 + testInstanceName + ")") + "</td>"262 + "<td class=\"numi\">" + resultSet.size() + "</td>"263 + "<td>" + start + "</td>" + "<td class=\"numi\">"264 + (end - start) + "</td>" + "</tr>");265 }266 if (mq > 0) {267 cq += 1;268 m_out.print("<tr class=\"" + style269 + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");270 if (mq > 1) {271 m_out.print(" rowspan=\"" + mq + "\"");272 }273 m_out.println(">" + lastClassName + "</td>" + buff);274 }275 }276 }277 278 /** Starts and defines columns result summary table */279 280 private void startResultSummaryTable(String style) 281 {282 tableStart(style, "summary");283 m_out.println("<tr><th>Class</th>"284 + "<th>Method</th><th># of<br/>Scenarios</th><th>Start</th><th>Time<br/>(ms)</th></tr>");285 m_row = 0;286 }287 private String qualifiedName(ITestNGMethod method) 288 {289 StringBuilder addon = new StringBuilder();290 String[] groups = method.getGroups();291 int length = groups.length;292 if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {293 addon.append("(");294 for (int i = 0; i < length; i++) {295 if (i > 0) {296 addon.append(", ");297 }298 addon.append(groups[i]);299 }300 addon.append(")");301 }302 return "<b>" + method.getMethodName() + "</b> " + addon;303 }304 305 private void resultDetail(IResultMap tests) {306 for (ITestResult result : tests.getAllResults()) {307 ITestNGMethod method = result.getMethod();308 m_methodIndex++;309 //String cname = method.getTestClass().getName();310 //m_out.println("<h2 id=\"m" + m_methodIndex + "\">" + cname + ":"311 // + method.getMethodName() + "</h2>");312 Set<ITestResult> resultSet = tests.getResults(method);313 generateForResult(result, method, resultSet.size());314 m_out.println("<hr>");315 m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>");316 }317 }318 319 private void generateForResult(ITestResult ans, ITestNGMethod method,320 int resultSetSize) {321 Object[] parameters = ans.getParameters();322 boolean hasParameters = parameters != null && parameters.length > 0;323 if (hasParameters) {324 tableStart("result", null);325 m_out.print("<tr class=\"param\">");326 for (int x = 1; x <= parameters.length; x++) {327 m_out.print("<th>Param." + x + "</th>");328 }329 m_out.println("</tr>");330 m_out.print("<tr class=\"param stripe\">");331 for (Object p : parameters) {332 m_out.println("<td>" + Utils.escapeHtml(Utils.toString(p))333 + "</td>");334 }335 m_out.println("</tr>");336 }337 List<String> msgs = Reporter.getOutput(ans);338 boolean hasReporterOutput = msgs.size() > 0;339 340 341 Throwable exception = ans.getThrowable();342 boolean hasThrowable = exception != null;343 if (hasReporterOutput || hasThrowable) {344 if (hasParameters) {345 m_out.print("<tr><td");346 if (parameters.length > 1) {347 m_out.print(" colspan=\"" + parameters.length + "\"");348 }349 m_out.println(">");350 } else {351 m_out.println("<div>");352 }353 if (hasReporterOutput) {354 if (hasThrowable) {355 //m_out.println("<h3>Test Messages</h3>");356 }357 for (String line : msgs) {358 m_out.println(line + "<br/>");359 }360 }361 if (hasThrowable) {362 boolean wantsMinimalOutput = ans.getStatus() == ITestResult.SUCCESS;363 if (hasReporterOutput) {364 m_out.println("<h3>"365 + (wantsMinimalOutput ? "Expected Exception"366 : "Failure") + "</h3>");367 }368 generateExceptionReport(exception, method);369 }370 if (hasParameters) {371 m_out.println("</td></tr>");372 } else {373 m_out.println("</div>");374 }375 }376 if (hasParameters) {377 m_out.println("</table>");378 }379 }380 381 protected void generateExceptionReport(Throwable exception,382 ITestNGMethod method) {383 m_out.print("<div class=\"stacktrace\">");384 385 String str=Utils.stackTrace(exception, true)[0];386 scanner = new Scanner(str);387 String firstLine = scanner.nextLine();388 m_out.println(firstLine);389 m_out.println("</div>");390 }391 /**392 * Since the methods will be sorted chronologically, we want to return the393 * ITestNGMethod from the invoked methods.394 */395 396 private Collection<ITestNGMethod> getMethodSet(IResultMap tests, ISuite suite) {397 List<IInvokedMethod> r = Lists.newArrayList();398 List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();399 for (IInvokedMethod im : invokedMethods) {400 if (tests.getAllMethods().contains(im.getTestMethod())) {401 r.add(im);402 }403 }404 Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new TestSorter());405 List<ITestNGMethod> result = Lists.newArrayList();406 // Add all the invoked methods407 for (IInvokedMethod m : r) {408 result.add(m.getTestMethod());409 }410 // Add all the methods that weren't invoked (e.g. skipped) that we411 // haven't added yet412 for (ITestNGMethod m : tests.getAllMethods()) {413 if (!result.contains(m)) {414 result.add(m);415 }416 }417 return result;418 }419 420 @SuppressWarnings("unused")421 public void generateSuiteSummaryReport(List<ISuite> suites) {422 tableStart("testOverview", "summary");423 m_out.print("<tr>"); 424 tableColumnStart("Test CaseID");425 tableColumnStart("Test Scenarios");426 tableColumnStart("Result");427 tableColumnStart("Start Time-<br/>End Time");428 tableColumnStart("Total<br/>Time");429 tableColumnStart("");430 m_out.println("</tr>");431 NumberFormat formatter = new DecimalFormat("#,##0.0");432 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yy hh:mm:ss a");433 int qty_tests = 0;434 int qty_pass_m = 0;435 int qty_pass_s = 0;436 int qty_skip = 0;437 int qty_fail = 0;438 int total_pass_s=0;439 int total_fail=0;440 int total_skip=0;441 long time_start = Long.MAX_VALUE;442 long time_end = Long.MIN_VALUE;443 m_testIndex = 1;444 for (ISuite suite : suites)445 {446 if (suites.size() >= 1) 447 {448 titleRow(suite.getName(), 7);449 }450 Map<String, ISuiteResult> tests = suite.getResults();451 for (ISuiteResult r : tests.values()) 452 {453 qty_tests += 1;454 455 456 //Getting Method Name457 ITestContext overview = r.getTestContext();458 459 MethodDetails MD= (MethodDetails) details.get(overview.getName());460 System.out.println("md : "+overview.getName());461 startSummaryRow(MD.getTestCaseID()); 462 printTestCaseName(overview.getName());463 464 //get Passed Methods Number465 int q = getMethodSet(overview.getPassedTests(), suite).size();466 /* qty_pass_m += q;467 summaryCell(q, Integer.MAX_VALUE);*/468 //Get passed Tests Number469 q = overview.getPassedTests().size();470 qty_pass_s += q;471 // summaryCell(q, Integer.MAX_VALUE);472 //Get Skipped Tests Number 473 q = getMethodSet(overview.getSkippedTests(), suite).size();474 qty_skip += q;475 // summaryCell(q, 0);476 //Get Failed Tests Number477 q = getMethodSet(overview.getFailedTests(), suite).size();478 qty_fail += q;479 //summaryCell(q, 0);480 if(qty_pass_s>0)481 {482 summaryCell("<center><label style='color:green;'>Passed</label></center>");483 total_pass_s+=qty_pass_s;484 qty_pass_s=0;485 486 }487 else if(qty_fail>0)488 {489 summaryCell("<center><label style='color:red;'>Failed</label></center>");490 total_fail+=qty_fail;491 qty_fail=0;492 }493 else if(qty_skip>0)494 {495 summaryCell("<center><label style='color:yellow;'>Skipped</label></center>");496 total_skip+=qty_skip;497 qty_skip=0;498 }499 500 // NEW ----DateFunctions.dateToDayAndTime(overview.getStartDate())501 502 summaryCell(503 df.format(overview.getStartDate()).toString()+" -- "+df.format(overview.getEndDate()).toString(),504 true);505 m_out.println("</td>");506 507 m_out.println("</td>");508 time_start = Math.min(overview.getStartDate().getTime(),509 time_start);510 time_end = Math.max(overview.getEndDate().getTime(), time_end);511 summaryCelltotal(512 formatter.format((overview.getEndDate().getTime() - overview513 .getStartDate().getTime()) / 1000.)514 + " seconds", true);515 516 m_out.println("</tr>");517 m_testIndex++;518 }519 }520 if (qty_tests >=1) {521 m_out.println("<tr class=\"total\"><td>Total</td>");522 m_out.println("<td>Passed:"+total_pass_s+"<br/>Failed:"+total_fail+"<br/>Skipped:"+total_skip+"</td>");523 summaryCell(" ", true);524 summaryCell(" ", true);525 summaryCell(" ", true);526 pass=total_pass_s;527 fail=total_fail;528 skip=total_skip;529 System.out.println("pass test cases:-"+pass);530 System.out.println("fail test cases:-"+fail);531 summaryCellEnd(formatter.format(((time_end - time_start) / 1000.) / 60.)+ " minutes", true);532 }533 m_out.println("</table>");534 }535 private void summaryCell(String val) {536 /* StringBuffer b = new StringBuffer();537 for (String v : val) {538 b.append(v + " ");539 }*/540 summaryCelltotal(val, true);541 }542 private void summaryCell(String v, boolean isgood) {543 m_out.print("<td class=\"numi" + (isgood ? "" : "_attn") + "\">" + v544 + "</td>");545 }546 private void summaryCelltotal(String v, boolean isgood) {547 m_out.print("<td class=\"numi2" + (isgood ? "" : "_attn") + "\">" + v548 + "</td>");549 }550 private void summaryCellEnd(String v, boolean isgood) {551 m_out.print("<td colspan=2 class=\"numi" + (isgood ? "" : "_attn") + "\"><center>" + v552 + "</center></td>");553 }554 private void startSummaryRow(String label) {555 m_row += 1;556 m_out.print("<tr"557 + (m_row % 2 == 0 ? " class=\"stripe\"" : "")558 + "><td width='5%'>" + label + "</td>");559 }560 private void printTestCaseName(String label) {561 m_row += 1;562 m_out.print("<td style=\"text-align:left;padding-right:2em;width:20%\"><a href='#"+label+"'>" + label + "</a>" + "</td>");563 }564 565 /* private void summaryCell(int v, int maxexpected) {566 summaryCell(String.valueOf(v), v <= maxexpected);567 }*/568 private void tableStart(String cssclass, String id) {569 m_out.println("<table cellspacing=\"0\" cellpadding=\"0\""570 + (cssclass != null ? " class=\"" + cssclass + "\""571 : " style=\"padding-bottom:2em\"")572 + (id != null ? " id=\"" + id + "\"" : "") + ">");573 m_row = 0;574 }575 private void tableColumnStart(String label) {576 m_out.print("<th>" + label + "</th>");577 }578 private void titleRow(String label, int cq) {579 titleRow(label, cq, null);580 }581 private void titleRow(String label, int cq, String id) {582 m_out.print("<tr");583 if (id != null) {584 m_out.print(" id=\"" + id + "\"");585 }586 m_out.println("><th colspan=\"" + cq + "\">" + label + "</th></tr>");587 m_row = 0;588 }589 /** Starts HTML stream */590 protected void startHtml(PrintWriter out) {591 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");592 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");593 out.println("<head>");594 out.println("<title>TestNG Report</title>");595 out.println("<style type=\"text/css\">");596 out.println("table {margin-bottom:10px;border-collapse:collapse;empty-cells:show}");597 out.println("td,th {border:1px solid #009;padding:.25em .5em}");598 out.println(".result th {vertical-align:bottom}");599 out.println(".param th {padding-left:1em;padding-right:1em}");600 out.println(".param td {padding-left:.5em;padding-right:2em}");601 out.println(".stripe td,.stripe th {background-color: #E6EBF9}");602 out.println(".numi,.numi_attn {width : 18%}");603 out.println(".numi2,.numi2_attn {width : 8%}");604 out.println(".total td {font-weight:bold}");605 out.println(".passedodd td {background-color: #0A0}");606 out.println(".passedeven td {background-color: #3F3}");607 out.println(".skippedodd td {background-color: #CCC}");608 out.println(".skippedodd td {background-color: #DDD}");609 out.println(".failedodd td,.numi_attn {background-color: #F33}");610 out.println(".failedeven td,.stripe .numi_attn {background-color: #D00}");611 out.println(".stacktrace {white-space:pre;font-family:monospace}");612 out.println(".totop {font-size:60%;text-align:center;border-bottom:2px solid #000}");613 out.println("</style>");614 out.println("</head>");615 out.println("<body>");616 }617 /** Finishes HTML stream */618 protected void endHtml(PrintWriter out) {619 out.println("<center> Report Customized By Jay Mukul </center>");620 out.println("</body></html>");621 }622 // ~ Inner Classes --------------------------------------------------------623 /** Arranges methods by classname and method name */624 private class TestSorter implements Comparator<IInvokedMethod> {625 // ~ Methods626 // -------------------------------------------------------------627 /** Arranges methods by classname and method name */628 public int compare(IInvokedMethod o1, IInvokedMethod o2) {629 // System.out.println("Comparing " + o1.getMethodName() + " " +630 // o1.getDate()631 // + " and " + o2.getMethodName() + " " + o2.getDate());632 return (int) (o1.getDate() - o2.getDate());633 // int r = ((T) o1).getTestClass().getName().compareTo(((T)634 // o2).getTestClass().getName());635 // if (r == 0) {636 // r = ((T) o1).getMethodName().compareTo(((T) o2).getMethodName());637 // }638 // return r;639 }640 }641}...

Full Screen

Full Screen

Source:IDEATestNGRemoteListener.java Github

copy

Full Screen

...53 break;54 }55 }56 if (!found) {57 final String fullEscapedMethodName = escapeName(getShortName(method.getTestClass().getName()) + "/" + method.getMethodName());58 myPrintStream.println("##teamcity[testStarted name=\'" + fullEscapedMethodName + "\']");59 myPrintStream.println("##teamcity[testIgnored name=\'" + fullEscapedMethodName + "\']");60 myPrintStream.println("##teamcity[testFinished name=\'" + fullEscapedMethodName + "\']");61 break;62 }63 }64 }65 }66 }67 catch (NoSuchMethodError ignored) {}68 for (int i = myCurrentSuites.size() - 1; i >= 0; i--) {69 onSuiteFinish(myCurrentSuites.remove(i));70 }71 myCurrentSuites.clear();72 }73 public synchronized void onConfigurationSuccess(ITestResult result, boolean start) {74 final DelegatedResult delegatedResult = createDelegated(result);75 if (start) {76 onConfigurationStart(delegatedResult);77 }78 onConfigurationSuccess(delegatedResult);79 }80 public synchronized void onConfigurationFailure(ITestResult result, boolean start) {81 final DelegatedResult delegatedResult = createDelegated(result);82 if (start) {83 onConfigurationStart(delegatedResult);84 }85 onConfigurationFailure(delegatedResult);86 }87 public synchronized void onConfigurationSkip(ITestResult itr) {}88 public synchronized void onTestStart(ITestResult result) {89 onTestStart(createDelegated(result));90 }91 public synchronized void onTestSuccess(ITestResult result) {92 onTestFinished(createDelegated(result));93 }94 public synchronized void onTestFailure(ITestResult result) {95 onTestFailure(createDelegated(result));96 }97 public synchronized void onTestSkipped(ITestResult result) {98 onTestSkipped(createDelegated(result));99 }100 public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) {101 final Throwable throwable = result.getThrowable();102 if (throwable != null) {103 throwable.printStackTrace();104 }105 onTestSuccess(result);106 }107 public synchronized void onStart(ITestContext context) {}108 public synchronized void onFinish(ITestContext context) {}109 public void onTestStart(ExposedTestResult result) {110 onStartWithParameters(result, false);111 }112 public void onStartWithParameters(ExposedTestResult result, boolean config) {113 final Object[] parameters = result.getParameters();114 final String qualifiedName = result.getClassName() + result.getDisplayMethodName();115 Integer invocationCount = myInvocationCounts.get(qualifiedName);116 if (invocationCount == null) {117 invocationCount = 0;118 }119 Integer normalizedIndex = normalizeInvocationCountInsideIncludedMethods(invocationCount, result);120 final String paramString = getParamsString(parameters, config, normalizedIndex);121 onTestStart(result, paramString, normalizedIndex, config);122 myInvocationCounts.put(qualifiedName, invocationCount + 1);123 }124 private static Integer normalizeInvocationCountInsideIncludedMethods(Integer invocationCount, ExposedTestResult result) {125 List<Integer> includeMethods = result.getIncludeMethods();126 if (includeMethods == null || invocationCount >= includeMethods.size()) {127 return invocationCount;128 }129 return includeMethods.get(invocationCount);130 }131 public void onConfigurationStart(ExposedTestResult result) {132 onStartWithParameters(result, true);133 }134 public void onConfigurationSuccess(ExposedTestResult result) {135 onTestFinished(result);136 }137 public void onConfigurationFailure(ExposedTestResult result) {138 onTestFailure(result);139 }140 141 public boolean onSuiteStart(String classFQName, boolean provideLocation) {142 return onSuiteStart(Collections.singletonList(classFQName), null, provideLocation);143 }144 public boolean onSuiteStart(List<String> parentsHierarchy, ExposedTestResult result, boolean provideLocation) {145 int idx = 0;146 String currentClass;147 String currentParent;148 while (idx < myCurrentSuites.size() && idx < parentsHierarchy.size()) {149 currentClass = myCurrentSuites.get(idx);150 currentParent =parentsHierarchy.get(parentsHierarchy.size() - 1 - idx);151 if (!currentClass.equals(getShortName(currentParent))) break;152 idx++;153 }154 for (int i = myCurrentSuites.size() - 1; i >= idx; i--) {155 currentClass = myCurrentSuites.remove(i);156 myPrintStream.println("##teamcity[testSuiteFinished name=\'" + escapeName(currentClass) + "\']");157 }158 for (int i = idx; i < parentsHierarchy.size(); i++) {159 String fqName = parentsHierarchy.get(parentsHierarchy.size() - 1 - i);160 String currentClassName = getShortName(fqName);161 String location = "java:suite://" + escapeName(fqName);162 if (result != null) {163 final String testName = result.getXmlTestName();164 if (fqName.equals(testName)) {165 final String fileName = result.getFileName();166 if (fileName != null) {167 location = "file://" + fileName;168 }169 }170 }171 myPrintStream.println("\n##teamcity[testSuiteStarted name =\'" + escapeName(currentClassName) +172 (provideLocation ? "\' locationHint = \'" + location : "") + "\']");173 myCurrentSuites.add(currentClassName);174 }175 return false;176 }177 public void onSuiteFinish(String suiteName) {178 myPrintStream.println("##teamcity[testSuiteFinished name=\'" + escapeName(suiteName) + "\']");179 }180 private void onTestStart(ExposedTestResult result, String paramString, Integer invocationCount, boolean config) {181 myParamsMap.put(result, paramString);182 onSuiteStart(result.getTestHierarchy(), result, true);183 final String className = result.getClassName();184 final String methodName = result.getDisplayMethodName();185 final String location = className + "/" + result.getMethodName() + (invocationCount >= 0 ? "[" + invocationCount + "]" : "");186 myPrintStream.println("\n##teamcity[testStarted name=\'" + escapeName(getShortName(className) + "." + methodName + (paramString != null ? paramString : "")) +187 "\' locationHint=\'java:test://" + escapeName(location) + (config ? "\' config=\'true" : "") + "\']");188 }189 public void onTestFailure(ExposedTestResult result) {190 if (!myParamsMap.containsKey(result)) {191 onTestStart(result);192 }193 Throwable ex = result.getThrowable();194 String methodName = getTestMethodNameWithParams(result);195 final Map<String, String> attrs = new LinkedHashMap<String, String>();196 attrs.put("name", methodName);197 final String failureMessage = ex != null ? ex.getMessage() : null;198 if (ex != null) {199 ComparisonFailureData notification;200 try {201 notification = ComparisonFailureData.create(ex);202 }203 catch (Throwable e) {204 notification = null;205 }206 if (notification == null) {207 try {208 notification = TestNGExpectedPatterns.createExceptionNotification(failureMessage);209 }210 catch (Throwable e) {211 notification = null;212 }213 }214 ComparisonFailureData.registerSMAttributes(notification, getTrace(ex), failureMessage, attrs, ex);215 }216 else {217 attrs.put("message", "");218 }219 myPrintStream.println();220 myPrintStream.println(MapSerializerUtil.asString("testFailed", attrs));221 onTestFinished(result);222 }223 public void onTestSkipped(ExposedTestResult result) {224 if (!myParamsMap.containsKey(result)) {225 onTestStart(result);226 mySkipped++;227 }228 myPrintStream.println("\n##teamcity[testIgnored name=\'" + escapeName(getTestMethodNameWithParams(result)) + "\']");229 onTestFinished(result);230 }231 public void onTestFinished(ExposedTestResult result) {232 final long duration = result.getDuration();233 myPrintStream.println("\n##teamcity[testFinished name=\'" +234 escapeName(getTestMethodNameWithParams(result)) +235 (duration > 0 ? "\' duration=\'" + duration : "") +236 "\']");237 }238 private synchronized String getTestMethodNameWithParams(ExposedTestResult result) {239 String methodName = getShortName(result.getClassName()) + "." + result.getDisplayMethodName();240 String paramString = myParamsMap.get(result);241 if (paramString != null) {242 methodName += paramString;243 }244 return methodName;245 }246 private static String getParamsString(Object[] parameters, boolean config, int invocationCount) {247 String paramString = "";248 if (parameters.length > 0) {249 if (config) {250 Object parameter = parameters[0];251 if (parameter != null) {252 Class<?> parameterClass = parameter.getClass();253 if (ITestResult.class.isAssignableFrom(parameterClass) || ITestContext.class.isAssignableFrom(parameterClass) || Method.class.isAssignableFrom(parameterClass)) {254 try {255 paramString = "[" + parameterClass.getMethod("getName").invoke(parameter) + "]";256 }257 catch (Throwable e) {258 paramString = "";259 }260 }261 else {262 paramString = "[" + parameter.toString() + "]";263 }264 }265 }266 else {267 paramString = Arrays.deepToString(parameters);268 }269 }270 if (invocationCount > 0) {271 paramString += " (" + invocationCount + ")";272 }273 return paramString.length() > 0 ? paramString : null;274 }275 protected String getTrace(Throwable tr) {276 StringWriter stringWriter = new StringWriter();277 PrintWriter writer = new PrintWriter(stringWriter);278 tr.printStackTrace(writer);279 StringBuffer buffer = stringWriter.getBuffer();280 return buffer.toString();281 }282 protected static String getShortName(String fqName) {283 int lastPointIdx = fqName.lastIndexOf('.');284 if (lastPointIdx >= 0) {285 return fqName.substring(lastPointIdx + 1);286 }287 return fqName;288 }289 private static String escapeName(String str) {290 return MapSerializerUtil.escapeStr(str, MapSerializerUtil.STD_ESCAPER);291 }292 public interface ExposedTestResult {293 Object[] getParameters();294 String getMethodName();295 String getDisplayMethodName();296 String getClassName();297 long getDuration();298 List<String> getTestHierarchy();299 String getFileName();300 String getXmlTestName();301 Throwable getThrowable();302 List<Integer> getIncludeMethods();303 }304 protected DelegatedResult createDelegated(ITestResult result) {305 final DelegatedResult newResult = new DelegatedResult(result);306 final DelegatedResult oldResult = myResults.get(newResult);307 if (oldResult != null) {308 return oldResult;309 }310 myResults.put(newResult, newResult);311 return newResult;312 }313 314 protected static class DelegatedResult implements ExposedTestResult {315 private final ITestResult myResult;316 private final String myTestName;317 public DelegatedResult(ITestResult result) {318 myResult = result;319 myTestName = calculateDisplayName();320 }321 //workaround for https://github.com/cbeust/testng/issues/1944322 private String calculateDisplayName() {323 String name = myResult.getTestName();324 if (name != null && !name.equals(myResult.getTestClass().getTestName())) {325 return name;326 }327 ITestNGMethod method = myResult.getMethod();328 ConstructorOrMethod constructorOrMethod = method.getConstructorOrMethod();329 AccessibleObject member = null;330 if (constructorOrMethod.getMethod() != null) {331 member = constructorOrMethod.getMethod();332 }333 if (constructorOrMethod.getConstructor() != null) {334 member = constructorOrMethod.getConstructor();335 }336 if (member == null) return method.getMethodName();337 Test annotation = member.getAnnotation(Test.class);338 if (annotation == null) return method.getMethodName();339 String testNameFromAnnotation = annotation.testName();340 return testNameFromAnnotation == null || testNameFromAnnotation.length() == 0 ? method.getMethodName() : testNameFromAnnotation;341 }342 public Object[] getParameters() {343 return myResult.getParameters();344 }345 public String getMethodName() {346 return myResult.getMethod().getMethodName();347 }348 public String getDisplayMethodName() {349 return myTestName;350 }351 public String getClassName() {352 return myResult.getMethod().getTestClass().getName();353 }354 public long getDuration() {355 return myResult.getEndMillis() - myResult.getStartMillis();356 }357 public List<String> getTestHierarchy() {358 final List<String> hierarchy;359 final XmlTest xmlTest = myResult.getTestClass().getXmlTest();360 if (xmlTest != null) {361 hierarchy = Arrays.asList(getClassName(), xmlTest.getName());362 } else {363 hierarchy = Collections.singletonList(getClassName());364 }365 return hierarchy;366 }367 public String getFileName() {368 final XmlTest xmlTest = myResult.getTestClass().getXmlTest();369 return xmlTest != null ? xmlTest.getSuite().getFileName() : null;370 }371 public String getXmlTestName() {372 final XmlTest xmlTest = myResult.getTestClass().getXmlTest();373 return xmlTest != null ? xmlTest.getName() : null;374 }375 public Throwable getThrowable() {376 return myResult.getThrowable();377 }378 public List<Integer> getIncludeMethods() {379 IClass testClass = myResult.getTestClass();380 if (testClass == null) return null;381 XmlClass xmlClass = testClass.getXmlClass();382 if (xmlClass == null) return null;383 List<XmlInclude> includedMethods = xmlClass.getIncludedMethods();384 if (includedMethods.isEmpty()) return null;385 return includedMethods.get(0).getInvocationNumbers();386 }387 @Override388 public boolean equals(Object o) {389 if (this == o) return true;390 if (o == null || getClass() != o.getClass()) return false;391 return myResult.equals(((DelegatedResult)o).myResult);392 }393 @Override...

Full Screen

Full Screen

Source:Listener.java Github

copy

Full Screen

...74 }75 // This is the method which will be executed in case of test pass or fail76 // This will provide the information on the test77 private void printTestResults(ITestResult result) {78 Reporter.log("Test Method resides in " + result.getTestClass().getName(), true);79 if (result.getParameters().length != 0) {80 String params = null;81 for (Object parameter : result.getParameters()) {82 params += parameter.toString() + ",";83 }84 Reporter.log("Test Method had the following parameters : " + params, true);85 }86 String status = null;87 switch (result.getStatus()) {88 case ITestResult.SUCCESS:89 status = "Pass";90 break;91 case ITestResult.FAILURE:92 status = "Failed";...

Full Screen

Full Screen

Source:ITestResult.java Github

copy

Full Screen

...36 public void setParameters(Object[] parameters);37 /**38 * @return The test class used this object is a result for.39 */40 public IClass getTestClass();41 /**42 * @return The throwable that was thrown while running the43 * method, or null if no exception was thrown.44 */45 public Throwable getThrowable();46 public void setThrowable(Throwable throwable);47 /**48 * @return the start date for this test, in milliseconds.49 */50 public long getStartMillis();51 /**52 * @return the end date for this test, in milliseconds.53 */54 public long getEndMillis();...

Full Screen

Full Screen

getTestClass

Using AI Code Generation

copy

Full Screen

1 ITestNGMethod[] tests = getTestClass().getTestMethods();2 for (ITestNGMethod test : tests) {3 methods.add(test.getMethodName());4 }5 return methods.toArray(new String[methods.size()]);6 }7}

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.

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful