#include <testFixture.h>
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. |
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.
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.
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.
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.
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.
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.
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().
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().
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.
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. |
Definition at line 465 of file testFixture.h.
References dlr::test::TestFixture< FixtureType >::m_textOutputLineLength.
Referenced by dlr::test::TestFixture< FixtureType >::run().
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.
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().
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.
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. |
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).
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().
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.
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().
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.
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().
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().
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().