FileDocCategorySizeDatePackage
TestRunner.javaAPI DocAndroid 1.5 API4424Wed May 06 22:42:02 BST 2009junit.textui

TestRunner

public class TestRunner extends BaseTestRunner
A command line based tool to run tests.
java junit.textui.TestRunner [-wait] TestCaseClass
TestRunner expects the name of a TestCase class as argument. If this class defines a static suite method it will be invoked and the returned test is run. Otherwise all the methods starting with "test" having no arguments are run.

When the wait command line argument is given TestRunner waits until the users types RETURN.

TestRunner prints a trace as the tests are executed followed by a summary at the end.

Fields Summary
private ResultPrinter
fPrinter
public static final int
SUCCESS_EXIT
public static final int
FAILURE_EXIT
public static final int
EXCEPTION_EXIT
Constructors Summary
public TestRunner()
Constructs a TestRunner.


	   	 
	  
		this(System.out);
	
public TestRunner(PrintStream writer)
Constructs a TestRunner using the given stream for all the output

		this(new ResultPrinter(writer));
	
public TestRunner(ResultPrinter printer)
Constructs a TestRunner using the given ResultPrinter all the output

		fPrinter= printer;
	
Methods Summary
protected TestResultcreateTestResult()
Creates the TestResult to be used for the test run.

		return new TestResult();
	
public TestResultdoRun(Test test)

		return doRun(test, false);
	
public TestResultdoRun(Test suite, boolean wait)

		TestResult result= createTestResult();
		result.addListener(fPrinter);
		long startTime= System.currentTimeMillis();
		suite.run(result);
		long endTime= System.currentTimeMillis();
		long runTime= endTime-startTime;
		fPrinter.print(result, runTime);

		pause(wait);
		return result;
	
public TestSuiteLoadergetLoader()
Always use the StandardTestSuiteLoader. Overridden from BaseTestRunner.

		return new StandardTestSuiteLoader();
	
public static voidmain(java.lang.String[] args)

		TestRunner aTestRunner= new TestRunner();
		try {
			TestResult r= aTestRunner.start(args);
			if (!r.wasSuccessful()) 
				System.exit(FAILURE_EXIT);
			System.exit(SUCCESS_EXIT);
		} catch(Exception e) {
			System.err.println(e.getMessage());
			System.exit(EXCEPTION_EXIT);
		}
	
protected voidpause(boolean wait)

		if (!wait) return;
		fPrinter.printWaitPrompt();
		try {
			System.in.read();
		}
		catch(Exception e) {
		}
	
public static voidrun(java.lang.Class testClass)
Runs a suite extracted from a TestCase subclass.

		run(new TestSuite(testClass));
	
public static TestResultrun(Test test)
Runs a single test and collects its results. This method can be used to start a test run from your program.
public static void main (String[] args) {
test.textui.TestRunner.run(suite());
}

		TestRunner runner= new TestRunner();
		return runner.doRun(test);
	
public static voidrunAndWait(Test suite)
Runs a single test and waits until the user types RETURN.

		TestRunner aTestRunner= new TestRunner();
		aTestRunner.doRun(suite, true);
	
protected voidrunFailed(java.lang.String message)

		System.err.println(message);
		System.exit(FAILURE_EXIT);
	
public voidsetPrinter(ResultPrinter printer)

		fPrinter= printer;
	
protected TestResultstart(java.lang.String[] args)
Starts a test run. Analyzes the command line arguments and runs the given test suite.

		String testCase= "";
		boolean wait= false;
		
		for (int i= 0; i < args.length; i++) {
			if (args[i].equals("-wait"))
				wait= true;
			else if (args[i].equals("-c")) 
				testCase= extractClassName(args[++i]);
			else if (args[i].equals("-v"))
				System.err.println("JUnit "+Version.id()+" by Kent Beck and Erich Gamma");
			else
				testCase= args[i];
		}
		
		if (testCase.equals("")) 
			throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");

		try {
			Test suite= getTest(testCase);
			return doRun(suite, wait);
		}
		catch(Exception e) {
			throw new Exception("Could not create and run test suite: "+e);
		}
	
public voidtestEnded(java.lang.String testName)

	
public voidtestFailed(int status, Test test, java.lang.Throwable t)

	
public voidtestStarted(java.lang.String testName)