Unit Test Frameworks - page 100



The class TestCase (see Figure C-18) represents a test object. Its purpose is to run test methods and thereby produce test results.

TestCase may be used in a number of ways. The simplest way to write a test object is to create a subclass of TestCase that overrides runTest() with a custom test method. To run the test object, call the run( ) method, which returns a TestResult .

More commonly, a subclass of TestFixture is created with multiple test methods. TestRunner then uses TestCaller to run the test methods, creating a new instance of TestCase for each one.

TestCase belongs to the namespace CppUnit . It is declared in TestCase.h and implemented in TestCase.cpp .

Figure C-18. The class TestCase, base class for all test objects


 class TestCase : public Test, public TestFixture 


TestCase(string name )

Constructs a TestCase with the given name.

TestCase( )

The default constructor used by TestCaller to create a temporary TestCase . Should not be used directly, since it creates a TestCase with no name.

~TestCase( )

A destructor.

Public Methods

virtual int countTestCases( ) const

Returns 1, the number of test cases contained in a base TestCase . Descendants of TestCase may contain multiple test cases.

string getName( ) const

Returns the TestCase name.

virtual TestResult *run( )

A convenience method that runs this TestCase and returns a new TestResult containing the results.

virtual void run(TestResult *result)

A method that runs the test, catches any failures or errors, and collects the results in result . The sequence of calls is: setUp( ) , runTest( ) , tearDown( ) . This method is the interface that the framework normally uses to run tests.

string toString( ) const

Returns a string representation of this TestCase , including the class name and TestCase name.

Protected/Private Methods

TestResult *defaultResult( )

A Protected method that returns a default empty TestResult . Used by run( ) .

virtual void runTest( )

A Protected method representing the actual test method. The default version does nothing. May be overridden by descendants of TestCase to implement actual tests. The overridden runTest( ) method contains the test assertions that result in success or failure of the unit test.

TestCase(const TestCase &other)

A copy constructor declared private to prevent its use.

TestCase& operator=(const TestCase &other)

A copy operator declared private to prevent its use.


const string m_name

The TestCase name ( private ).



The class TestDecorator (see Figure C-19) allows the functionality of a Test to be extended without subclassing it. TestDecorator implements the Test interface and thus can be run like any other Test . When constructed , it takes a reference to another Test that it "decorates," or wraps. A TestDecorator modifies the decorated Test by performing other operations before or after running it.

The classes RepeatedTest and TestSetUp are subclasses of TestDecorator and provide practical examples of its usage. The base implementation of TestDecorator simply is a wrapper for a Test and does not change its operation.

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

Figure C-19. The class TestDecorator


 class TestDecorator : public Test 


TestDecorator(Test *test)

Constructs a TestDecorator to decorate test .

~TestDecorator( )

A destructor. Does not delete the decorated Test .

Public Methods

int countTestCases( ) const

Returns the value of countTestCases( ) for the decorated Test .

string getName( ) const

Returns the name of the decorated Test .

void run(TestResult *result)

Calls run(result) on the decorated Test .

string toString( ) const

Returns the value of toString( ) for the decorated Test .

Protected/Private Methods

TestDecorator(const TestDecorator &other)

A copy constructor declared private to prevent its use.

void operator=(const TestDecorator &other)

A copy operator declared private to prevent its use.


Test *m_test

The Test decorated by this TestDecorator ( protected ).