Unit Test Frameworks

   
TestCase

Description

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

Declaration

class TestCase : public Test, public TestFixture

Constructors/Destructors

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.

Attributes

const string m_name

The TestCase name ( private ).

Категории