Java Code Examples for org.testng.ITestResult#getMethod()

The following examples show how to use org.testng.ITestResult#getMethod() . 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: 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 2
Source File: StepExecutionTracker.java    From qaf with MIT License 5 votes vote down vote up
/**
 * This is utility method and will return {@link TestNGScenario} for which
 * current step invoked or null if not found reference.
 * 
 * @return
 */
public TestNGScenario getScenario() {
	ITestResult result =
			(ITestResult) ApplicationProperties.CURRENT_TEST_RESULT.getObject();
	if (result != null) {
		return ((TestNGScenario) result.getMethod());
	}
	return null;
}
 
Example 3
Source File: TestNamingListener.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
 * get Test Method name
 * 
 * @param result ITestResult
 * @return String method name
 */
public static String getMethodName(ITestResult result) {
    // adjust testName using pattern
    ITestNGMethod m = result.getMethod();
    String name = Configuration.get(Configuration.Parameter.TEST_NAMING_PATTERN);
    name = name.replace(SpecialKeywords.METHOD_NAME, m.getMethodName());
    name = name.replace(SpecialKeywords.METHOD_PRIORITY, String.valueOf(m.getPriority()));
    name = name.replace(SpecialKeywords.METHOD_THREAD_POOL_SIZE, String.valueOf(m.getThreadPoolSize()));

    if (m.getDescription() != null) {
        name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, m.getDescription());
    } else {
        name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, "");
    }

    return name;
}
 
Example 4
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestStart(ITestResult iTestResult) {
    ITestNGMethod method = iTestResult.getMethod();
    String testSuiteLabel = iTestResult.getTestContext().getSuite().getName();
    String testGroupLabel = iTestResult.getTestContext().getCurrentXmlTest().getName();
    String testClassLabel = iTestResult.getTestClass().getName();
    String testMethodLabel = method.getMethodName();
    String suitePrefix = getCurrentSuitePrefix(iTestResult);
    String testName = getName(iTestResult);
    startedTestNames.add(testName);
    testName = testName.replace(suitePrefix, "");

    String invoc = getMethodInvocationsAndSuccessPercentage(iTestResult);
    Description description = new Description().withValue(method.getDescription());
    String suiteUid = getSuiteUid(iTestResult.getTestContext());
    TestCaseStartedEvent event = new TestCaseStartedEvent(suiteUid, testName + invoc).withLabels(
            AllureModelUtils.createTestSuiteLabel(testSuiteLabel),
            AllureModelUtils.createTestGroupLabel(testGroupLabel),
            AllureModelUtils.createTestClassLabel(testClassLabel),
            AllureModelUtils.createTestMethodLabel(testMethodLabel));
    if (description.getValue() != null) {
        event.setDescription(description);
    }
    AnnotationManager am = new AnnotationManager(getMethodAnnotations(iTestResult));
    am.setDefaults(getClassAnnotations(iTestResult));
    am.update(event);

    getLifecycle().fire(event);

    if (AllureConfig.newInstance().areTestNgParametersEnabled()) {
        fireAddParameterEvents(iTestResult);
    }
}
 
Example 5
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 6
Source File: VerboseReporter.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {
    ITestNGMethod[] result = new ITestNGMethod[results.size()];
    int i = 0;
    for (ITestResult tr : results) {
        result[i++] = tr.getMethod();
    }
    return result;
}
 
Example 7
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 8
Source File: Retry.java    From Quantum with MIT License 5 votes vote down vote up
@Override
public boolean retry(ITestResult result) {

	boolean shouldRetry = shouldRetry(result);
	if (shouldRetry) {
		try {
			if (result.getMethod() instanceof TestNGScenario) {
				((TestNGScenario) result.getMethod()).decAndgetCurrentInvocationCount();
			}
		} catch (Exception e) {
			System.err.println(e);
		}
		int retryInvocationCount = getRetryCount() + 1;
		System.err.println(
				"Retrying [" + result.getName() + "]" + toStringWithSufix(retryInvocationCount) + " time.");

		getBundle().addProperty(RETRY_INVOCATION_COUNT, retryInvocationCount);

		// correct failed invocation numbers for data driven test case.
		List<Integer> failedInvocations = result.getMethod().getFailedInvocationNumbers();
		if (null != failedInvocations && !failedInvocations.isEmpty()) {
			int lastFailedIndex = failedInvocations.size() - 1;
			failedInvocations.remove(lastFailedIndex);
		}

	} else {
		getBundle().clearProperty(RETRY_INVOCATION_COUNT);
	}

	return shouldRetry;
}
 
