The abstract class TestFactory (see Figure C-20) defines the interface for a factory that produces Test objects.

TestFactory belongs to the namespace CppUnit . It is declared in extensions/TestFactory.h . It is abstract and has no implementation.

Figure C-20. The abstract class TestFactory


 class TestFactory 


virtual ~TestFactory( )

A destructor.

Public Methods

virtual Test* makeTest( ) = 0

A pure, virtual method to create a Test .

Protected/Private Methods






The class TestFactoryRegistry (see Figure C-21) is a subclass of TestFactory . It acts as both a registry and a factory for Test objects. It registers Test s, and it produces TestSuite s containing registered Test s. Rather than containing the registered Test objects themselves , it contains a TestFactory for each one.

The default registry is a TestFactoryRegistry named "All Tests." Named instances of TestFactoryRegistry may also be created.

The macro CPPUNIT_TEST_SUITE_REGISTRATION( ) takes a Test and adds a TestFactory for it to the default registry. The macro CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ) similarly adds a TestFactory to a named registry. Calling a registry's makeTest( ) method creates a TestSuite containing all the registered Test s, demonstrating the main usefulness of TestFactoryRegistry .

The singleton NamedRegistries manages all instances of TestFactoryRegistry .

The following code snippet registers the Test class BookTest in the default registry and creates a TestSuite containing it:


TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry( );

TestSuite *suite = registry.makeTest( ); 

TestFactoryRegistry belongs to the namespace CppUnit . It is declared in extensions/TestFactoryRegistry.h and implemented in TestFactoryRegistry.cpp .

Figure C-21. The class TestFactoryRegistry


 class TestFactoryRegistry : public TestFactory 


TestFactoryRegistry(string name = "All Tests")

A constructor. If no name is provided, a default registry named "All Tests" is created.

virtual ~TestFactoryRegistry( )

A destructor. Each TestFactory contained by this TestFactoryRegistry is deleted if NamedRegistries indicates that it has not already been deleted. This prevents double deletion of a TestFactory .

Public Methods

void addTestToSuite(TestSuite *suite)

Adds the registered Test objects to a preexisting TestSuite .

static TestFactoryRegistry &getRegistry( )

Returns the default TestFactoryRegistry named "All Tests."

static TestFactoryRegistry &getRegistry(const string &name)

Returns a TestFactoryRegistry with the given name. If the registry doesn't already exist, it is created.

virtual Test* makeTest( )

Creates a TestSuite containing the registered Test objects. The TestSuite has the same name as the TestFactoryRegistry . For example, the TestSuite created by the default registry is named "All Tests."

void registerFactory(TestFactory *factory)

Adds a TestFactory to the TestFactoryRegistry .

void registerFactory(const string &name, TestFactory *factory)

A deprecated method that adds a TestFactory to the TestFactoryRegistry with the given name. The previous version of registerFactory( ) should be used instead.

Protected/Private Methods

TestFactoryRegistry(const TestFactoryRegistry &copy)

A copy constructor declared private to prevent its use.

void operator=(const TestFactoryRegistry &copy)

A copy operator declared private to prevent its use.


typedef map<string, TestFactory *> Factories

Defines the type Factories as a map of TestFactory objects by name ( private ).

 Factories m_factories 

Registered TestFactory objects (private) .

string m_name

The name of this TestFactoryRegistry ( private ).