There are other ways to feed TestSuites to Test Runners, but, in my opinion, the strategy of returning a TestSuite via TestCase.suite(), as explained in the Making your TestCases Test-Runner-Friendly chapter is by far the best. Therefore, in this chapter I will explain how to use Test Runners with TestCases/TestSuites implemented in that way.
You can use any of the executable programs that JUnit comes with to select your TestCase class (with an implementation of suite()), and it will do exactly what you want.
You need to have the junit jar file and your own classes available in your classpath.
# To run your TestCase suite:
java -cp whatever:/path/to/junit.jar junit.textui.TestRunner MyTestClass
# To run your TestCase suite in a Gui:
java -cp whatever:/path/to/junit.jar junit.swingui.TestRunner MyTestClass
# To run the TestRunner Gui and select your suite or test methods
# from within the Gui:
java -cp whatever:/path/to/junit.jar junit.swingui.TestRunner
(Note that you can't supply more than one TestCase on the command-line, though after the Swing TestRunner is running, you can load multiple TestCases into it).
It is also very easy to make your TestCase class execute by just running it.
# This runs the suite:
java -cp whatever:/path/to/junit.jar org.blah.your.TestClass
# This does the same thing with the fancy Gui:
java -cp whatever:/path/to/junit.jar org.blah.your.TestClass -g
To have this ability, just copy the following code into any TestCase source file that you have.
static public void main(String[] sa) {
if (sa.length > 0 && sa[0].startsWith("-g")) {
junit.swingui.TestRunner.run(SMTPTest.class);
} else {
junit.textui.TestRunner runner = new junit.textui.TestRunner();
System.exit(
runner.run(runner.getTest(SMTPTest.class.getName())).
wasSuccessful() ? 0 : 1
);
}
}
Just replace SMTPTest with the name of your class. (I use the full package paths to the JUnit classes so as to not pollute your namespace and require you to add import statements).
Enjoy.