private void ifTestFixtureStarted(final ITestContext context, final ITestNGMethod testMethod) { if (testMethod.isBeforeTestConfiguration()) { startBefore(getUniqueUuid(context), testMethod); } if (testMethod.isAfterTestConfiguration()) { startAfter(getUniqueUuid(context), testMethod); } }
private void ifMethodFixtureStarted(final ITestNGMethod testMethod) { currentTestContainer.remove(); Current current = currentTestResult.get(); final FixtureResult fixture = getFixtureResult(testMethod); final String uuid = currentExecutable.get(); if (testMethod.isBeforeMethodConfiguration()) { if (current.isStarted()) { currentTestResult.remove(); current = currentTestResult.get(); } getLifecycle().startPrepareFixture(createFakeContainer(testMethod, current), uuid, fixture); } if (testMethod.isAfterMethodConfiguration()) { getLifecycle().startTearDownFixture(createFakeContainer(testMethod, current), uuid, fixture); } }
@Override public void afterInvocation(final IInvokedMethod method, final ITestResult testResult, final ITestContext context) { final ITestNGMethod testMethod = method.getTestMethod(); if (isSupportedConfigurationFixture(testMethod)) { final String executableUuid = currentExecutable.get(); currentExecutable.remove(); if (testResult.isSuccess()) { getLifecycle().updateFixture(executableUuid, result -> result.withStatus(Status.PASSED)); } else { getLifecycle().updateFixture(executableUuid, result -> result .withStatus(getStatus(testResult.getThrowable())) .withStatusDetails(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); } } }
private List<Parameter> getParameters(final ITestResult testResult) { final Stream<Parameter> tagsParameters = testResult.getTestContext() .getCurrentXmlTest().getAllParameters().entrySet() .stream() .map(entry -> new Parameter().withName(entry.getKey()).withValue(entry.getValue())); final String[] parameterNames = Optional.of(testResult) .map(ITestResult::getMethod) .map(ITestNGMethod::getConstructorOrMethod) .map(ConstructorOrMethod::getMethod) .map(Executable::getParameters) .map(Stream::of) .orElseGet(Stream::empty) .map(java.lang.reflect.Parameter::getName) .toArray(String[]::new); final String[] parameterValues = Stream.of(testResult.getParameters()) .map(this::convertParameterValueToString) .toArray(String[]::new); final Stream<Parameter> methodParameters = range(0, min(parameterNames.length, parameterValues.length)) .mapToObj(i -> new Parameter().withName(parameterNames[i]).withValue(parameterValues[i])); return Stream.concat(tagsParameters, methodParameters) .collect(Collectors.toList()); }
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; }
public ITestNGMethod[] getAllMethodsForTest(String testName) { ITestContext overview = null; for (ISuite suite : suites) { suiteName = suite.getName(); Map<String, ISuiteResult> tests = suite.getResults(); for (ISuiteResult r : tests.values()) { overview = r.getTestContext(); if (overview.getName().equalsIgnoreCase(testName)) { return overview.getAllTestMethods(); } } } return null; }
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; }
private void generateExceptionReport(Throwable exception, ITestNGMethod method, String title) { m_out.println("<p>" + Utils.escapeHtml(title) + "</p>"); StackTraceElement[] s1 = exception.getStackTrace(); Throwable t2 = exception.getCause(); if (t2 == exception) { t2 = null; } int maxlines = Math.min(100, StackTraceTools.getTestRoot(s1, method)); for (int x = 0; x <= maxlines; x++) { m_out.println((x > 0 ? "<br/>at " : "") + Utils.escapeHtml(s1[x].toString())); } if (maxlines < s1.length) { m_out.println("<br/>" + (s1.length - maxlines) + " lines not shown"); } if (t2 != null) { generateExceptionReport(t2, method, "Caused by " + t2.getLocalizedMessage()); } }
/** * Collects and orders test or configuration methods * @param methods methods to be worked on * @param forTests true for test methods, false for configuration methods * @param runInfo * @param finder annotation finder * @param unique true for unique methods, false otherwise * @param outExcludedMethods * @return list of ordered methods */ public static ITestNGMethod[] collectAndOrderMethods(List<ITestNGMethod> methods, boolean forTests, RunInfo runInfo, IAnnotationFinder finder, boolean unique, List<ITestNGMethod> outExcludedMethods) { List<ITestNGMethod> includedMethods = Lists.newArrayList(); MethodGroupsHelper.collectMethodsByGroup(methods.toArray(new ITestNGMethod[methods.size()]), forTests, includedMethods, outExcludedMethods, runInfo, finder, unique); return sortMethods(forTests, includedMethods, finder).toArray(new ITestNGMethod[]{}); }
private static List<ITestNGMethod> sortMethods(boolean forTests, List<ITestNGMethod> allMethods, IAnnotationFinder finder) { List<ITestNGMethod> sl = Lists.newArrayList(); List<ITestNGMethod> pl = Lists.newArrayList(); ITestNGMethod[] allMethodsArray = allMethods.toArray(new ITestNGMethod[allMethods.size()]); // Fix the method inheritance if these are @Configuration methods to make // sure base classes are invoked before child classes if 'before' and the // other way around if they are 'after' if (!forTests && allMethodsArray.length > 0) { ITestNGMethod m = allMethodsArray[0]; boolean before = m.isBeforeClassConfiguration() || m.isBeforeMethodConfiguration() || m.isBeforeSuiteConfiguration() || m.isBeforeTestConfiguration(); MethodInheritance.fixMethodInheritance(allMethodsArray, before); } topologicalSort(allMethodsArray, sl, pl); List<ITestNGMethod> result = Lists.newArrayList(); result.addAll(sl); result.addAll(pl); return result; }
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; }
@Test public void finishTestMethodSkipped() { ListenerParameters listenerParameters = new ListenerParameters(); listenerParameters.setSkippedAnIssue(true); when(launch.getParameters()).thenReturn(listenerParameters); when(testResult.getAttribute(RP_ID)).thenReturn(null); ITestNGMethod method = mock(ITestNGMethod.class); when(method.isTest()).thenReturn(true); when(testResult.getMethod()).thenReturn(method); testNGService.finishTestMethod(Statuses.SKIPPED, testResult); verify(launch, times(1)).startTestItem(eq(id), any(StartTestItemRQ.class)); verify(testResult, times(1)).setAttribute(eq(RP_ID), any(Maybe.class)); verify(launch, times(1)).finishTestItem(any(Maybe.class), any(FinishTestItemRQ.class)); }
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; }
@DataProvider(name = "genericDataProvider") public static Object[][] baseDataProvider(ITestNGMethod testContext) throws IOException { String testName = testContext.getMethodName(); ITestData testDataReader = ConfigurationReaderFactory .getTestDataReader(); int iterationCount = testDataReader.getIterationCountForTest(testName); List<HashMap<String, String>> data = testDataReader .getAllData(testName); Object[][] dataSet = new Object[iterationCount][1]; for (int i = 0; i < iterationCount; i++) { dataSet[i][0] = new TestData(data.get(i), testName); } return dataSet; }
@Override public void onStart(ITestContext context) { final Set<Object> instances = Stream.of(context.getAllTestMethods()) .map(ITestNGMethod::getInstance) .collect(Collectors.toSet()); if (instances.size() != 1) { throw new IllegalStateException("Tck test should be one and only one in suite."); } instance = instances.iterator().next(); injector = Guice.createInjector(createModule(context, instance.getClass().getName())); injector.injectMembers(instance); }
private void initializeClassLevel(ITestContext iTestContext) { classBrowsers.clear(); ITestNGMethod[] testMethods = iTestContext.getAllTestMethods(); for (ITestNGMethod method : testMethods) { Class<?> testClass = method.getRealClass(); for (Field field : ReflectionUtils.getAllFieldsOfClassHierarchy(testClass)) { boolean fieldIsStatic = Modifier.isStatic(field.getModifiers()); boolean fieldIsABrowser = Browser.class.isAssignableFrom(field.getType()); boolean fieldIsAnnotatedAsResource = field.getAnnotation(Resource.class) != null; boolean isNoDuplicate = fieldIsNoDuplicateOfAnExistingClassBrowserField(field); if (fieldIsStatic && fieldIsABrowser && fieldIsAnnotatedAsResource && isNoDuplicate) { classBrowserFields.add(field); classBrowsers.add(new ClassTestBrowser(field)); } } } }
private Map<String, ArrayList<String>> processTestContext( ITestContext context) { Map<String, ArrayList<String>> classMap = new HashMap<String, ArrayList<String>>(); Collection<ITestNGMethod> testMethods = Arrays.asList(context.getAllTestMethods()); for (ITestNGMethod testMethod : testMethods) { ConstructorOrMethod consMethod = testMethod.getConstructorOrMethod(); String methodName = consMethod.getName(); String className = consMethod.getDeclaringClass().getName(); ArrayList<String> methodList; if (!classMap.containsKey(className)) { methodList = new ArrayList<String>(); } else { methodList = classMap.get(className); } methodList.add(methodName); classMap.put(className, methodList); } return classMap; }
private void writeSuiteGroups(XMLStringBuffer xmlBuffer, ISuite suite) { xmlBuffer.push(XMLReporterConfig.TAG_GROUPS); Map<String, Collection<ITestNGMethod>> methodsByGroups = suite .getMethodsByGroups(); for (Map.Entry<String, Collection<ITestNGMethod>> entry : methodsByGroups .entrySet()) { Properties groupAttrs = new Properties(); groupAttrs.setProperty(XMLReporterConfig.ATTR_NAME, entry.getKey()); xmlBuffer.push(XMLReporterConfig.TAG_GROUP, groupAttrs); Set<ITestNGMethod> groupMethods = getUniqueMethodSet(entry .getValue()); for (ITestNGMethod groupMethod : groupMethods) { Properties methodAttrs = new Properties(); methodAttrs.setProperty(XMLReporterConfig.ATTR_NAME, groupMethod.getMethodName()); methodAttrs.setProperty(XMLReporterConfig.ATTR_METHOD_SIG, groupMethod.toString()); methodAttrs.setProperty(XMLReporterConfig.ATTR_CLASS, groupMethod.getRealClass().getName()); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_METHOD, methodAttrs); } xmlBuffer.pop(); } xmlBuffer.pop(); }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { for (IMethodInstance methodIns : methods) { ITestNGMethod method = methodIns.getMethod(); ConstructorOrMethod meth = method.getConstructorOrMethod(); Method m = meth.getMethod(); if (m != null) { DB db = m.getAnnotation(DB.class); if (db != null) { TransactionLegacy txn = TransactionLegacy.open(m.getName()); } } } // TODO Auto-generated method stub return methods; }
private void addPendingMethods(ITestContext iTestContext) { for (ITestNGMethod method : iTestContext.getExcludedMethods()) { if (method.isTest() && !method.getEnabled() && isInActiveGroup(method, iTestContext)) { Description description = new Description().withValue(method.getDescription()); String suiteUid = getSuiteUid(iTestContext); TestCaseStartedEvent event = new TestCaseStartedEvent(suiteUid, method.getMethodName()); if (description.getValue() != null) { event.setDescription(description); } Annotation[] annotations = method.getConstructorOrMethod().getMethod().getAnnotations(); AnnotationManager am = new AnnotationManager(annotations); am.setDefaults(method.getInstance().getClass().getAnnotations()); am.update(event); getLifecycle().fire(event); getLifecycle().fire(new TestCasePendingEvent()); fireFinishTest(); } } }
/** * 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); } }
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 }
/** * 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; }
@SuppressWarnings("unchecked") public static Object[][] getNeedRerunDataProvider(Annotation[] annotations, ITestContext context, ITestNGMethod m) { Object[][] dp = getDataProvider(annotations, context, m); List<String> doNotRunRowIDs = (List<String>) context.getAttribute(SpecialKeywords.DO_NOT_RUN_TESTS); Map<String, String> testNameArgsMap = (Map<String, String>) context.getAttribute(SpecialKeywords.CANONICAL_TEST_NAME_ARGS_MAP); if (!doNotRunRowIDs.isEmpty()) { for (int i = dp.length - 1; i >= 0; i--) { String testUniqueName = testNameArgsMap.get(testNameArgsMap.keySet().toArray()[i]); if (testUniqueName != null) { if (doNotRunRowIDs.contains(testUniqueName)) { dp = ArrayUtils.remove(dp, i); } } } } return dp; }
public static String appendTestMethodName(String testName, ITestNGMethod m) { int invocationID = -1; if (m.getInvocationCount() > 1) { invocationID = m.getCurrentInvocationCount() + 1; } if (invocationID != -1) { // TODO: analyze if "InvCount=nnnn" is already present in name and don't append it one more time testName = testName + " - " + m.getMethodName() + String.format(SpecialKeywords.INVOCATION_COUNTER, String.format("%04d", invocationID)); } else { testName = testName + " - " + m.getMethodName(); } return StringEscapeUtils.escapeHtml4(testName); }
@Override public void onStart(final ITestContext context) { final String parentUuid = getUniqueUuid(context.getSuite()); final String uuid = getUniqueUuid(context); final TestResultContainer container = new TestResultContainer() .withUuid(uuid) .withName(context.getName()) .withStart(System.currentTimeMillis()); getLifecycle().startTestContainer(parentUuid, container); Stream.of(context.getAllTestMethods()) .map(ITestNGMethod::getTestClass) .distinct() .forEach(this::onBeforeClass); }
@Override public void onFinish(final ITestContext context) { final String uuid = getUniqueUuid(context); getLifecycle().stopTestContainer(uuid); getLifecycle().writeTestContainer(uuid); Stream.of(context.getAllTestMethods()) .map(ITestNGMethod::getTestClass) .distinct() .forEach(this::onAfterClass); }
@Override public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult, final ITestContext context) { final ITestNGMethod testMethod = method.getTestMethod(); if (isSupportedConfigurationFixture(testMethod)) { ifSuiteFixtureStarted(context.getSuite(), testMethod); ifTestFixtureStarted(context, testMethod); ifClassFixtureStarted(testMethod); ifMethodFixtureStarted(testMethod); } }
private void ifSuiteFixtureStarted(final ISuite suite, final ITestNGMethod testMethod) { if (testMethod.isBeforeSuiteConfiguration()) { startBefore(getUniqueUuid(suite), testMethod); } if (testMethod.isAfterSuiteConfiguration()) { startAfter(getUniqueUuid(suite), testMethod); } }
private String createFakeContainer(final ITestNGMethod method, final Current current) { final String parentUuid = currentTestContainer.get(); final TestResultContainer container = new TestResultContainer() .withUuid(parentUuid) .withName(getQualifiedName(method)) .withStart(System.currentTimeMillis()) .withDescription(method.getDescription()) .withChildren(current.getUuid()); getLifecycle().startTestContainer(container); return parentUuid; }
private FixtureResult getFixtureResult(final ITestNGMethod method) { return new FixtureResult() .withName(getMethodName(method)) .withStart(System.currentTimeMillis()) .withDescription(method.getDescription()) .withStage(Stage.RUNNING); }
protected String getHistoryId(final ITestNGMethod method, final List<Parameter> parameters) { final MessageDigest digest = getMessageDigest(); final String testClassName = method.getTestClass().getName(); final String methodName = method.getMethodName(); digest.update(testClassName.getBytes(UTF_8)); digest.update(methodName.getBytes(UTF_8)); parameters.stream() .sorted(comparing(Parameter::getName).thenComparing(Parameter::getValue)) .forEachOrdered(parameter -> { digest.update(parameter.getName().getBytes(UTF_8)); digest.update(parameter.getValue().getBytes(UTF_8)); }); final byte[] bytes = digest.digest(); return new BigInteger(1, bytes).toString(16); }
@SuppressWarnings("BooleanExpressionComplexity") private boolean isSupportedConfigurationFixture(final ITestNGMethod testMethod) { return testMethod.isBeforeMethodConfiguration() || testMethod.isAfterMethodConfiguration() || testMethod.isBeforeTestConfiguration() || testMethod.isAfterTestConfiguration() || testMethod.isBeforeClassConfiguration() || testMethod.isAfterClassConfiguration() || testMethod.isBeforeSuiteConfiguration() || testMethod.isAfterSuiteConfiguration(); }
private <T extends Annotation> List<T> getAnnotationsOnMethod(final ITestResult result, final Class<T> clazz) { return Stream.of(result) .map(ITestResult::getMethod) .filter(Objects::nonNull) .map(ITestNGMethod::getConstructorOrMethod) .map(ConstructorOrMethod::getMethod) .flatMap(method -> Stream.of(method.getAnnotationsByType(clazz))) .collect(Collectors.toList()); }
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>"); } }
private String generateExceptionReport(Throwable exception, ITestNGMethod method, String title) { StackTraceElement[] s1 = exception.getStackTrace(); Throwable t2 = exception.getCause(); StringBuilder exceptionStackTrace = new StringBuilder(); if (t2 == exception) { t2 = null; } int maxlines = Math.min(100, StackTraceTools.getTestRoot(s1, method)); for (int x = 0; x <= maxlines; x++) { exceptionStackTrace.append((x > 0 ? "<br/>at " : "") + Utils.escapeHtml(s1[x].toString())); } if (maxlines < s1.length) { exceptionStackTrace.append("<br/>" + (s1.length - maxlines) + " lines not shown"); } if (t2 != null) { generateExceptionReport(t2, method, "Caused by " + t2.getLocalizedMessage()); } return exceptionStackTrace.toString(); }
public void onStart(ITestContext context) { System.err.println("TEST started : " + context.getName()); testsInProgress.add(context.getName()); ITestNGMethod[] allTestMethods = context.getAllTestMethods(); for (ITestNGMethod m : allTestMethods) { String testMethod = m.getTestClass().getName() + "." + m.getMethodName(); testsPending.add(testMethod); } update(); }
/** * @param tests * @return */ private Collection<ITestNGMethod> getMethodSet(IResultMap tests) { Set r = new TreeSet<ITestNGMethod>(new TestSorter<ITestNGMethod>()); r.addAll(tests.getAllMethods()); return r; }
protected static void logInvocation(final TestStatus testStatus, final IInvokedMethod method) { ITestNGMethod iTestNGMethod = method.getTestMethod(); String methodName = iTestNGMethod.getTestClass().getName() + '#' + iTestNGMethod.getMethodName(); String methodType = "test"; if (method.isConfigurationMethod()) { methodType = "configuration"; } LOGGER.info("{} {} {} method: {}", (testStatus == TestStatus.ERRORED_OUT ? "!!!" : "==="), testStatus, methodType, methodName); }