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

The following examples show how to use org.testng.ITestResult#getThrowable() . 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: TestCaseTestListener.java    From agent with MIT License 6 votes vote down vote up
@Override
public void onTestSkipped(ITestResult tr) {
    TestDescription testDesc = (TestDescription) tr.getTestContext().getAttribute(TEST_DESCRIPTION);
    log.warn("[{}]onTestSkipped, testcaseId: {}", testDesc.getDeviceId(), testDesc.getTestcaseId());

    Testcase testcase = new Testcase();
    testcase.setId(testDesc.getTestcaseId());
    testcase.setEndTime(new Date());
    testcase.setStatus(Testcase.SKIP_STATUS);

    if (CONFIG_FAIL_ERR_INFO.get() != null) { // 前置任务执行失败
        testcase.setFailInfo(CONFIG_FAIL_ERR_INFO.get());
    } else if (tr.getThrowable() != null) { // dependsOnMethods执行失败。实际前置任务执行失败,也是!=null,但为了获取更加详细的信息,已经在上面的if作了处理
        testcase.setFailInfo(tr.getThrowable().getMessage());
    } else { // 正常情况下的跳过,throw SkipException导致
        testcase.setFailInfo(tr.getThrowable().getMessage());
        testcase.setFailImgPath(uploadScreenshot(testDesc));
        testcase.setVideoPath(uploadVideo(testDesc));
    }

    ServerClient.getInstance().updateTestcase(testDesc.getDeviceTestTaskId(), testcase);
}
 
Example 2
Source File: TestNG_ConsoleRunner.java    From Selenium-Framework-Design-in-Data-Driven-Testing with MIT License 6 votes vote down vote up
/**
 * getTestMessage method
 *
 * @param tr
 * @return String
 */
public String getTestMessage(ITestResult tr) {
    Boolean found = false;

    if ( tr != null && tr.getThrowable() != null ) {
        found = true;
    }

    if ( found == true ) {
        return tr.getThrowable().getMessage() == null ? "" : tr.getThrowable().getMessage();
    }

    else {
        return "";
    }
}
 
Example 3
Source File: CustomJUnitReportListener.java    From heat with Apache License 2.0 6 votes vote down vote up
private void setFailedTcAttribute(XMLStringBuffer doc, ITestResult failedTestCase) {
    Properties attributesFailedTestSuites = new Properties();
    String tcName = ((HashMap<String, String>) failedTestCase.getParameters()[0]).get(PROP_TEST_ID);
    attributesFailedTestSuites.setProperty(XMLConstants.ATTR_NAME, tcName);
    long elapsedTimeMillis = failedTestCase.getEndMillis() - failedTestCase.getStartMillis();
    testRunningTotalTime += elapsedTimeMillis;
    Throwable t = failedTestCase.getThrowable();
    doc.push(XMLConstants.TESTCASE, attributesFailedTestSuites);
    if (t != null) {
        attributesFailedTestSuites.setProperty(XMLConstants.ATTR_TYPE, t.getClass().getName());
        String message = t.getMessage();
        if ((message != null) && (message.length() > 0)) {
            attributesFailedTestSuites.setProperty(XMLConstants.ATTR_MESSAGE, encodeAttr(message)); // ENCODE
        }
        doc.push(XMLConstants.FAILURE, attributesFailedTestSuites);
        doc.addCDATA(Utils.stackTrace(t, false)[0]);
        doc.pop();
    } else {
        doc.addEmptyElement(XMLConstants.FAILURE); // THIS IS AN ERROR
    }
    doc.pop();
}
 
Example 4
Source File: MPConfigTest.java    From microprofile-context-propagation with Apache License 2.0 5 votes vote down vote up
@AfterMethod
public void afterMethod(Method m, ITestResult result) {
    System.out.println("<<< END " + m.getClass().getSimpleName() + '.' + m.getName() + (result.isSuccess() ? " SUCCESS" : " FAILED"));
    Throwable failure = result.getThrowable();
    if (failure != null) {
        failure.printStackTrace(System.out);
    }
}
 
