CppUnit - The Unit Testing Library

CppUnit project page | FAQ

Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
TestFactoryRegistry Class Reference
Creating TestSuite

Registry for TestFactory. More...

`#include <TestFactoryRegistry.h>`

Inheritance diagram for TestFactoryRegistry:
[legend]
Collaboration diagram for TestFactoryRegistry:
[legend]

## Public Member Functions
| TestFactoryRegistry (std::string name)
| Constructs the registry with the specified name.
virtual | ~TestFactoryRegistry ()
| Destructor.
virtual Test * | makeTest ()
| Returns a new TestSuite that contains the registered test.
void | addTestToSuite (TestSuite *suite)
| Adds the registered tests to the specified suite.
void | registerFactory (TestFactory *factory)
| Adds the specified TestFactory to the registry.
void | unregisterFactory (TestFactory *factory)
void | addRegistry (const std::string &name)
void | registerFactory (const std::string &name, TestFactory *factory)
| Adds the specified TestFactory with a specific name (DEPRECATED).
Public Member Functions inherited from TestFactory
virtual | ~TestFactory ()

## Static Public Member Functions
static TestFactoryRegistry & | getRegistry (const std::string &name="All Tests")
| Returns a named registry.
static bool | isValid ()

## Private Types
typedef std::set< TestFactory *, std::less< TestFactory * > > | Factories

## Private Member Functions
| TestFactoryRegistry (const TestFactoryRegistry &copy)
void | operator= (const TestFactoryRegistry &copy)

## Private Attributes
Factories | m_factories
std::string | m_name

## Detailed Description

Registry for TestFactory.

Notes that the registry DON'T assumes lifetime control for any registered tests anymore.

The default registry is the registry returned by getRegistry() with the default name parameter value.

To register tests, use the macros:

- CPPUNIT_TEST_SUITE_REGISTRATION(): to add tests in the default registry.
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(): to add tests in a named registry.

Example 1: retreiving a suite that contains all the test registered with CPPUNIT_TEST_SUITE_REGISTRATION().

CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
CppUnit::TestSuite *suite = registry.makeTest();

Example 2: retreiving a suite that contains all the test registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" ).

CppUnit::TestFactoryRegistry &mathRegistry = CppUnit::TestFactoryRegistry::getRegistry( "Math" );
CppUnit::TestSuite *mathSuite = mathRegistry.makeTest();

Example 3: creating a test suite hierarchy composed of unnamed registration and named registration:

- All Tests
  - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Graph" )
  - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )
  - tests registered with CPPUNIT_TEST_SUITE_REGISTRATION

CppUnit::TestSuite *rootSuite = new CppUnit::TestSuite( "All tests" );
rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Graph" ).makeTest() );
rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Math" ).makeTest() );
CppUnit::TestFactoryRegistry::getRegistry().addTestToSuite( rootSuite );
TestFactoryRegistry::makeTest
virtual Test * makeTest()
Returns a new TestSuite that contains the registered test.
Definition TestFactoryRegistry.cpp:135

The same result can be obtained with:

CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
registry.addRegistry( "Graph" );
registry.addRegistry( "Math" );
CppUnit::TestSuite *suite = registry.makeTest();

Since a TestFactoryRegistry is a TestFactory, the named registries can be registered in the unnamed registry, creating the hierarchy links.

See also
  TestSuiteFactory, AutoRegisterSuite
  CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION

## Member Typedef Documentation

## ◆ Factories

typedef std::set<TestFactory *, std::less<TestFactory*> > TestFactoryRegistry::Factories
| private

## Constructor & Destructor Documentation

## ◆ TestFactoryRegistry() [1/2]

TestFactoryRegistry::TestFactoryRegistry | ( | std::string | name | ) |

Constructs the registry with the specified name.

Parameters

  name | Name of the registry. It is the name of TestSuite returned by makeTest().

## ◆ ~TestFactoryRegistry()

TestFactoryRegistry::~TestFactoryRegistry | ( |  | ) |
| virtual

Destructor.

## ◆ TestFactoryRegistry() [2/2]

TestFactoryRegistry::TestFactoryRegistry | ( | const TestFactoryRegistry & | copy | ) |
| private

## Member Function Documentation

## ◆ addRegistry()

void TestFactoryRegistry::addRegistry | ( | const std::string & | name | ) |

Adds a registry to the registry.

Convenience method to help create test hierarchy. See TestFactoryRegistry detail for examples of use. Calling this method is equivalent to:

this->registerFactory( TestFactoryRegistry::getRegistry( name ) );
TestFactoryRegistry::registerFactory
void registerFactory(TestFactory *factory)
Adds the specified TestFactory to the registry.
Definition TestFactoryRegistry.cpp:114
TestFactoryRegistry::getRegistry
static TestFactoryRegistry & getRegistry(const std::string &name="All Tests")
Returns a named registry.
Definition TestFactoryRegistry.cpp:99

Parameters

  name | Name of the registry to add.

## ◆ addTestToSuite()

void TestFactoryRegistry::addTestToSuite | ( | TestSuite * | suite | ) |

Adds the registered tests to the specified suite.

Parameters

  suite | Suite the tests are added to.

## ◆ getRegistry()

TestFactoryRegistry & TestFactoryRegistry::getRegistry | ( | const std::string & | name = "All Tests" | ) |
| static

Returns a named registry.

If the name is left to its default value, then the registry that is returned is the one used by CPPUNIT_TEST_SUITE_REGISTRATION(): the 'top' level registry.

Parameters

  name | Name of the registry to return.

Returns
  Registry. If the registry does not exist, it is created with the specified name.

## ◆ isValid()

bool TestFactoryRegistry::isValid | ( |  | ) |
| static

Tests if the registry is valid.

This method should be used when unregistering test factory on static variable destruction to ensure that the registry has not been already destroyed (in that case there is no need to unregister the test factory).

You should not concern yourself with this method unless you are writing a class like AutoRegisterSuite.

Returns
  `true` if the specified registry has not been destroyed, otherwise returns `false`.

See also
  AutoRegisterSuite.

## ◆ makeTest()

Test * TestFactoryRegistry::makeTest | ( |  | ) |
| virtual

Returns a new TestSuite that contains the registered test.

Returns
  A new TestSuite which contains all the test added using registerFactory(TestFactory *).

Implements TestFactory.

## ◆ operator=()

void TestFactoryRegistry::operator= | ( | const TestFactoryRegistry & | copy | ) |
| private

## ◆ registerFactory() [1/2]

void TestFactoryRegistry::registerFactory | ( | const std::string & | name,
|  | TestFactory * | factory )

Adds the specified TestFactory with a specific name (DEPRECATED).

Parameters

  name | Name associated to the factory.
  factory | Factory to register.

Deprecated
  Use registerFactory( TestFactory *) instead.

## ◆ registerFactory() [2/2]

void TestFactoryRegistry::registerFactory | ( | TestFactory * | factory | ) |

Adds the specified TestFactory to the registry.

Parameters

  factory | Factory to register.

## ◆ unregisterFactory()

void TestFactoryRegistry::unregisterFactory | ( | TestFactory * | factory | ) |

Removes the specified TestFactory from the registry.

The specified factory is not destroyed.

Parameters

  factory | Factory to remove from the registry.

Todo
  Address case when trying to remove a TestRegistryFactory.

## Member Data Documentation

## ◆ m_factories

Factories TestFactoryRegistry::m_factories
| private

## ◆ m_name

std::string TestFactoryRegistry::m_name
| private

The documentation for this class was generated from the following files:

- TestFactoryRegistry.h
- TestFactoryRegistry.cpp

| Send comments to: CppUnit Developers