org.testng.ITestNGMethod Java Examples

The following examples show how to use org.testng.ITestNGMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VerboseReporter.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Print out test summary
 */
private void logResults() {
    //
    // Log test summary
    //
    ITestNGMethod[] ft = resultsToMethods(getFailedTests());
    StringBuilder sb = new StringBuilder("\n===============================================\n");
    sb.append("    ").append(suiteName).append("\n");
    sb.append("    Tests run: ").append(Utils.calculateInvokedMethodCount(getAllTestMethods()));
    sb.append(", Failures: ").append(Utils.calculateInvokedMethodCount(ft));
    sb.append(", Skips: ").append(Utils.calculateInvokedMethodCount(resultsToMethods(getSkippedTests())));
    int confFailures = getConfigurationFailures().size();
    int confSkips = getConfigurationSkips().size();
    if (confFailures > 0 || confSkips > 0) {
        sb.append("\n").append("    Configuration Failures: ").append(confFailures);
        sb.append(", Skips: ").append(confSkips);
    }
    sb.append("\n===============================================");
    log(sb.toString());
}
 
Example #2
Source File: ExpectedSkipManager.java    From carina with Apache License 2.0 6 votes vote down vote up
/**
 * Collect rules based on tests and its context
 * 
 * @param testMethod
 * @param context
 * @return rules list
 */
private List<Class<? extends IRule>> collectRules(Method testMethod, ITestContext context) {
    List<Class<? extends IRule>> rules = new ArrayList<>();
    // collect rules from current class and method
    ExpectedSkip classSkipAnnotation = testMethod.getDeclaringClass().getAnnotation(ExpectedSkip.class);
    ExpectedSkip methodSkipAnnotation = testMethod.getAnnotation(ExpectedSkip.class);
    rules.addAll(getRulesFromAnnotation(classSkipAnnotation));
    rules.addAll(getRulesFromAnnotation(methodSkipAnnotation));

    // analyze all dependent methods and collect rules
    ITestNGMethod[] methods = context.getAllTestMethods();
    for (ITestNGMethod iTestNGMethod : methods) {
        if (iTestNGMethod.getMethodName().equalsIgnoreCase(testMethod.getName())) {
            String[] methodsDep = iTestNGMethod.getMethodsDependedUpon();
            for (String method : methodsDep) {
                rules.addAll(getDependentMethodsRules(method));
            }
        }
    }

    return rules;
}
 
Example #3
Source File: PlatformInterceptor.java    From Selenium-Foundation with Apache License 2.0 6 votes vote down vote up
/**
 * Add the specified method to the method list
 *
 * @param methodList list of methods that are about to be run
 * @param testMethod method to be added to the list
 * @param  platformConstant target platform on which to run this method
 */
@SuppressWarnings("unchecked")
private static <P extends Enum<?> & PlatformEnum> void addMethodForPlatform(List<IMethodInstance> methodList, IMethodInstance testMethod, PlatformEnum platformConstant) {
    if (platformConstant != null) {
        ITestNGMethod method = testMethod.getMethod();
        PlatformIdentity<P> identity = new PlatformIdentity<>((P) platformConstant, method.getDescription());
        testMethod.getMethod().setDescription(DataUtils.toString(identity));
    }
    methodList.add(testMethod);
}
 
Example #4
Source File: AllureTestNg.java    From allure-java with Apache License 2.0 6 votes vote down vote up
@Override
public void onStart(final ITestContext context) {
    final String parentUuid = getUniqueUuid(context.getSuite());
    final String uuid = getUniqueUuid(context);
    final TestResultContainer container = new TestResultContainer()
            .setUuid(uuid)
            .setName(context.getName())
            .setStart(System.currentTimeMillis());
    getLifecycle().startTestContainer(parentUuid, container);

    Stream.of(context.getAllTestMethods())
            .map(ITestNGMethod::getTestClass)
            .distinct()
            .forEach(this::onBeforeClass);

    context.getExcludedMethods().stream()
            .filter(ITestNGMethod::isTest)
            .filter(method -> !method.getEnabled())
            .forEach(method -> createFakeResult(context, method));
}
 
Example #5
Source File: EverrestJetty.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
public void onStart(ITestContext context) {

        ITestNGMethod[] allTestMethods = context.getAllTestMethods();
        if (allTestMethods == null) {
            return;
        }
        if (httpServer == null && hasEverrestJettyListenerTestHierarchy(allTestMethods)) {
            httpServer = new JettyHttpServer();

            context.setAttribute(JETTY_PORT, httpServer.getPort());
            context.setAttribute(JETTY_SERVER, httpServer);

            try {
                httpServer.start();
                httpServer.resetFactories();
                httpServer.resetFilter();
                RestAssured.port = httpServer.getPort();
                RestAssured.basePath = JettyHttpServer.UNSECURE_REST;
            } catch (Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
                throw new RuntimeException(e.getLocalizedMessage(), e);
            }
        }
    }
 
