Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable Tutorials.NET Framework Essentials.NET Programming securityC# In A Nutshell TutorialProgramming C.SharpMastering Visual Studio .NETASP.NETWeb Solutions based on ASP.NET and ADO.NETJava data objectsJava extreme programmingJava performance tuningJava development on pda's. Building applications for pocket pc and palm devicesJavaScript and DHTMLLearning UMLUMLLearning XMLCocoaProgramming CppPerl objects, references and modulesPerl tutorialPython tutorialPython. Text processingPocket pc network programmingPHP & MySQL. Building web database applicationsPHP & MySQL. Programming for beginnersPHP, MySQL and Apache in 24 hoursSoftware architecture in practiceSoftware engineering and computer gamesBuilding Solutions With the Microsoft .NET Compact FrameworkProgramming Microsoft Visual C# 2005ActionscriptMastering Delphi 7Ado.netPractical mod_perlPerl for bioinformaticsWeb ServicesPrinciples of Secure CodingC/C++ Secure ProgrammingASP.NET AJAXVisual C#Borland C++ Builder 6 Developer's Guide |
4.12 Unit Test Organization
|
|
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.
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[9] because you can select any AllTests class and run tests for a subset of your project. Assuming that you follow this pattern consistently, you can run the AllTests in your root directory to run every test in your application.
[9] IntelliJ IDEA allows you to right-click on any directory and run all tests in that package, thus eliminating the need to manually create an AllTests class.
|
Human fallibility is the main weakness of this technique. If you are not diligent, you will forget to add some tests to one of the AllTests classes. This can be overcome by writing a utility to automatically generate the AllTests classes. Yet another technique is to do the same thing dynamically: to write a class that sifts through a directory/package looking for TestXXXX classes and including them in the suite.
You might also want to consider whether the AllTests classes should run tests in subpackages, or just the current package. Here is a modification that allows you to choose the behavior you want based on a system property:
public static Test suite( ) {
TestSuite suite = new TestSuite( );
// add tests from the current directory
suite.addTest(new TestSuite(TestGame.class));
suite.addTest(new TestSuite(TestPerson.class));
// only test subdirectories if a system property is true
if ("true".equals(System.getProperty("test.subdirs"))) {
// add AllTests from any sub-packages
suite.addTest(com.oreilly.javaxp.junit.sub.AllTests.suite( ));
// suite.addTest(...) // continue for other sub-packages
}
return suite;
}
Recipe 3.16 shows an example of Ant's batchtest element.
![]() | Java extreme programming |