dlr::test::TestFixture< FixtureType > Class Template Reference

The TestFixture class helps with unit testing by coordinating the execution of a test suite. More...

#include <testFixture.h>

Inheritance diagram for dlr::test::TestFixture< FixtureType >:
[legend]
Collaboration diagram for dlr::test::TestFixture< FixtureType >:
[legend]

List of all members.

Public Types

typedef FixtureType TestFixtureType
 The TestFixtureType typedef is used by helper macros, and may eventually be useful for other things, too.
typedef void(FixtureType::* TestFunctionPtr )()
 The TestFunctionPtr typedef is used to conveniently work with member functions of the derived class, which will be run as individual tests.

Public Member Functions

 TestFixture (const std::string &testFixtureName)
 This constructor sets the name of the group of tests run by this test fixture.
virtual ~TestFixture ()
 The destructor cleans up any resources and destroys the class instance.
virtual void registerTest (const std::string &testName, TestFunctionPtr testFunctionPtr)
 This member function is used to indicate which member functions of the subclass should be run as tests.
virtual bool run ()
 This member function runs all of the registered tests, keeps track of the results, and prints any diagnostic output, finally returning a bool indicating success (true) or failure (false).

Protected Member Functions

virtual void announceTestFinish ()
 This protected member function prints a message indicating that the test fixture has run all of its tests.
virtual void announceTestStart ()
 This protected member function prints a message indicating that the test fixture is about to run its tests.
virtual std::string buildFailureMessage (size_t failureIndex, const std::string &testName, const std::string &failureType, const std::string &whatMessage)
 This protected member function builds a diagnostic string describing a test failure.
void printTestStatistics (int errorCount, const std::vector< std::string > &testMessages)
 This protected member function prints the final output indicating how many tests passed/failed, etc.
virtual void setUp (const std::string &)
 This protected member function is called immediately before each test is run.
virtual void tearDown (const std::string &)
 This protected member function is called immediately after each test is run.

Protected Attributes

int m_textOutputLineLength
 This variable controls at what character position text output will be wrapped.
int m_verbosity
 This variable controls the amount of text output produced while running tests.


Detailed Description

template<class FixtureType>
class dlr::test::TestFixture< FixtureType >

The TestFixture class helps with unit testing by coordinating the execution of a test suite.

To use this class, you should create a derived class using the following slightly scary inheritance:

class MyTestClass : public dlr::TestFixture<MyTestClass>;

The derived class should define individual tests as member functions:

void MyTestClass::testFunction0() {...};

These tests should be registered with the TestFixture, probably in the constructor of the derived class. You can do this the easy way, by using the provided macro:

DLR_TEST_REGISTER_MEMBER(testFunction0);

Note that this macro will fail if you call it from any context that is not a constructor or member function of the derived class.

Or you can do it the hard way by calling the registerTest member function directly:

this->registerTest("testFunction0", &MyTestClass::testFunction0);

Within the tests, use the DLR_TEST_ASSERT macros on pass/fail conditions:

DLR_TEST_ASSERT(residual < m_errorThreshold);

DLR_TEST_ASSERT_EXCEPTION(ValueException, functionWhichThrows());

Then you can run the tests by calling the run() method, which your derived class will have inherited from TestFixture<>.

Definition at line 70 of file testFixture.h.


Member Typedef Documentation

template<class FixtureType>
typedef FixtureType dlr::test::TestFixture< FixtureType >::TestFixtureType

The TestFixtureType typedef is used by helper macros, and may eventually be useful for other things, too.

Definition at line 81 of file testFixture.h.

template<class FixtureType>
typedef void(FixtureType::* dlr::test::TestFixture< FixtureType >::TestFunctionPtr)()

The TestFunctionPtr typedef is used to conveniently work with member functions of the derived class, which will be run as individual tests.


Constructor & Destructor Documentation

template<class FixtureType >
dlr::test::TestFixture< FixtureType >::TestFixture ( const std::string &  testFixtureName  )  [inline, explicit]

This constructor sets the name of the group of tests run by this test fixture.

Parameters:
testFixtureName This argument specifies the name by which this test fixture will be referred to in status output.

Definition at line 288 of file testFixture.h.

template<class FixtureType >
dlr::test::TestFixture< FixtureType >::~TestFixture (  )  [inline, virtual]

The destructor cleans up any resources and destroys the class instance.

Definition at line 304 of file testFixture.h.


Member Function Documentation

template<class FixtureType >
void dlr::test::TestFixture< FixtureType >::announceTestFinish (  )  [inline, protected, virtual]

This protected member function prints a message indicating that the test fixture has run all of its tests.

If a subclass overrides TestFixture::run(), then this method should generally be called at the end of the run() method.

Definition at line 433 of file testFixture.h.

References dlr::test::TestFixture< FixtureType >::m_verbosity.

Referenced by dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType >
void dlr::test::TestFixture< FixtureType >::announceTestStart (  )  [inline, protected, virtual]

