636
|
1 |
package org.junit.runner;
|
|
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.List;
|
|
5 |
|
|
6 |
import junit.runner.Version;
|
|
7 |
import org.junit.internal.JUnitSystem;
|
|
8 |
import org.junit.internal.RealSystem;
|
|
9 |
import org.junit.internal.TextListener;
|
|
10 |
import org.junit.internal.runners.JUnit38ClassRunner;
|
|
11 |
import org.junit.runner.notification.Failure;
|
|
12 |
import org.junit.runner.notification.RunListener;
|
|
13 |
import org.junit.runner.notification.RunNotifier;
|
|
14 |
|
|
15 |
/**
|
|
16 |
* <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests,
|
|
17 |
* JUnit 3.8.x tests, and mixtures. To run tests from the command line, run
|
|
18 |
* <code>java org.junit.runner.JUnitCore TestClass1 TestClass2 ...</code>.
|
|
19 |
* For one-shot test runs, use the static method {@link #runClasses(Class[])}.
|
|
20 |
* If you want to add special listeners,
|
|
21 |
* create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
|
|
22 |
*
|
|
23 |
* @see org.junit.runner.Result
|
|
24 |
* @see org.junit.runner.notification.RunListener
|
|
25 |
* @see org.junit.runner.Request
|
|
26 |
*/
|
|
27 |
public class JUnitCore {
|
|
28 |
|
|
29 |
private RunNotifier fNotifier;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Create a new <code>JUnitCore</code> to run tests.
|
|
33 |
*/
|
|
34 |
public JUnitCore() {
|
|
35 |
fNotifier= new RunNotifier();
|
|
36 |
}
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Run the tests contained in the classes named in the <code>args</code>.
|
|
40 |
* If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
|
|
41 |
* Write feedback while tests are running and write
|
|
42 |
* stack traces for all failed tests after the tests all complete.
|
|
43 |
* @param args names of classes in which to find tests to run
|
|
44 |
*/
|
|
45 |
public static void main(String... args) {
|
|
46 |
runMainAndExit(new RealSystem(), args);
|
|
47 |
}
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Do not use. Testing purposes only.
|
|
51 |
* @param system
|
|
52 |
*/
|
|
53 |
public static void runMainAndExit(JUnitSystem system, String... args) {
|
|
54 |
Result result= new JUnitCore().runMain(system, args);
|
|
55 |
system.exit(result.wasSuccessful() ? 0 : 1);
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Run the tests contained in <code>classes</code>. Write feedback while the tests
|
|
60 |
* are running and write stack traces for all failed tests after all tests complete. This is
|
|
61 |
* similar to {@link #main(String[])}, but intended to be used programmatically.
|
|
62 |
* @param classes Classes in which to find tests
|
|
63 |
* @return a {@link Result} describing the details of the test run and the failed tests.
|
|
64 |
*/
|
|
65 |
public static Result runClasses(Class<?>... classes) {
|
|
66 |
return new JUnitCore().run(classes);
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Do not use. Testing purposes only.
|
|
71 |
* @param system
|
|
72 |
*/
|
|
73 |
public Result runMain(JUnitSystem system, String... args) {
|
|
74 |
system.out().println("JUnit version " + Version.id());
|
|
75 |
List<Class<?>> classes= new ArrayList<Class<?>>();
|
|
76 |
List<Failure> missingClasses= new ArrayList<Failure>();
|
|
77 |
for (String each : args)
|
|
78 |
try {
|
|
79 |
classes.add(Class.forName(each));
|
|
80 |
} catch (ClassNotFoundException e) {
|
|
81 |
system.out().println("Could not find class: " + each);
|
|
82 |
Description description= Description.createSuiteDescription(each);
|
|
83 |
Failure failure= new Failure(description, e);
|
|
84 |
missingClasses.add(failure);
|
|
85 |
}
|
|
86 |
RunListener listener= new TextListener(system);
|
|
87 |
addListener(listener);
|
|
88 |
Result result= run(classes.toArray(new Class[0]));
|
|
89 |
for (Failure each : missingClasses)
|
|
90 |
result.getFailures().add(each);
|
|
91 |
return result;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* @return the version number of this release
|
|
96 |
*/
|
|
97 |
public String getVersion() {
|
|
98 |
return Version.id();
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Run all the tests in <code>classes</code>.
|
|
103 |
* @param classes the classes containing tests
|
|
104 |
* @return a {@link Result} describing the details of the test run and the failed tests.
|
|
105 |
*/
|
|
106 |
public Result run(Class<?>... classes) {
|
|
107 |
return run(Request.classes(classes));
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Run all the tests contained in <code>request</code>.
|
|
112 |
* @param request the request describing tests
|
|
113 |
* @return a {@link Result} describing the details of the test run and the failed tests.
|
|
114 |
*/
|
|
115 |
public Result run(Request request) {
|
|
116 |
return run(request.getRunner());
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Run all the tests contained in JUnit 3.8.x <code>test</code>. Here for backward compatibility.
|
|
121 |
* @param test the old-style test
|
|
122 |
* @return a {@link Result} describing the details of the test run and the failed tests.
|
|
123 |
*/
|
|
124 |
public Result run(junit.framework.Test test) {
|
|
125 |
return run(new JUnit38ClassRunner(test));
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Do not use. Testing purposes only.
|
|
130 |
*/
|
|
131 |
public Result run(Runner runner) {
|
|
132 |
Result result= new Result();
|
|
133 |
RunListener listener= result.createListener();
|
|
134 |
addFirstListener(listener);
|
|
135 |
try {
|
|
136 |
fNotifier.fireTestRunStarted(runner.getDescription());
|
|
137 |
runner.run(fNotifier);
|
|
138 |
fNotifier.fireTestRunFinished(result);
|
|
139 |
} finally {
|
|
140 |
removeListener(listener);
|
|
141 |
}
|
|
142 |
return result;
|
|
143 |
}
|
|
144 |
|
|
145 |
private void addFirstListener(RunListener listener) {
|
|
146 |
fNotifier.addFirstListener(listener);
|
|
147 |
}
|
|
148 |
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Add a listener to be notified as the tests run.
|
|
152 |
* @param listener the listener to add
|
|
153 |
* @see org.junit.runner.notification.RunListener
|
|
154 |
*/
|
|
155 |
public void addListener(RunListener listener) {
|
|
156 |
fNotifier.addListener(listener);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Remove a listener.
|
|
161 |
* @param listener the listener to remove
|
|
162 |
*/
|
|
163 |
public void removeListener(RunListener listener) {
|
|
164 |
fNotifier.removeListener(listener);
|
|
165 |
}
|
|
166 |
}
|