Example 5
Source File: PulsarTestListener.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestFailure(ITestResult result) {
    System.out.format("!!!!!!!!! FAILURE-- %s.%s(%s)-------\n", result.getTestClass(),
            result.getMethod().getMethodName(), Arrays.toString(result.getParameters()));

    if (result.getThrowable() instanceof ThreadTimeoutException) {
        System.out.println("====== THREAD DUMPS ======");
        System.out.println(ThreadDumpUtil.buildThreadDiagnosticString());
    }
}
 
Example 6
Source File: ExtentIReporterSuiteClassListenerAdapter.java    From extentreports-testng-adapter with Apache License 2.0 5 votes vote down vote up
private void buildTestNodes(ExtentTest suiteTest, IResultMap tests, Status status) {
    ExtentTest testNode;
    ExtentTest classNode;

    if (tests.size() > 0) {
        for (ITestResult result : tests.getAllResults()) {
            String className = result.getInstance().getClass().getSimpleName();

            if (classTestMap.containsKey(className)) {
                classNode = classTestMap.get(className);
            } else {
                classNode = suiteTest.createNode(className);
                classTestMap.put(className, classNode);
            }

            testNode = classNode.createNode(result.getMethod().getMethodName(),
                    result.getMethod().getDescription());

            String[] groups = result.getMethod().getGroups();
            ExtentTestCommons.assignGroups(testNode, groups);
            
            if (result.getThrowable() != null) {
                testNode.log(status, result.getThrowable());
            } else {
                testNode.log(status, "Test " + status.toString().toLowerCase() + "ed");
            }

            testNode.getModel().getLogContext().getAll().forEach(x -> x.setTimestamp(getTime(result.getEndMillis())));
            testNode.getModel().setStartTime(getTime(result.getStartMillis()));
            testNode.getModel().setEndTime(getTime(result.getEndMillis()));
        }
    }
}
 
Example 7
Source File: TckTest.java    From microprofile-context-propagation with Apache License 2.0 5 votes vote down vote up
@AfterMethod
public void afterMethod(Method m, ITestResult result) {
    System.out.println("<<< END " + m.getClass().getSimpleName() + '.' + m.getName() + (result.isSuccess() ? " SUCCESS" : " FAILED"));
    Throwable failure = result.getThrowable();
    if (failure != null) {
        failure.printStackTrace(System.out);
    }
}
 
Example 8
Source File: RetryAnalyzer.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRetriable(ITestResult result) {
    if (result.getThrowable() instanceof WebDriverException) {
        return true;
    }
    return super.isRetriable(result);
}
 
Example 9
Source File: AtsTestngListener.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
private void endTestcaseWithFailureStatus( ITestResult testResult ) {

        try {
            //Check if the test was successfully started, if not - make it started and then end it with failure
            String testName = testResult.getMethod().toString();
            if (!testName.equals(currentTestcaseName)) {
                startTestcase(testResult);
            }

            sendTestEndEventToAgents();

            // if this is an assertion error, we need to log it
            Throwable failureException = testResult.getThrowable();
            if (failureException instanceof AssertionError) {
                if (failureException.getMessage() != null) {
                    logger.error(ExceptionUtils.getExceptionMsg(failureException));
                } else {
                    logger.error("Received java.lang.AssertionError with null message");
                }
            } else {
                logger.error(MSG__TEST_FAILED, testResult.getThrowable());
            }

            currentTestcaseName = null;
            lastTestcaseResult = TestCaseResult.FAILED.toInt();
            // end test case
            logger.endTestcase(TestCaseResult.FAILED);
        } catch (Exception e) {
            logger.fatal("UNEXPECTED EXCEPTION IN AtsTestngListener@endTestcaseWithFailureStatus", e);
        }

    }
 
Example 10
Source File: CDIContextTest.java    From microprofile-context-propagation with Apache License 2.0 5 votes vote down vote up
@AfterMethod
public void afterMethod(Method m, ITestResult result) {
    System.out.println("<<< END " + m.getClass().getSimpleName() + '.' + m.getName() + (result.isSuccess() ? " SUCCESS" : " FAILED"));
    Throwable failure = result.getThrowable();
    if (failure != null) {
        failure.printStackTrace(System.out);
    }
}
 
Example 11
Source File: EarlReporter.java    From teamengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a description of an error or exception that occurred while
 * executing a test. The details are extracted from the associated
 * <code>Throwable</code> or its underlying cause.
 * 
 * @param result
 *            Information about a test result.
 * @return A String providing diagnostic information.
 */