This protected member function prints a message indicating that the test fixture is about to run its tests.

If a subclass overrides TestFixture::run(), then this method should generally be called at the beginning of the run() method.

Definition at line 449 of file testFixture.h.

References dlr::test::TestFixture< FixtureType >::m_verbosity.

Referenced by dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType >
std::string dlr::test::TestFixture< FixtureType >::buildFailureMessage ( size_t  failureIndex,
const std::string &  testName,
const std::string &  failureType,
const std::string &  whatMessage 
) [inline, protected, virtual]

This protected member function builds a diagnostic string describing a test failure.

Derived classes can override this to customize test failure output.

Parameters:
failureIndex This argument should be set to the number of the failure. It will be included in the test message. Increment this number each time you call buildFailureMessage().
testName This argument specifies the name of the failed test.
failureType This argument specifies the type of failure. Suggestions are "Failed test" for tests which fail, and "Error in test" for tests which crash.
whatMessage This argument is set to the "what()" output of the caught exception, and should usually provide information about the nature of the failure.
Returns:
The return value is a formatted string describing the error. The default format looks something like this: "1) Failed test foo::bar ..."

Definition at line 465 of file testFixture.h.

References dlr::test::TestFixture< FixtureType >::m_textOutputLineLength.

Referenced by dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType >
void dlr::test::TestFixture< FixtureType >::printTestStatistics ( int  errorCount,
const std::vector< std::string > &  testMessages 
) [inline, protected]

This protected member function prints the final output indicating how many tests passed/failed, etc.

Parameters:
errorCount This argument specifies how many tests aborted by throwing exceptions.
testMessages This argument is a vector of failure messages, one for each test that didn't pass.

Definition at line 543 of file testFixture.h.

Referenced by dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType>
virtual void dlr::test::TestFixture< FixtureType >::registerTest ( const std::string &  testName,
TestFunctionPtr  testFunctionPtr 
) [virtual]

This member function is used to indicate which member functions of the subclass should be run as tests.

Call it once for each test.

Parameters:
testName This argument specifies the name of the test. It will be used to identify the test in text output, and it will be passed to both the setUp() member function (before the test is run) and the tearDown() member function (after the test is run).
testFunctionPtr This argument should be a pointer to the member function which should be run as a test.

template<class FixtureType >
bool dlr::test::TestFixture< FixtureType >::run (  )  [inline, virtual]

This member function runs all of the registered tests, keeps track of the results, and prints any diagnostic output, finally returning a bool indicating success (true) or failure (false).

Returns:
The return value will be true if all of the registered tests pass, false otherwise.

Implements dlr::test::RunnableObject.

Definition at line 329 of file testFixture.h.

References dlr::test::TestFixture< FixtureType >::announceTestFinish(), dlr::test::TestFixture< FixtureType >::announceTestStart(), dlr::test::TestFixture< FixtureType >::buildFailureMessage(), dlr::test::TestFixture< FixtureType >::m_textOutputLineLength, dlr::test::TestFixture< FixtureType >::printTestStatistics(), dlr::test::TestFixture< FixtureType >::setUp(), and dlr::test::TestFixture< FixtureType >::tearDown().

template<class FixtureType>
virtual void dlr::test::TestFixture< FixtureType >::setUp ( const std::string &   )  [inline, protected, virtual]

This protected member function is called immediately before each test is run.

The argument will be set to the name of the test which is about to be run.

Parameters:
testName The argument will be set to the name of the test which is about to be run, allowing the setUp() method of the derived class to modify its behavior for each test.

Definition at line 219 of file testFixture.h.

Referenced by dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType>
virtual void dlr::test::TestFixture< FixtureType >::tearDown ( const std::string &   )  [inline, protected, virtual]

This protected member function is called immediately after each test is run.

The argument will be set to the name of the test which was just run.

Parameters:
testName The argument will be set to the name of the test which was just run, allowing the tearDown() method of the derived class to modify its behavior for each test.

Definition at line 231 of file testFixture.h.

Referenced by dlr::test::TestFixture< FixtureType >::run().


Member Data Documentation

template<class FixtureType>
int dlr::test::TestFixture< FixtureType >::m_textOutputLineLength [protected]

This variable controls at what character position text output will be wrapped.

Definition at line 240 of file testFixture.h.

Referenced by dlr::test::TestFixture< FixtureType >::buildFailureMessage(), and dlr::test::TestFixture< FixtureType >::run().

template<class FixtureType>
int dlr::test::TestFixture< FixtureType >::m_verbosity [protected]

This variable controls the amount of text output produced while running tests.

Lower numbers mean less output.

Definition at line 246 of file testFixture.h.

Referenced by dlr::test::TestFixture< FixtureType >::announceTestFinish(), and dlr::test::TestFixture< FixtureType >::announceTestStart().


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

Generated on Wed Nov 25 01:02:08 2009 for dlrTest Utility Library by  doxygen 1.5.8