TestRunner.java revision 58a8b0aba2dec5695628a2bf25a3fae42c2c3533
1package junit.textui; 2 3 4import java.io.PrintStream; 5 6import junit.framework.Test; 7import junit.framework.TestResult; 8import junit.framework.TestSuite; 9import junit.runner.BaseTestRunner; 10import junit.runner.StandardTestSuiteLoader; 11import junit.runner.TestSuiteLoader; 12import junit.runner.Version; 13 14/** 15 * A command line based tool to run tests. 16 * <pre> 17 * java junit.textui.TestRunner [-wait] TestCaseClass 18 * </pre> 19 * TestRunner expects the name of a TestCase class as argument. 20 * If this class defines a static <code>suite</code> method it 21 * will be invoked and the returned test is run. Otherwise all 22 * the methods starting with "test" having no arguments are run. 23 * <p> 24 * When the wait command line argument is given TestRunner 25 * waits until the users types RETURN. 26 * <p> 27 * TestRunner prints a trace as the tests are executed followed by a 28 * summary at the end. 29 */ 30public class TestRunner extends BaseTestRunner { 31 private ResultPrinter fPrinter; 32 33 public static final int SUCCESS_EXIT= 0; 34 public static final int FAILURE_EXIT= 1; 35 public static final int EXCEPTION_EXIT= 2; 36 37 /** 38 * Constructs a TestRunner. 39 */ 40 public TestRunner() { 41 this(System.out); 42 } 43 44 /** 45 * Constructs a TestRunner using the given stream for all the output 46 */ 47 public TestRunner(PrintStream writer) { 48 this(new ResultPrinter(writer)); 49 } 50 51 /** 52 * Constructs a TestRunner using the given ResultPrinter all the output 53 */ 54 public TestRunner(ResultPrinter printer) { 55 fPrinter= printer; 56 } 57 58 /** 59 * Runs a suite extracted from a TestCase subclass. 60 */ 61 static public void run(Class testClass) { 62 run(new TestSuite(testClass)); 63 } 64 65 /** 66 * Runs a single test and collects its results. 67 * This method can be used to start a test run 68 * from your program. 69 * <pre> 70 * public static void main (String[] args) { 71 * test.textui.TestRunner.run(suite()); 72 * } 73 * </pre> 74 */ 75 static public TestResult run(Test test) { 76 TestRunner runner= new TestRunner(); 77 return runner.doRun(test); 78 } 79 80 /** 81 * Runs a single test and waits until the user 82 * types RETURN. 83 */ 84 static public void runAndWait(Test suite) { 85 TestRunner aTestRunner= new TestRunner(); 86 aTestRunner.doRun(suite, true); 87 } 88 89 /** 90 * Always use the StandardTestSuiteLoader. Overridden from 91 * BaseTestRunner. 92 */ 93 public TestSuiteLoader getLoader() { 94 return new StandardTestSuiteLoader(); 95 } 96 97 public void testFailed(int status, Test test, Throwable t) { 98 } 99 100 public void testStarted(String testName) { 101 } 102 103 public void testEnded(String testName) { 104 } 105 106 /** 107 * Creates the TestResult to be used for the test run. 108 */ 109 protected TestResult createTestResult() { 110 return new TestResult(); 111 } 112 113 public TestResult doRun(Test test) { 114 return doRun(test, false); 115 } 116 117 public TestResult doRun(Test suite, boolean wait) { 118 TestResult result= createTestResult(); 119 result.addListener(fPrinter); 120 long startTime= System.currentTimeMillis(); 121 suite.run(result); 122 long endTime= System.currentTimeMillis(); 123 long runTime= endTime-startTime; 124 fPrinter.print(result, runTime); 125 126 pause(wait); 127 return result; 128 } 129 130 protected void pause(boolean wait) { 131 if (!wait) return; 132 fPrinter.printWaitPrompt(); 133 try { 134 System.in.read(); 135 } 136 catch(Exception e) { 137 } 138 } 139 140 public static void main(String args[]) { 141 TestRunner aTestRunner= new TestRunner(); 142 try { 143 TestResult r= aTestRunner.start(args); 144 if (!r.wasSuccessful()) 145 System.exit(FAILURE_EXIT); 146 System.exit(SUCCESS_EXIT); 147 } catch(Exception e) { 148 System.err.println(e.getMessage()); 149 System.exit(EXCEPTION_EXIT); 150 } 151 } 152 153 /** 154 * Starts a test run. Analyzes the command line arguments and runs the given 155 * test suite. 156 */ 157 public TestResult start(String args[]) throws Exception { 158 String testCase= ""; 159 String method= ""; 160 boolean wait= false; 161 162 for (int i= 0; i < args.length; i++) { 163 if (args[i].equals("-wait")) 164 wait= true; 165 else if (args[i].equals("-c")) 166 testCase= extractClassName(args[++i]); 167 else if (args[i].equals("-m")) { 168 String arg= args[++i]; 169 int lastIndex= arg.lastIndexOf('.'); 170 testCase= arg.substring(0, lastIndex); 171 method= arg.substring(lastIndex + 1); 172 } else if (args[i].equals("-v")) 173 System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma"); 174 else 175 testCase= args[i]; 176 } 177 178 if (testCase.equals("")) 179 throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"); 180 181 try { 182 if (!method.equals("")) 183 return runSingleMethod(testCase, method, wait); 184 Test suite= getTest(testCase); 185 return doRun(suite, wait); 186 } catch (Exception e) { 187 throw new Exception("Could not create and run test suite: " + e); 188 } 189 } 190 191 protected TestResult runSingleMethod(String testCase, String method, boolean wait) throws Exception { 192 Class testClass= loadSuiteClass(testCase); 193 Test test= TestSuite.createTest(testClass, method); 194 return doRun(test, wait); 195 } 196 197 protected void runFailed(String message) { 198 System.err.println(message); 199 System.exit(FAILURE_EXIT); 200 } 201 202 public void setPrinter(ResultPrinter printer) { 203 fPrinter= printer; 204 } 205 206 207}