Example 9
Source File: ReporterUtil.java    From qaf with MIT License 5 votes vote down vote up
private static String getTestClassName(ITestResult result){
	if((result.getMethod() instanceof TestNGScenario)){
		String classOrFile = ((TestNGScenario)result.getMethod()).getClassOrFileName();
		File f = new File(classOrFile);
		if(f.exists() && !classOrFile.equalsIgnoreCase(result.getTestClass().getName())) {
			return FilenameUtils.removeExtension(f.getName());
		}
		return classOrFile;
	}
	return result.getTestClass().getName();
}
 
Example 10
Source File: ReporterUtil.java    From qaf with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static String getMethodIdentifier(ITestResult result){

	if(result.getAttribute(QAF_TEST_IDENTIFIER)!=null){
		return (String) result.getAttribute(QAF_TEST_IDENTIFIER);
	}
	
	String id = getMethodName(result);
	String identifierKey = ApplicationProperties.TESTCASE_IDENTIFIER_KEY.getStringVal("testCaseId");

	Map<String, Object> metadata =
			new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);

	if (result.getMethod() instanceof TestNGScenario) {
		TestNGScenario scenario = (TestNGScenario) result.getMethod();
		metadata.putAll(scenario.getMetaData());
	}
	if(result.getParameters()!=null && result.getParameters().length>0){
		if(result.getParameters()[0] instanceof Map<?, ?>){
			metadata.putAll((Map<String, Object>)result.getParameters()[0]);
		}
	}
	String idFromMetaData = metadata.getOrDefault(identifierKey,"").toString();
	if (StringUtil.isNotBlank(idFromMetaData) ) {
		id = idFromMetaData;
	}
	id=StringUtil.toTitleCaseIdentifier(id);
	
	if(id.length()>45){
		id=id.substring(0, 45);
	}
	result.setAttribute(QAF_TEST_IDENTIFIER,id);
	return (String) result.getAttribute(QAF_TEST_IDENTIFIER);
}
 
Example 11
Source File: RetryAnalyzer.java    From qaf with MIT License 5 votes vote down vote up
@Override
public boolean retry(ITestResult result) {

	boolean shouldRetry = shouldRetry(result);
	if (shouldRetry) {
		try {
			if (result.getMethod() instanceof TestNGScenario) {
				((TestNGScenario) result.getMethod()).decAndgetCurrentInvocationCount();
			}
		} catch (Exception e) {
			System.err.println(e);
		}
		int retryInvocationCount = getRetryCount() + 1;
		System.err.println(
				"Retrying [" + result.getName() + "]" + toStringWithSufix(retryInvocationCount) + " time.");

		getBundle().addProperty(RETRY_INVOCATION_COUNT, retryInvocationCount);
		getBundle().addProperty(WILL_RETRY, true);


		// correct failed invocation numbers for data driven test case.
		List<Integer> failedInvocations = result.getMethod().getFailedInvocationNumbers();
		if (null != failedInvocations && !failedInvocations.isEmpty()) {
			int lastFailedIndex = failedInvocations.size() - 1;
			failedInvocations.remove(lastFailedIndex);
		}

	} else {
		getBundle().clearProperty(RETRY_INVOCATION_COUNT);
		getBundle().clearProperty(WILL_RETRY);
	}

	return shouldRetry;
}
 
Example 12
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 13
Source File: FHIRPathSpecTest.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * Hack to override the method name in the TestNG reports (but still doesn't work to override the method name on the Eclipse TestNG view)
 * @see <a href="https://stackoverflow.com/a/49522435/161022">https://stackoverflow.com/a/49522435/161022</a>
 */
@AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result) {
    try {
        BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
        Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
        f.setAccessible(true);
        f.set(baseTestMethod, testName);
    } catch (Exception e) {
        Reporter.log("Exception : " + e.getMessage());
    }
}
 
Example 14
Source File: FHIRPathPatchSpecTest.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * Hack to override the method name in the TestNG reports (but still doesn't work to override the method name on the Eclipse TestNG view)
 * @see <a href="https://stackoverflow.com/a/49522435/161022">https://stackoverflow.com/a/49522435/161022</a>
 */
@AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result) {
    try {
        BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
        Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
        f.setAccessible(true);
        f.set(baseTestMethod, testName);
    } catch (Exception e) {
        Reporter.log("Exception : " + e.getMessage());
    }
}
 
Example 15
Source File: QuickPerfTestNGListener.java    From quickperf with Apache License 2.0 4 votes vote down vote up
private Method extractTestMethod(ITestResult testResult) {
    ITestNGMethod testNGMethod = testResult.getMethod();
    return testNGMethod.getConstructorOrMethod().getMethod();
}
 
Example 16
Source File: ReporterUtil.java    From qaf with MIT License 4 votes vote down vote up
/**
 * should be called on test method completion
 * 
 * @param context
 * @param result
 */
