Search in sources :

Example 16 with TestResult

use of junit.framework.TestResult in project felix by apache.

the class JunitCommand method execute.

/**
 * Executes the command.
 * @param line the command line
 * @param out the output stream
 * @param err the error stream
 * @see org.apache.felix.shell.Command#execute(java.lang.String, java.io.PrintStream, java.io.PrintStream)
 */
public void execute(String line, PrintStream out, PrintStream err) {
    line = line.substring(getName().length()).trim();
    List tr = null;
    if (line.equals("all")) {
        if (m_runner.getTests() == null) {
            err.println("No tests to execute");
            return;
        } else {
            out.println("Executing " + getNamesFromTests(m_runner.getTests()));
            tr = m_runner.run();
        }
    } else {
        try {
            Long bundleId = new Long(line);
            if (m_runner.getTests(bundleId.longValue()) == null) {
                err.println("No tests to execute");
                return;
            } else {
                out.println("Executing " + getNamesFromTests(m_runner.getTests(bundleId.longValue())));
                tr = m_runner.run(bundleId.longValue());
            }
        } catch (NumberFormatException e) {
            err.println("Unable to parse id " + line);
            return;
        }
    }
    ListIterator it = tr.listIterator();
    while (it.hasNext()) {
        TestResult result = (TestResult) it.next();
        if (result.failureCount() != 0) {
            TestFailure fail = (TestFailure) result.failures().nextElement();
            out.println(fail.trace());
            return;
        }
    }
}
Also used : TestFailure(junit.framework.TestFailure) List(java.util.List) ArrayList(java.util.ArrayList) TestResult(junit.framework.TestResult) ListIterator(java.util.ListIterator)

Example 17 with TestResult

use of junit.framework.TestResult in project felix by apache.

the class JunitExtender method run.

/**
 * Runs tests.
 * @return the list of {@link TestResult}
 * @see org.apache.felix.ipojo.junit4osgi.OSGiJunitRunner#run()
 */
public synchronized List run() {
    List /*<TestResult>*/
    results = new ArrayList(/*<TestResult>*/
    m_suites.size());
    Iterator /*<Entry<Bundle, List<Class>>>*/
    it = m_suites.entrySet().iterator();
    while (it.hasNext()) {
        Entry /*<Bundle, List<Class>>*/
        entry = (Entry) it.next();
        Bundle bundle = (Bundle) entry.getKey();
        List /*<Class>*/
        list = (List) m_suites.get(bundle);
        for (int i = 0; i < list.size(); i++) {
            Test test = createTestFromClass((Class) list.get(i), bundle);
            TestResult tr = doRun(test);
            results.add(tr);
        }
    }
    return results;
}
Also used : Entry(java.util.Map.Entry) Test(junit.framework.Test) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) TestResult(junit.framework.TestResult)

Example 18 with TestResult

use of junit.framework.TestResult in project android_frameworks_base by crdroidandroid.

the class UiAutomatorTestRunner method start.

/**
     * Called after all test classes are in place, ready to test
     */
protected void start() {
    TestCaseCollector collector = getTestCaseCollector(this.getClass().getClassLoader());
    try {
        collector.addTestClasses(mTestClasses);
    } catch (ClassNotFoundException e) {
        // will be caught by uncaught handler
        throw new RuntimeException(e.getMessage(), e);
    }
    if (mDebug) {
        Debug.waitForDebugger();
    }
    mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
    mHandlerThread.setDaemon(true);
    mHandlerThread.start();
    UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
    automationWrapper.connect();
    long startTime = SystemClock.uptimeMillis();
    TestResult testRunResult = new TestResult();
    ResultReporter resultPrinter;
    String outputFormat = mParams.getString("outputFormat");
    List<TestCase> testCases = collector.getTestCases();
    Bundle testRunOutput = new Bundle();
    if ("simple".equals(outputFormat)) {
        resultPrinter = new SimpleResultPrinter(System.out, true);
    } else {
        resultPrinter = new WatcherResultPrinter(testCases.size());
    }
    try {
        automationWrapper.setRunAsMonkey(mMonkey);
        mUiDevice = UiDevice.getInstance();
        mUiDevice.initialize(new ShellUiAutomatorBridge(automationWrapper.getUiAutomation()));
        String traceType = mParams.getString("traceOutputMode");
        if (traceType != null) {
            Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
            if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
                String filename = mParams.getString("traceLogFilename");
                if (filename == null) {
                    throw new RuntimeException("Name of log file not specified. " + "Please specify it using traceLogFilename parameter");
                }
                Tracer.getInstance().setOutputFilename(filename);
            }
            Tracer.getInstance().setOutputMode(mode);
        }
        // add test listeners
        testRunResult.addListener(resultPrinter);
        // add all custom listeners
        for (TestListener listener : mTestListeners) {
            testRunResult.addListener(listener);
        }
        // run tests for realz!
        for (TestCase testCase : testCases) {
            prepareTestCase(testCase);
            testCase.run(testRunResult);
        }
    } catch (Throwable t) {
        // catch all exceptions so a more verbose error message can be outputted
        resultPrinter.printUnexpectedError(t);
        testRunOutput.putString("shortMsg", t.getMessage());
    } finally {
        long runTime = SystemClock.uptimeMillis() - startTime;
        resultPrinter.print(testRunResult, runTime, testRunOutput);
        automationWrapper.disconnect();
        automationWrapper.setRunAsMonkey(false);
        mHandlerThread.quit();
    }
}
Also used : Bundle(android.os.Bundle) Tracer(com.android.uiautomator.core.Tracer) TestResult(junit.framework.TestResult) UiAutomationShellWrapper(com.android.uiautomator.core.UiAutomationShellWrapper) HandlerThread(android.os.HandlerThread) TestCase(junit.framework.TestCase) ShellUiAutomatorBridge(com.android.uiautomator.core.ShellUiAutomatorBridge) TestListener(junit.framework.TestListener)