Example #6
Source File: FirstLastTestExecutionBehaviour.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
    if (!isThisSuiteListener(testResult)) {
        return;
    }
    if (method.isTestMethod()) {
        ITestNGMethod thisMethod = method.getTestMethod();
        ITestNGMethod[] allTestMethods = testResult.getTestContext().getAllTestMethods();
        ITestNGMethod firstMethod = allTestMethods[0];
        ITestNGMethod lastMethod = allTestMethods[allTestMethods.length - 1];

        if (!thisMethod.equals(firstMethod) && !thisMethod.equals(lastMethod)) {
            IResultMap success = testResult.getTestContext().getPassedTests();
            if (!success.getAllMethods().contains(firstMethod)) {
                throw new SkipException("Skipped because first method was not succcessfull");
            }
        }
    }
}
 
Example #7
Source File: SeleniumTestHandler.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Skip test if preceding test with higher priority from the same test class has failed.
 *
 * @param result holds result of test execution
 * @throws SkipException if test should be skipped
 */
private void skipTestIfNeeded(ITestResult result) {
  ITestNGMethod testMethodToSkip = result.getMethod();

  ITestResult failedTestResult =
      testsWithFailure.get(testMethodToSkip.getInstance().getClass().getName());

  // skip test with lower priority value and if it shouldn't always run
  if (failedTestResult != null
      && testMethodToSkip.getPriority() > failedTestResult.getMethod().getPriority()
      && !testMethodToSkip.isAlwaysRun()) {
    throw new SkipException(
        format(
            "Skipping test %s because it depends on test %s which has failed earlier.",
            getStartingTestLabel(testMethodToSkip),
            getCompletedTestLabel(failedTestResult.getMethod())));
  }
}
 
Example #8
Source File: AllureTestNg.java    From allure-java with Apache License 2.0 6 votes vote down vote up
@Override
public void afterInvocation(final IInvokedMethod method, final ITestResult testResult) {
    final ITestNGMethod testMethod = method.getTestMethod();
    if (isSupportedConfigurationFixture(testMethod)) {
        final String executableUuid = currentExecutable.get();
        currentExecutable.remove();
        if (testResult.isSuccess()) {
            getLifecycle().updateFixture(executableUuid, result -> result.setStatus(Status.PASSED));
        } else {
            getLifecycle().updateFixture(executableUuid, result -> result
                    .setStatus(getStatus(testResult.getThrowable()))
                    .setStatusDetails(getStatusDetails(testResult.getThrowable()).orElse(null)));
        }
        getLifecycle().stopFixture(executableUuid);

        if (testMethod.isBeforeMethodConfiguration() || testMethod.isAfterMethodConfiguration()) {
            final String containerUuid = currentTestContainer.get();
            validateContainerExists(getQualifiedName(testMethod), containerUuid);
            currentTestContainer.remove();
            getLifecycle().stopTestContainer(containerUuid);
            getLifecycle().writeTestContainer(containerUuid);
        }
    }
}
 
Example #9
Source File: AllureTestNg.java    From allure-java with Apache License 2.0 6 votes vote down vote up
private List<Label> getLabels(final ITestNGMethod method, final IClass iClass) {
    final List<Label> labels = new ArrayList<>();
    getMethod(method)
            .map(AnnotationUtils::getLabels)
            .ifPresent(labels::addAll);
    getClass(iClass)
            .map(AnnotationUtils::getLabels)
            .ifPresent(labels::addAll);

    getMethod(method)
            .map(this::getSeverity)
            .filter(Optional::isPresent)
            .orElse(getClass(iClass).flatMap(this::getSeverity))
            .map(ResultsUtils::createSeverityLabel)
            .ifPresent(labels::add);
    return labels;
}
 
Example #10
Source File: PowerEmailableReporter.java    From WebAndAppUITesting with GNU General Public License v3.0 6 votes vote down vote up
private String qualifiedName(ITestNGMethod method) {
	StringBuilder addon = new StringBuilder();
	String[] groups = method.getGroups();
	int length = groups.length;
	if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {
		addon.append("(");
		for (int i = 0; i < length; i++) {
			if (i > 0) {
				addon.append(", ");
			}
			addon.append(groups[i]);
		}
		addon.append(")");
	}

	return "<b>" + method.getMethodName() + "</b> " + addon;
}
 