private static synchronized void updateClassMetaInfo(ITestContext context,
		ITestResult result, String methodfname) {
	String dir = getClassDir(context, result);
	String file = dir + "/meta-info.json";
	FileUtil.checkCreateDir(dir);

	ClassInfo classInfo = getJsonObjectFromFile(file, ClassInfo.class);

	MethodInfo methodInfo = new MethodInfo();
	methodInfo.setStartTime(result.getStartMillis());
	methodInfo.setDuration(result.getEndMillis() - result.getStartMillis());

	if (result.getStatus() == ITestResult.SUCCESS_PERCENTAGE_FAILURE) {
		// methodResult.setPassPer(result.getMethod().getCurrentInvocationCount())
	}
	Map<String, Object> metadata;
	if (result.getMethod().isTest()) {
		methodInfo.setIndex(result.getMethod().getCurrentInvocationCount());
		int retryCount = getBundle().getInt(RetryAnalyzer.RETRY_INVOCATION_COUNT, 0);
		if (retryCount > 0) {
			methodInfo.setRetryCount(retryCount);
		}
		methodInfo.setArgs(result.getParameters());

		if (result.getMethod() instanceof TestNGScenario) {
			TestNGScenario scenario = (TestNGScenario) result.getMethod();
			metadata = scenario.getMetaData();
			metadata.put("description", scenario.getDescription());
			metadata.put("groups", scenario.getGroups());
		} else {
			String desc = ApplicationProperties.CURRENT_TEST_DESCRIPTION
					.getStringVal(result.getMethod().getDescription());
			metadata = new HashMap<String, Object>();
			metadata.put("groups", result.getMethod().getGroups());
			metadata.put("description", desc);
		}
		metadata.put("name", getMethodName(result));
		try {
			metadata.values().removeAll(Collections.singleton(null));
		} catch (Throwable e) {
		}
		methodInfo.setMetaData(metadata);
		getBundle().clearProperty(ApplicationProperties.CURRENT_TEST_DESCRIPTION.key);

		Test test = result.getMethod().getConstructorOrMethod().getMethod()
				.getAnnotation(Test.class);
		if (((test.dependsOnMethods() != null)
				&& (test.dependsOnMethods().length > 0))
				|| ((test.dependsOnGroups() != null)
						&& (test.dependsOnGroups().length > 0))) {
			String[] depends =
					{"Methods: " + Arrays.toString(test.dependsOnMethods()),
							"Groups: " + Arrays.toString(test.dependsOnGroups())};
			methodInfo.setDependsOn(depends);
		}
		methodInfo.setType("test");
	} else { // config method
		String name = getMethodName(result);
		logger.debug("config method:  " + name);

		metadata = new HashMap<String, Object>();
		metadata.put("groups", result.getMethod().getGroups());
		metadata.put("description", result.getMethod().getDescription());
		metadata.put("name", name);

		methodInfo.setMetaData(metadata);
		methodInfo.setType("config");

	}
	methodInfo.setResult(getResult(result.getStatus()));

	if (StringUtil.isNotBlank(methodfname)) {
		metadata.put("resultFileName", methodfname);
	}

	if (!classInfo.getMethods().contains(methodInfo)) {
		logger.debug("method:  result: " + methodInfo.getResult() + " groups: "
				+ methodInfo.getMetaData());
		classInfo.getMethods().add(methodInfo);
		writeJsonObjectToFile(file, classInfo);
	} else {
		logger.warn("methodInfo already wrritten for " + methodInfo.getName());
	}

}
 