Example 19 with TestResult

use of junit.framework.TestResult in project bnd by bndtools.

the class Activator method test.

/**
	 * The main test routine.
	 *
	 * @param bundle The bundle under test or null
	 * @param testnames The names to test
	 * @param report The report writer or null
	 * @return # of errors
	 */
int test(Bundle bundle, String testnames, Writer report) {
    trace("testing bundle %s with %s", bundle, testnames);
    Bundle fw = context.getBundle(0);
    try {
        bundle = findHost(bundle);
        List<String> names = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(testnames, " ,");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (!names.contains(token))
                names.add(token);
        }
        List<TestReporter> reporters = new ArrayList<TestReporter>();
        final TestResult result = new TestResult();
        Tee systemOut = new Tee(System.err);
        Tee systemErr = new Tee(System.err);
        systemOut.capture(isTrace()).echo(true);
        systemErr.capture(isTrace()).echo(true);
        final PrintStream originalOut = System.out;
        final PrintStream originalErr = System.err;
        System.setOut(systemOut.getStream());
        System.setErr(systemErr.getStream());
        trace("changed streams");
        try {
            BasicTestReport basic = new BasicTestReport(this, systemOut, systemErr, result);
            add(reporters, result, basic);
            if (port > 0) {
                add(reporters, result, jUnitEclipseReport);
            }
            if (report != null) {
                add(reporters, result, new JunitXmlReport(report, bundle, basic));
            }
            for (TestReporter tr : reporters) {
                tr.setup(fw, bundle);
            }
            TestSuite suite = createSuite(bundle, names, result);
            try {
                trace("created suite %s #%s", suite.getName(), suite.countTestCases());
                List<Test> flattened = new ArrayList<Test>();
                int realcount = flatten(flattened, suite);
                for (TestReporter tr : reporters) {
                    tr.begin(flattened, realcount);
                }
                trace("running suite %s", suite);
                suite.run(result);
            } catch (Throwable t) {
                trace("%s", t);
                result.addError(suite, t);
            } finally {
                for (TestReporter tr : reporters) {
                    tr.end();
                }
            }
        } catch (Throwable t) {
            System.err.println("exiting " + t);
            t.printStackTrace(System.err);
        } finally {
            System.setOut(originalOut);
            System.setErr(originalErr);
            trace("unset streams");
        }
        System.err.println("Tests run  : " + result.runCount());
        System.err.println("Passed     : " + (result.runCount() - result.errorCount() - result.failureCount()));
        System.err.println("Errors     : " + result.errorCount());
        System.err.println("Failures   : " + result.failureCount());
        return result.errorCount() + result.failureCount();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}
Also used : PrintStream(java.io.PrintStream) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) TestResult(junit.framework.TestResult) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) TestSuite(junit.framework.TestSuite) Test(junit.framework.Test)

Example 20 with TestResult

use of junit.framework.TestResult in project android_frameworks_base by crdroidandroid.

the class TestSuiteBuilderTest method runSuite.

private SuiteExecutionRecorder runSuite(TestSuiteBuilder builder) {
    TestSuite suite = builder.build();
    SuiteExecutionRecorder recorder = new SuiteExecutionRecorder();
    TestResult result = new TestResult();
    result.addListener(recorder);
    suite.run(result);
    return recorder;
}
Also used : TestSuite(junit.framework.TestSuite) TestResult(junit.framework.TestResult)

Aggregations

TestResult (junit.framework.TestResult)108 TestSuite (junit.framework.TestSuite)33 Test (junit.framework.Test)19 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)17 TestCase (junit.framework.TestCase)14 TestFailure (junit.framework.TestFailure)12 Test (org.junit.Test)10 TestListener (junit.framework.TestListener)9 ArrayList (java.util.ArrayList)8 PrintStream (java.io.PrintStream)7 RepeatedTest (junit.extensions.RepeatedTest)7 Enumeration (java.util.Enumeration)6 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 ShellUiAutomatorBridge (com.android.uiautomator.core.ShellUiAutomatorBridge)5 Tracer (com.android.uiautomator.core.Tracer)5 UiAutomationShellWrapper (com.android.uiautomator.core.UiAutomationShellWrapper)5 AssertionFailedError (junit.framework.AssertionFailedError)5 IOException (java.io.IOException)4 Iterator (java.util.Iterator)4