How to use runAndWait method of junit.textui.TestRunner class

Best junit code snippet using junit.textui.TestRunner.runAndWait

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/******************************************************************************
2 * Copyright (c) 2007 Stefan Franke, Robert Hanussek, Benjamin Keil,          *
3 *                    Steffen Kieß, Johannes Langauf,                         *
4 *                    Christoph Marian Müller, Igor Podolskiy,                *
5 *                    Tilmann Scheller, Michael Starzmann, Markus Wittlinger  *
6 * All rights reserved. This program and the accompanying materials           *
7 * are made available under the terms of the Eclipse Public License v1.0      *
8 * which accompanies this distribution, and is available at                   *
9 * http://www.eclipse.org/legal/epl-v10.html                                  *
10 ******************************************************************************/
11
12package org.codecover.junit3.text;
13
14import junit.framework.Test;
15import junit.framework.TestCase;
16import junit.framework.TestResult;
17import junit.framework.TestSuite;
18import junit.runner.BaseTestRunner;
19
20import org.codecover.junit.HelperMethods;
21import org.codecover.junit3.JUnitResultListener;
22import org.codecover.junit3.JUnitResultListenerMethod;
23import org.codecover.junit3.JUnitResultListenerTestCase;
24
25/**
26 * This is a CodeCover TestRunner which encapsulates a
27 * {@link junit.textui.TestRunner} of JUnit 3.8.x.<br>
28 * <br>
29 * Usage from commandline:
30 * 
31 * <pre>
32 * org.codecover.junit3.text.TestRunner [-wait] [-methodsAsTestCases] (&lt;Testclass&gt;|-c &lt;Testclass in VA/Java style&gt;)
33 * </pre>
34 * 
35 * The <code>&lt;Testclass.class&gt;</code> can either be a {@link TestSuite}
36 * or a {@link TestCase}.
37 * 
38 * @author Christoph Müller
39 * 
40 * @version 1.0 ($Id: TestRunner.java 1 2007-12-12 17:37:26Z t-scheller $)
41 * 
42 * @see junit.textui.TestRunner
43 * @see BaseTestRunner
44 */
45public class TestRunner extends junit.textui.TestRunner {
46
47    private boolean useMethodsAsTestcases = false;
48
49    private JUnitResultListener usedListener;
50
51    /**
52     * Starts a new {@link TestRunner} with the given arguments.
53     * 
54     * @param args
55     *            see {@link TestRunner} for description.
56     *            
57     * @see junit.textui.TestRunner#main(String[]) Copied from there.
58     */
59    public static void main(String args[]) {
60        boolean useMethodsAsTestcases = HelperMethods.useMethodsAsArguments(args);
61
62        TestRunner aTestRunner = new TestRunner(useMethodsAsTestcases);
63        try {
64            TestResult r = aTestRunner.start(args);
65            if (!r.wasSuccessful()) {
66                System.exit(FAILURE_EXIT);
67            }
68            System.exit(SUCCESS_EXIT);
69        } catch (Exception e) {
70            System.err.println(e.getMessage());
71            System.exit(EXCEPTION_EXIT);
72        }
73    }
74
75    /**
76     * @see junit.textui.TestRunner#run(Test) Copied from there.
77     * @see #run(Test, boolean)
78     */
79    public static TestResult run(Test test) {
80        return run(test, false);
81    }
82
83    /**
84     * Runs the suite.
85     * 
86     * @param test
87     *            The {@link Test}.
88     * @param useMethodsAsTestcases
89     *            use test case methods or test cases as test cases for the
90     *            protocol
91     * @return The {@link TestResult} of the run. 
92     * 
93     * @see junit.textui.TestRunner#run(Test) Adapted from there.
94     */
95    public static TestResult run(Test test, boolean useMethodsAsTestcases) {
96        return new TestRunner(useMethodsAsTestcases).doRun(test, false);
97    }
98
99    /**
100     * @see junit.textui.TestRunner#runAndWait(Test) Copied from there.
101     * @see #runAndWait(Test, boolean)
102     */
103    public static void runAndWait(Test suite) {
104        runAndWait(suite, false);
105    }
106
107    /**
108     * Runs the suite and waits for a <code>&lt;RETURN&gt;</code>.
109     * 
110     * @param suite
111     *            The {@link Test}.
112     * @param useMethodsAsTestcases
113     *            use test case methods or test cases as test cases for the
114     *            protocol
115     * @see junit.textui.TestRunner#runAndWait(Test) Adapted from there.
116     */
117    public static void runAndWait(Test suite, boolean useMethodsAsTestcases) {
118        new TestRunner(useMethodsAsTestcases).doRun(suite, true);
119    }
120
121    /**
122     * 
123     * @param useMethodsAsTestcases
124     *            true &rarr; methods of a {@link TestCase} are used as test
125     *            cases<br>
126     *            false &rarr; the whole {@link TestCase} is used as a test
127     *            case.
128     */
129    public TestRunner(boolean useMethodsAsTestcases) {
130        this.useMethodsAsTestcases = useMethodsAsTestcases;
131    }
132
133    public TestResult doRun(Test suite, boolean wait) {
134        TestResult testResult = super.doRun(suite, wait);
135        this.usedListener.endLastOpenTestCase();
136        return testResult;
137    }
138    
139    protected TestResult createTestResult() {
140        TestResult newResult = super.createTestResult();
141        if (this.useMethodsAsTestcases) {
142            this.usedListener = new JUnitResultListenerMethod();
143        } else {
144            this.usedListener = new JUnitResultListenerTestCase();
145        }
146        newResult.addListener(this.usedListener);
147        return newResult;
148    }
149}
150
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger runAndWait code on LambdaTest Cloud Grid

Execute automation tests with runAndWait on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)