Example 17
Source File: QAFTestNGListener2.java    From qaf with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void deployResult(ITestResult tr) {
	try {
		if (ResultUpdator.getResultUpdatorsCnt()>0 && (tr.getMethod() instanceof TestNGScenario) && ((tr.getStatus() == ITestResult.FAILURE)
				|| (tr.getStatus() == ITestResult.SUCCESS || tr.getStatus() == ITestResult.SKIP))) {

			TestCaseRunResult.Status status = tr.getStatus() == ITestResult.SUCCESS ? TestCaseRunResult.Status.PASS
					: tr.getStatus() == ITestResult.FAILURE ? TestCaseRunResult.Status.FAIL
							: TestCaseRunResult.Status.SKIPPED;

			TestNGScenario scenario = (TestNGScenario) tr.getMethod();
			Map<String, Object> params = new HashMap<String, Object>(scenario.getMetaData());
			params.put("duration", tr.getEndMillis() - tr.getStartMillis());

			Map<String, Object> executionInfo = new HashMap<String, Object>();
			executionInfo.put("testName", tr.getTestContext().getCurrentXmlTest().getName());
			executionInfo.put("suiteName", tr.getTestContext().getSuite().getName());
			
			Map<String, Object> runPrams = new HashMap<String, Object>(
					tr.getTestContext().getCurrentXmlTest().getAllParameters());
			runPrams.putAll(ConfigurationConverter.getMap(getBundle().subset("env")));
			executionInfo.put("env", runPrams);
			int retryCount = getBundle().getInt(RetryAnalyzer.RETRY_INVOCATION_COUNT, 0);
			boolean willRetry =  getBundle().getBoolean(RetryAnalyzer.WILL_RETRY, false);
			getBundle().clearProperty(RetryAnalyzer.WILL_RETRY);
			if(retryCount>0) {
				executionInfo.put("retryCount", retryCount);
			}
			TestCaseRunResult testCaseRunResult = new TestCaseRunResult(status, scenario.getMetaData(),
					tr.getParameters(), executionInfo, scenario.getSteps(), tr.getStartMillis(),willRetry,scenario.isTest() );
			testCaseRunResult.setClassName(scenario.getClassOrFileName());
			if (scenario.getGroups() != null && scenario.getGroups().length > 0) {
				testCaseRunResult.getMetaData().put("groups", scenario.getGroups());
			}
			testCaseRunResult.getMetaData().put("description",scenario.getDescription());
			testCaseRunResult.setThrowable(tr.getThrowable());
			ResultUpdator.updateResult(testCaseRunResult);
		}
	} catch (Exception e) {
		logger.warn("Unable to deploy result", e);
	}
}
 
Example 18
Source File: VerboseReporter.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Log meaningful message for passed in arguments.
 * Message itself is of form:
 * $status: "$suiteName" - $methodDeclaration ($actualArguments) finished in $x ms ($run of $totalRuns)
 *
 * @param st status of passed in itr
 * @param itr test result to be described
 * @param isConfMethod is itr describing configuration method
 */
private void logTestResult(Status st, ITestResult itr, boolean isConfMethod) {
    StringBuilder sb = new StringBuilder();
    String stackTrace = "";
    switch (st) {
        case STARTED:
            sb.append("INVOKING");
            break;
        case SKIP:
            sb.append("SKIPPED");
            stackTrace = itr.getThrowable() != null
                    ? Utils.stackTrace(itr.getThrowable(), false)[0] : "";
            break;
        case FAILURE:
            sb.append("FAILED");
            stackTrace = itr.getThrowable() != null
                    ? Utils.stackTrace(itr.getThrowable(), false)[0] : "";
            break;
        case SUCCESS:
            sb.append("PASSED");
            break;
        case SUCCESS_PERCENTAGE_FAILURE:
            sb.append("PASSED with failures");
            break;
        default:
            //not happen
            throw new RuntimeException("Unsupported test status:" + itr.getStatus());
    }
    if (isConfMethod) {
        sb.append(" CONFIGURATION: ");
    } else {
        sb.append(": ");
    }
    ITestNGMethod tm = itr.getMethod();
    int identLevel = sb.length();
    sb.append(getMethodDeclaration(tm));
    Object[] params = itr.getParameters();
    Class<?>[] paramTypes = tm.getConstructorOrMethod().getParameterTypes();
    if (null != params && params.length > 0) {
        // The error might be a data provider parameter mismatch, so make
        // a special case here
        if (params.length != paramTypes.length) {
            sb.append("Wrong number of arguments were passed by the Data Provider: found ");
            sb.append(params.length);
            sb.append(" but expected ");
            sb.append(paramTypes.length);
        } else {
            sb.append("(value(s): ");
            for (int i = 0; i < params.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(Utils.toString(params[i], paramTypes[i]));
            }
            sb.append(")");

        }
    }
    if (Status.STARTED != st) {
        sb.append(" finished in ");
        sb.append(itr.getEndMillis() - itr.getStartMillis());
        sb.append(" ms");
        if (!Utils.isStringEmpty(tm.getDescription())) {
            sb.append("\n");
            for (int i = 0; i < identLevel; i++) {
                sb.append(" ");
            }
            sb.append(tm.getDescription());
        }
        if (tm.getInvocationCount() > 1) {
            sb.append(" (");
            sb.append(tm.getCurrentInvocationCount());
            sb.append(" of ");
            sb.append(tm.getInvocationCount());
            sb.append(")");
        }
        if (!Utils.isStringEmpty(stackTrace)) {
            sb.append("\n").append(stackTrace.substring(0, stackTrace.lastIndexOf(System.getProperty("line.separator"))));
        }
    } else {
        if (!isConfMethod && tm.getInvocationCount() > 1) {
            sb.append(" success: ");
            sb.append(tm.getSuccessPercentage());
            sb.append("%");
        }
    }
    log(sb.toString());
}