String getDetailMessage(ITestResult result) {
    if (null == result.getThrowable()) {
        return "No details available.";
    }
    String msg = result.getThrowable().getMessage();
    if (null == msg && null != result.getThrowable().getCause()) {
        msg = result.getThrowable().getCause().getMessage();
    } else {
        msg = result.getThrowable().toString();
    }
    return msg;
}
 
Example 12
Source File: RetryAnalyzer.java    From carina with Apache License 2.0 5 votes vote down vote up
@Override
public boolean retry(ITestResult result) {
    incrementRunCount();
    if (result.getThrowable() != null && result.getThrowable().getMessage() != null
            && result.getThrowable().getMessage().startsWith(SpecialKeywords.ALREADY_PASSED)) {
        LOGGER.debug("AlreadyPassedRetryAnalyzer: " + result.getMethod().getRetryAnalyzer(result) + "Method: " + result.getMethod().getMethodName() + "; Incremented retryCount: " + getRunCount());
        return false;
    }

    LOGGER.debug("RetryAnalyzer: " + result.getMethod().getRetryAnalyzer(result) + "Method: " + result.getMethod().getMethodName() + "; Incremented retryCount: " + getRunCount());
    if (getRunCount() <= getMaxRetryCountForTest() && !Jira.isRetryDisabled(result)) {
        return true;
    }
    return false;
}
 
Example 13
Source File: SOAPTest.java    From carina-demo with Apache License 2.0 5 votes vote down vote up
private Throwable getTestResultException(ITestResult testResult) {
    Throwable testResultException = testResult.getThrowable();
    if (testResultException instanceof InvocationTargetException) {
        testResultException = ((InvocationTargetException) testResultException).getCause();
    }
    return testResultException;
}
 
Example 14
Source File: AbstractTestNGSpringContextTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Throwable getTestResultException(ITestResult testResult) {
	Throwable testResultException = testResult.getThrowable();
	if (testResultException instanceof InvocationTargetException) {
		testResultException = ((InvocationTargetException) testResultException).getCause();
	}
	return testResultException;
}
 
Example 15
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 5 votes vote down vote up
@Override
public void onConfigurationFailure(ITestResult iTestResult) {
    if (isSuppressConfigEvent(iTestResult)) {
        return;
    }
    String suiteUid = getSuiteUid(iTestResult.getTestContext());
    if (isAfterSuiteConfigMethod(iTestResult)) {
        String suiteTitle = getCurrentSuiteTitle(iTestResult.getTestContext());
        getLifecycle().fire(new TestSuiteStartedEvent(suiteUid, suiteTitle).withTitle(suiteTitle));
    }
    Throwable throwable = iTestResult.getThrowable();
    createConfigEvent(iTestResult);
    getLifecycle().fire(new TestCaseFailureEvent().withThrowable(throwable));
    fireFinishTest();
    if (isAfterSuiteConfigMethod(iTestResult)) {
        getLifecycle().fire(new TestSuiteFinishedEvent(suiteUid));
    }
}
 
Example 16
Source File: CarinaListener.java    From carina with Apache License 2.0 4 votes vote down vote up
private void onTestFinish(ITestResult result) {
    try {
        // clear all kind of temporary properties
        R.CONFIG.clearTestProperties();
        R.TESTDATA.clearTestProperties();
        R.DATABASE.clearTestProperties();
        R.EMAIL.clearTestProperties();
        R.REPORT.clearTestProperties();
        R.ZAFIRA.clearTestProperties();
        
        LOGGER.debug("Test result is : " + result.getStatus());
        // result status == 2 means failure, status == 3 means skip. We need to quit driver anyway for failure and skip
        if ((automaticDriversCleanup && !hasDependencies(result)) || result.getStatus() == 2 || result.getStatus() == 3) {
            quitDrivers(Phase.BEFORE_METHOD, Phase.METHOD);
        }

        // TODO: improve later removing duplicates with AbstractTestListener
        // handle Zafira already passed exception for re-run and do nothing.
        // maybe return should be enough
        if (result.getThrowable() != null && result.getThrowable().getMessage() != null
                && result.getThrowable().getMessage().startsWith(SpecialKeywords.ALREADY_PASSED)) {
            // [VD] it is prohibited to release TestInfoByThread in this
            // place.!
            return;
        }

        // handle CarinaListener->SkipExecution
        if (result.getThrowable() != null && result.getThrowable().getMessage() != null
                && result.getThrowable().getMessage().startsWith(SpecialKeywords.SKIP_EXECUTION)) {
            // [VD] it is prohibited to release TestInfoByThread in this
            // place.!
            return;
        }

        List<String> tickets = Jira.getTickets(result);
        result.setAttribute(SpecialKeywords.JIRA_TICKET, tickets);
        Jira.updateAfterTest(result);

        // we shouldn't deregister info here as all retries will not work
        // TestNamingUtil.releaseZafiraTest();

    } catch (Exception e) {
        LOGGER.error("Exception in CarinaListener->onTestFinish!", e);
    }
}
 