Example #11
Source File: BaseTest.java    From video-recorder-java with MIT License 6 votes vote down vote up
protected ITestResult prepareMock(Class<?> tClass, Method method) {
  ITestResult result = mock(ITestResult.class);
  IClass clazz = mock(IClass.class);
  ITestNGMethod testNGMethod = mock(ITestNGMethod.class);
  ConstructorOrMethod cm = mock(ConstructorOrMethod.class);
  String methodName = method.getName();
  when(result.getTestClass()).thenReturn(clazz);
  when(result.getTestClass().getRealClass()).thenReturn(tClass);
  when(clazz.getName()).thenReturn(this.getClass().getName());
  when(result.getMethod()).thenReturn(testNGMethod);
  when(cm.getMethod()).thenReturn(method);
  when(result.getMethod().getConstructorOrMethod()).thenReturn(cm);
  when(testNGMethod.getMethodName()).thenReturn(methodName);
  ITestContext context = mock(ITestContext.class);
  when(result.getTestContext()).thenReturn(context);
  XmlTest xmlTest = new XmlTest();
  XmlSuite suite = new XmlSuite();
  xmlTest.setXmlSuite(suite);
  suite.setListeners(Arrays.asList(VideoListener.class.getName()));
  when(context.getCurrentXmlTest()).thenReturn(xmlTest);
  return result;
}
 
Example #12
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 6 votes vote down vote up
private boolean isInActiveGroupWithIncludes(ITestNGMethod method, List<String> includeGroups, List<String> excludeGroups) {
    if (method.getGroups() != null) {
        boolean included = false;
        boolean excluded = false;
        for (String group : method.getGroups()) {
            if (includeGroups.contains(group)) {
                included = true;
            }
            if (excludeGroups.contains(group)) {
                excluded = true;
            }
        }
        if (included && !excluded) {
            return true; // a group of the method is included and not excluded
        }
    }
    return false; // no groups of the method are included
}
 
Example #13
Source File: ReflectionUtils.java    From test-data-supplier with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Tuple2<Class<?>, String> getFactoryAnnotationMetaData(final ITestNGMethod testMethod) {
    var constructor = testMethod.getConstructorOrMethod().getConstructor();
    var method = testMethod.getConstructorOrMethod().getMethod();

    var factoryAnnotation = nonNull(method)
            ? ofNullable(method.getDeclaredAnnotation(Factory.class))
            : ofNullable(constructor.getDeclaredAnnotation(Factory.class));

    var dataProviderClass = factoryAnnotation
            .map(fa -> (Class) fa.dataProviderClass())
            .filter(cl -> cl != Object.class)
            .orElseGet(() -> testMethod.getConstructorOrMethod().getDeclaringClass());

    var dataProviderMethod = factoryAnnotation.map(Factory::dataProvider).orElse("");

    return Tuple.of(dataProviderClass, dataProviderMethod);
}
 
Example #14
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the given test method is part of the current test run in matters of the include/exclude group filtering 
 */
private boolean isInActiveGroup(ITestNGMethod method, ITestContext context) {
    String[] includedGroupsArray = context.getIncludedGroups();
    List<String> includeGroups = includedGroupsArray != null ? asList(includedGroupsArray) : Collections.<String>emptyList();
    String[] excludedGroupsArray = context.getExcludedGroups();
    List<String> excludeGroups = excludedGroupsArray != null ? asList(excludedGroupsArray) : Collections.<String>emptyList();
    if (includeGroups.isEmpty()) {
        if (excludeGroups.isEmpty()) {
            return true; // no group restriction
        } else {
            return isInActiveGroupWithoutIncludes(method, excludeGroups);
        }
    } else {
        return isInActiveGroupWithIncludes(method, includeGroups, excludeGroups);
    }
}
 
Example #15
Source File: MetaDataScanner.java    From qaf with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static boolean applyMetafilter(ITestNGMethod imethod, Map<String, Object> scenarioMetadata) {
	String includeStr = getParameter(imethod, "include");
	String excludeStr = getParameter(imethod, "exclude");
	if (StringUtil.isBlank(includeStr) && StringUtil.isBlank(excludeStr)) {
		// no need to process as no include/exclude filter provided
		return true;
	}

	Gson gson = new GsonBuilder().create();

	Map<String, Object> includeMeta = gson.fromJson(includeStr, Map.class);
	Map<String, Object> excludeMeta = gson.fromJson(excludeStr, Map.class);

	return MetaDataScanner.includeMethod(scenarioMetadata, includeMeta, excludeMeta);
}
 
