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

The following examples show how to use org.testng.ITestResult#getEndMillis() . 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: 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 2
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 6 votes vote down vote up
private static long getTotalTime(List<ITestResult> results) {
    long start = 0, stop = 0;
    for (ITestResult result : results) {
        if (result == null)
            continue;
        long tmp_start = result.getStartMillis(), tmp_stop = result.getEndMillis();
        if (start == 0)
            start = tmp_start;
        else {
            start = Math.min(start, tmp_start);
        }

        if (stop == 0)
            stop = tmp_stop;
        else {
            stop = Math.max(stop, tmp_stop);
        }
    }
    return stop - start;
}
 
Example 3
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 6 votes vote down vote up
private static long getTotalTime(List<ITestResult> results) {
    long start = 0, stop = 0;
    for (ITestResult result : results) {
        if (result == null)
            continue;
        long tmp_start = result.getStartMillis(), tmp_stop = result.getEndMillis();
        if (start == 0)
            start = tmp_start;
        else {
            start = Math.min(start, tmp_start);
        }

        if (stop == 0)
            stop = tmp_stop;
        else {
            stop = Math.max(stop, tmp_stop);
        }
    }
    return stop - start;
}
 
Example 4
Source File: ConsoleReporter.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestSuccess(ITestResult result) {
	if (testStarted) {
		long testDuration = result.getEndMillis() - result.getStartMillis();
		print("------------------------------------------------------------------------");
		print("[TEST END]: " + result.getName()); 
		print("duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
		print("status: success");
		print("------------------------------------------------------------------------");
		testStarted = false;
	}
}
 
Example 5
Source File: ConsoleReporter.java    From difido-reports with Apache License 2.0 5 votes vote down vote up
@Override
public void onTestFailure(ITestResult result) {
	if (testStarted) {
		long testDuration = result.getEndMillis() - result.getStartMillis();
		print("------------------------------------------------------------------------");
		print("[TEST END]: " + result.getName()); 
		print("duration: " + TimeUnit.MILLISECONDS.toSeconds(testDuration) + " seconds");
		print("status: failure");
		print("------------------------------------------------------------------------");
		testStarted = false;
	}
}
 
Example 6
Source File: JUnitXMLReporter.java    From jgroups-raft with Apache License 2.0 5 votes vote down vote up
protected void addTest(Class<?> clazz, ITestResult result) {
    try {
        TestCase test_case=new TestCase(result.getStatus(), clazz.getName(), getMethodName(result),
                                        result.getStartMillis(), result.getEndMillis());
        switch(result.getStatus()) {
            case ITestResult.FAILURE:
            case ITestResult.SKIP:
                Throwable ex=result.getThrowable();
                if(ex != null) {
                    String failure_type=ex.getClass().getName();
                    String failure_msg=ex.getMessage();
                    String stack_trace=printException(ex);
                    test_case.setFailure(failure_type, failure_msg, stack_trace);
                }
                else
                    test_case.setFailure("exception", "SKIPPED", null);
                break;
        }

        synchronized(this) { // handle concurrent access by different threads, if test methods are run in parallel
            DataOutputStream output=tests.get(clazz);
            test_case.writeTo(output);
        }
    }
    catch(Exception e) {
        error(e.toString());
    }
}
 
Example 7
Source File: BenchmarkTestListener.java    From oxAuth with MIT License 5 votes vote down vote up
@Override
public void onTestSuccess(ITestResult result) {
	final String methodName = result.getMethod().getMethodName();
	final long takes = result.getEndMillis() - result.getStartMillis();

	Long totalTakes;
	Long totalInvoked;
	
	lock.lock();
	try {
		if (methodTakes.containsKey(methodName)) {
			totalTakes = methodTakes.get(methodName);
			totalInvoked = methodInvoked.get(methodName);

			totalTakes += takes;
			totalInvoked++;
		} else {
			methodNames.add(methodName);

			totalTakes = takes;
			totalInvoked = 1L;
		}
		methodTakes.put(methodName, totalTakes);
		methodInvoked.put(methodName, totalInvoked);
	} finally {
		lock.unlock();
	}
}
 
Example 8
Source File: TestNGRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
private void recordResult(ITestResult result, ResultType type, Throwable failure) {
  String stdOut = streamToString(rawStdOutBytes);
  String stdErr = streamToString(rawStdErrBytes);

  String className = result.getTestClass().getName();
  String methodName = getTestMethodNameWithParameters(result);

  long runTimeMillis = result.getEndMillis() - result.getStartMillis();
  results.add(
      new TestResult(className, methodName, runTimeMillis, type, failure, stdOut, stdErr));
}
 
Example 9
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 10
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 11
Source File: CustomisedListener.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onTestSuccess(ITestResult arg0) {
    long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis()));
    LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms");

}
 
