March 2003
Intermediate to advanced
288 pages
7h 4m
English
You want to organize all of your tests consistently.
Create a test case that runs all tests in the current package and subpackages. Duplicate this pattern for all packages in your application.
Some Java IDEs allow you to automatically run all tests in your project or in a specific package, negating the need for this recipe.
Example 4-5 shows an example of the technique
outlined in the solution just presented. It runs all of the test
suites in the current package as well as delegating to each
AllTests class in
immediate subpackages.
Example 4-5. AllTests example
package com.oreilly.javaxp.junit;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Runs all test suites in the current package and sub-packages.
*/
public class AllTests extends TestCase {
/**
* @return a suite containing all tests in this package
* and subpackages.
*/
public static Test suite( ) {
TestSuite suite = new TestSuite( );
// add tests from the current directory. This requires manual
// updates, which is the main weakness of this technique
suite.addTest(new TestSuite(TestGame.class));
suite.addTest(new TestSuite(TestPerson.class));
// add AllTests from any sub-packages
suite.addTest(com.oreilly.javaxp.junit.sub.AllTests.suite( ));
// suite.addTest(...) // continue for other sub-packages
return suite;
}
}This technique can be useful when using an IDE[26]
because you can select any AllTests class and run tests ...
Read now
Unlock full access