Example #16
Source File: QAFInetrceptableDataProvider.java    From qaf with MIT License 6 votes vote down vote up
private static Iterator<Object[]> invokeCustomDataProvider(ITestNGMethod tm, ITestContext c, String dp,
		String dpc) {
	String methodClass = tm.getConstructorOrMethod().getDeclaringClass().getName();

	if (isBlank(dpc)) {
		dpc = getBundle().getString("global.dataproviderclass", getBundle().getString("dataproviderclass",methodClass));
	}
	if (isNotBlank(dpc)) {
		Method m;
		try {
			m = getDataProviderMethod(dp, dpc);
		} catch (Exception e) {
			m = getDataProviderMethod(dp, methodClass);
		}
		Object instanceToUse = m.getDeclaringClass().equals(tm.getConstructorOrMethod().getDeclaringClass()) ? tm.getInstance()
				: ClassHelper.newInstanceOrNull(m.getDeclaringClass());
		return InvocatoinHelper.invokeDataProvider(instanceToUse, m, tm, c, null,
				new Configuration().getAnnotationFinder());
	} else {
		throw new DataProviderException(
				"Data-provider class not found. Please provide fully qualified class name as dataProviderClass");
	}
}
 
Example #17
Source File: ReporterUtil.java    From qaf with MIT License 6 votes vote down vote up
private static int getSkipCnt(ITestContext context) {
	if ((context != null) && (context.getSkippedTests() != null)) {
		if (context.getSkippedTests().getAllResults() != null) {
			Collection<ITestNGMethod> skippedTest =
					context.getSkippedTests().getAllMethods();
			Set<ITestNGMethod> set = new HashSet<ITestNGMethod>(skippedTest);
			if (ApplicationProperties.RETRY_CNT.getIntVal(0) > 0) {
				set.removeAll(context.getPassedTests().getAllMethods());
				set.removeAll(context.getFailedTests().getAllMethods());
				return set.size();
			}
			return context.getSkippedTests().getAllResults().size();
		}
		return context.getSkippedTests().size();
	}
	return 0;
}
 
Example #18
Source File: TestNGTestResultProcessorAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onFinish(ITestContext iTestContext) {
    Object id;
    synchronized (lock) {
        id = suites.remove(iTestContext.getName());
        for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
            testMethodToSuiteMapping.remove(method);
        }
    }
    resultProcessor.completed(id, new TestCompleteEvent(iTestContext.getEndDate().getTime()));
}
 
Example #19
Source File: SpotRetryOnceTestListener.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
/**
 * Add {@link SpotRetryOnce} retry analyzer to test methods that should be retried on failure.
 */
@Override
public void onTestStart(ITestResult result) {
    ITestNGMethod testNGMethod = result.getMethod();
    if (spotUtil.shouldUseSpotInstancesForTest(testNGMethod.getConstructorOrMethod().getMethod())) {
        testNGMethod.setRetryAnalyzerClass(SpotRetryOnce.class);
    }
}
 
Example #20
Source File: MethodHelper.java    From qaf with MIT License 5 votes vote down vote up
/**
 * Extracts the unique list of <code>ITestNGMethod</code>s.
 */
public static List<ITestNGMethod> uniqueMethodList(Collection<List<ITestNGMethod>> methods) {
  Set<ITestNGMethod> resultSet = Sets.newHashSet();

  for (List<ITestNGMethod> l : methods) {
    resultSet.addAll(l);
  }

  return Lists.newArrayList(resultSet);
}
 
Example #21
Source File: TestNGTestResultProcessorAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onFinish(ITestContext iTestContext) {
    Object id;
    synchronized (lock) {
        id = suites.remove(iTestContext.getName());
        for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
            testMethodToSuiteMapping.remove(method);
        }
    }
    resultProcessor.completed(id, new TestCompleteEvent(iTestContext.getEndDate().getTime()));
}
 
Example #22
Source File: MethodHelper.java    From qaf with MIT License 5 votes vote down vote up
/**
 * Finds method based on regex and TestNGMethod. If regex doesn't represent the
 * class name, uses the TestNG method's class name.
 * @param testngMethod TestNG method
 * @param regExp regex representing a method and/or related class name
 */
private static Method findMethodByName(ITestNGMethod testngMethod, String regExp) {
  if (regExp == null) {
    return null;
  }
  int lastDot = regExp.lastIndexOf('.');
  String className, methodName;
  if (lastDot == -1) {
    className = testngMethod.getConstructorOrMethod().getDeclaringClass().getCanonicalName();
    methodName = regExp;
  } else {
    methodName = regExp.substring(lastDot + 1);
    className = regExp.substring(0, lastDot);
  }

  try {
    Class<?> c = Class.forName(className);
    for (Method m : c.getDeclaredMethods()) {
      if (methodName.equals(m.getName())) {
        return m;
      }
    }
  }
  catch (Exception e) {
    //only logging
    Utils.log("MethodHelper", 3, "Caught exception while searching for methods using regex");
  }
  return null;
}
 