Example 12
Source File: EarlReporter.java    From teamengine with Apache License 2.0 4 votes vote down vote up
/**
 * Creates EARL statements from the given test results. A test result is
 * described by an Assertion resource. The TestResult and TestCase resources
 * are linked to the Assertion in accord with the EARL schema; the latter is
 * also linked to a TestRequirement.
 * 
 * @param earl
 *            An RDF Model containing EARL statements.
 * @param results
 *            The results of invoking a collection of test methods.
 */
void processTestResults(Model earl, IResultMap results) {
    for (ITestResult tngResult : results.getAllResults()) {
        // create earl:Assertion
        long endTime = tngResult.getEndMillis();
        GregorianCalendar calTime = new GregorianCalendar(TimeZone.getDefault());
        calTime.setTimeInMillis(endTime);
        Resource assertion = earl.createResource("assert-" + ++this.resultCount,
                EARL.Assertion);
        assertion.addProperty(EARL.mode, EARL.AutomaticMode);
        assertion.addProperty(EARL.assertedBy, this.assertor);
        assertion.addProperty(EARL.subject, this.testSubject);
        // link earl:TestResult to earl:Assertion
        Resource earlResult = earl.createResource("result-" + this.resultCount,
                EARL.TestResult);
        earlResult.addProperty(DCTerms.date, earl.createTypedLiteral(calTime));
        switch (tngResult.getStatus()) {
        case ITestResult.FAILURE:
            earlResult.addProperty(DCTerms.description, getDetailMessage(tngResult));
            if (AssertionError.class.isInstance(tngResult.getThrowable())) {
                earlResult.addProperty(EARL.outcome, EARL.Fail);
            } else { // an exception occurred
                earlResult.addProperty(EARL.outcome, EARL.CannotTell);
            }
            processResultAttributes(earlResult, tngResult);
            break;
        case ITestResult.SKIP:
            earlResult.addProperty(DCTerms.description, getDetailMessage(tngResult));
            earlResult.addProperty(EARL.outcome, EARL.NotTested);
            break;
        default:
            earlResult.addProperty(EARL.outcome, EARL.Pass);
            break;
        }
        assertion.addProperty(EARL.result, earlResult);
        // link earl:TestCase to earl:Assertion and earl:TestRequirement
        String testMethodName = tngResult.getMethod().getMethodName();
        String testClassName = tngResult.getTestClass().getName().replaceAll("\\.", "/");
        StringBuilder testCaseId = new StringBuilder(testClassName);
        testCaseId.append('#').append(testMethodName);
        Resource testCase = earl.createResource(testCaseId.toString(), EARL.TestCase);
        testCase.addProperty(DCTerms.title, breakIntoWords(testMethodName));
        String testDescr = tngResult.getMethod().getDescription();
        if (null != testDescr && !testDescr.isEmpty()) {
            testCase.addProperty(DCTerms.description, testDescr);
        }
        assertion.addProperty(EARL.test, testCase);
        String testReqName = tngResult.getTestContext().getName().replaceAll("\\s", "-");
        earl.createResource(testReqName).addProperty(DCTerms.hasPart, testCase);
    }
}