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


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
public static final int
public static final int
public static final int
Constructors Summary
public TestRunner()
Constructs a TestRunner.

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();
		long startTime= System.currentTimeMillis();;
		long endTime= System.currentTimeMillis();
		long runTime= endTime-startTime;
		fPrinter.print(result, runTime);

		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()) 
		} catch(Exception e) {
protected voidpause(boolean wait)

		if (!wait) return;
		try {;
		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) {;

		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)

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 "" by Kent Beck and Erich Gamma");
				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)