Example 17
Source File: AllureTestListener.java    From allure1 with Apache License 2.0 4 votes vote down vote up
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    Throwable throwable = iTestResult.getThrowable();
    getLifecycle().fire(new TestCaseCanceledEvent().withThrowable(throwable));
    fireFinishTest();
}
 
Example 18
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * generate the XML report given what we know from all the test results
 */
protected void generateReport() throws IOException {
    for (Map.Entry<Class, List<ITestResult>> entry : classes.entrySet()) {
        Class clazz = entry.getKey();
        List<ITestResult> results = entry.getValue();

        int num_failures = getFailures(results);
        int num_skips = getSkips(results);
        int num_errors = getErrors(results);
        long total_time = getTotalTime(results);

        String file_name = output_dir + File.separator + "TEST-" + clazz.getName();
        if (suffix != null)
            file_name = file_name + "-" + suffix;
        file_name = file_name + ".xml";
        FileWriter out = new FileWriter(file_name, false); // don't append, overwrite
        try {
            out.write(XML_DEF + "\n");

            out.write("\n<testsuite " + " failures=\"" + num_failures + "\" errors=\"" + num_errors + "\" skips=\"" + num_skips + "\" name=\"" + clazz.getName());
            if (suffix != null)
                out.write(" (" + suffix + ")");
            out.write("\" tests=\"" + results.size() + "\" time=\"" + (total_time / 1000.0) + "\">");

            out.write("\n<properties>");
            Properties props = System.getProperties();

            for (Map.Entry<Object, Object> tmp : props.entrySet()) {
                out.write("\n    <property name=\"" + tmp.getKey() + "\"" + " value=\"" + tmp.getValue() + "\"/>");
            }
            out.write("\n</properties>\n");

            for (ITestResult result : results) {
                if (result == null)
                    continue;
                long time = result.getEndMillis() - result.getStartMillis();
                out.write("\n    <testcase classname=\"" + clazz.getName());
                if (suffix != null)
                    out.write(" (" + suffix + ")");
                out.write("\" name=\"" + result.getMethod().getMethodName() + "\" time=\"" + (time / 1000.0) + "\">");

                Throwable ex = result.getThrowable();

                switch (result.getStatus()) {
                case ITestResult.SUCCESS:
                case ITestResult.SUCCESS_PERCENTAGE_FAILURE:
                    break;
                case ITestResult.FAILURE:
                    writeFailure("failure", result.getMethod().getMethod(), ex, "exception", out);
                    break;
                case ITestResult.SKIP:
                    writeFailure("error", result.getMethod().getMethod(), ex, "SKIPPED", out);
                    break;
                default:
                    writeFailure("error", result.getMethod().getMethod(), ex, "exception", out);
                }

                out.write("\n</testcase>");
            }

            Tuple<ByteArrayOutputStream, ByteArrayOutputStream> stdout = outputs.get(clazz);
            if (stdout != null) {
                ByteArrayOutputStream system_out = stdout.getVal1();
                ByteArrayOutputStream system_err = stdout.getVal2();
                writeOutput(out, system_out.toString(), 1);
                out.write("\n");
                writeOutput(out, system_err.toString(), 2);
            }

            out.write("\n</testsuite>\n");
        } finally {
            out.close();
        }
    }

}
 
Example 19
Source File: TestNGAdapter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public void onTestFailure(final ITestResult arg0) {
  this.hasHadFailure = true;
  this.error = arg0.getThrowable();
  this.rc.notifyEnd(makeDescription(arg0), this.error);
}
 
Example 20
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());
}