Example #23
Source File: TestNGTestResultProcessorAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onStart(ITestContext iTestContext) {
    TestDescriptorInternal testInternal;
    synchronized (lock) {
        testInternal = new DefaultTestSuiteDescriptor(idGenerator.generateId(), iTestContext.getName());
        suites.put(testInternal.getName(), testInternal.getId());
        for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
            testMethodToSuiteMapping.put(method, testInternal.getId());
        }
    }
    resultProcessor.started(testInternal, new TestStartEvent(iTestContext.getStartDate().getTime()));
}
 
Example #24
Source File: TestNGMethod.java    From test-data-supplier with Apache License 2.0 5 votes vote down vote up
public Tuple2<Method, Object[]> getDataSupplierMetaData() {
    return Tuple.of(dataSupplierMethod, stream(dataSupplierMethod.getParameterTypes())
        .map(t -> Match((Class) t).of(
            Case($(ITestContext.class), () -> context),
            Case($(Method.class), () -> testMethod.getConstructorOrMethod().getMethod()),
            Case($(ITestNGMethod.class), () -> testMethod),
            Case($(), () -> null)))
        .toArray());
}
 
Example #25
Source File: MethodUtils.java    From video-recorder-java with MIT License 5 votes vote down vote up
public static Video getVideoAnnotation(ITestResult result) {
    ITestNGMethod method = result.getMethod();
    Annotation[] declaredAnnotations = method.getConstructorOrMethod().getMethod().getDeclaredAnnotations();
    for (Annotation declaredAnnotation : declaredAnnotations) {
        if (declaredAnnotation.annotationType().equals(Video.class)) {
            return (Video) declaredAnnotation;
        }
    }
    return null;
}
 
Example #26
Source File: BaseClassForTests.java    From xian with Apache License 2.0 5 votes vote down vote up
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context)
{
    for ( ITestNGMethod method : context.getAllTestMethods() )
    {
        method.setRetryAnalyzer(new RetryTest());
    }
}
 
Example #27
Source File: MethodHelper.java    From qaf with MIT License 5 votes vote down vote up
/**
 * @return A sorted array containing all the methods 'method' depends on
 */
public static List<ITestNGMethod> getMethodsDependedUpon(ITestNGMethod method, ITestNGMethod[] methods) {
  Graph<ITestNGMethod> g = GRAPH_CACHE.get(methods);
  if (g == null) {
    List<ITestNGMethod> parallelList = Lists.newArrayList();
    List<ITestNGMethod> sequentialList = Lists.newArrayList();
    g = topologicalSort(methods, sequentialList, parallelList);
    GRAPH_CACHE.put(methods, g);
  }

  List<ITestNGMethod> result = g.findPredecessors(method);
  return result;
}
 
Example #28
Source File: TestNGTestResultProcessorAdapter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void onFinish(ITestContext iTestContext) {
    Object id;
    synchronized (lock) {
        id = suites.remove(iTestContext.getName());
        for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
            testMethodToSuiteMapping.remove(method);
        }
    }
    resultProcessor.completed(id, new TestCompleteEvent(iTestContext.getEndDate().getTime()));
}
 
Example #29
Source File: PowerEmailableReporter.java    From WebAndAppUITesting with GNU General Public License v3.0 5 votes vote down vote up
private void resultDetail(IResultMap tests) {
	for (ITestResult result : tests.getAllResults()) {
		ITestNGMethod method = result.getMethod();

		int methodId = getId(result);

		String cname = method.getTestClass().getName();
		m_out.println("<h2 id=\"m" + methodId + "\" name=\"m" + methodId + "\" >" + cname + ":"
				+ method.getMethodName() + "</h2>");
		Set<ITestResult> resultSet = tests.getResults(method);
		generateForResult(result, method, resultSet.size());
		m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>");

	}
}
 
Example #30
Source File: BenchmarkTestListener.java    From oxAuth with MIT License 5 votes vote down vote up
private long getMethodThreqads(ITestContext context, String methodName) {
	ITestNGMethod[] allTestMethods = context.getAllTestMethods();
	for (int i = 0; i < allTestMethods.length; i++) {
		if (StringHelper.equalsIgnoreCase(allTestMethods[i].getMethodName(), methodName)) {
			return allTestMethods[i].getThreadPoolSize();
		}
	}

	return 1;
}