Using Test Runners

Running non-graphical tests from the command-line is as simple as possible. Make sure the junit-4.5.jar file, plus your test class, plus all needed resources (including, of course, the test object), are in your CLASSPATH. Then run org.junit.runner.JUnitCore, giving the absolute class names of all the Test Classes to execute. JUnitCore is JUnit version 4's test-runner manager. It examines each specified classes and invokes a test-runner to execute the test mods of the class.

Example 7.1. Executing tests from the command line

    export CLASSPATH=classes:/local/libjunit-4.5.jar
            java org.junit.runner.JUnitCore com.acme.LoadTester com.acme.PushTester

If you want your test class to be self-executable, you can write up a main method in your test class like this
public static void main(String args[]) {
      org.junit.runner.JUnitCore.main(LoadTester.class.getName());
    }
where your test class file is LoadTester.java.

As noted above, JUnitCore inspects the specified test classes and instructs test runner classes to execute the test methods. If you use JUnit version 4.5 and follow the methods explained in this guide, JUnitCore will always use a BlockJUnit4ClassRunner. If JUnitCore detects test classes or suites coded for JUnit version 3, it will select a JUnit-3-compatible test runner. There are also ways to specify the test runner implementation to use for a class (in order to use the graphical version 3 test-runner, for example), but since I am covering only version of JUnit, I am wrapping up the discussion here.


$Revision: 2422 $