Simpatico
v1.10
|
Template for a TestRunner that runs test methods of an associated UnitTest.
A instance of UnitTestRunner<MyTest> holds an array of pointers to all of the test methods of a class MyTest that is a subclass of UnitTest. Each such test method must return void and take zero parameters. The addTestMethod() method is used to register a test method with the UnitTestRunner instantiation, by adding a pointer to a test method to this array. The run() method runs all of the registered test methods in sequence.
To run a set of unit tests one must:
The boilerplate code required to define a UnitTestRunner class may be simplified by using set preprocessor macros that are defined at the end of this file.
Here is an example of the code to to define a subclass of UnitTestRunner<MyTest>, associated with a subclass MyTest of UnitTest, and then run all of its test methods, written without using any preprocessor macros:
Note that, by convention:
Calling the run() method of MyTest_Runner will then run all of the tests.
The following series of preprocessor macros may be used to generate the definition of the MyTest_Runner class in the above example, and to create an instance of this class:
The macro TEST_BEGIN(TestClass) generates the beginning of the class definition for subclass MyTest_Runner of UnitTestRunner<TestClass>. The TEST_ADD(TestClass, Method) adds a specified method of the associated class TestClass to the constructor of the new UnitTestRunner class. The TEST_END macro closes both the constructor definition and the class definition. After expansion, the resulting code is completely equivalent to that given in the previous example, after the definition of MyTest.
The name of the UnitTestRunner class created by these preprocessor macros is created by appending the standard suffix "_Runner" to the name of the unit test class. Thus, in the above example, the TestRunner subclass is named MyTest_Runner. This TestRunner subclass name may be referred directly, using this name, or by using the preprocessor macro TEST_RUNNER(TestClass), which expands to the name of the test runner class, e.g., to TestClass_Runner. In the above example, this macro is used as a class name to instantiate an instance of the of required test runner.
Definition at line 110 of file UnitTestRunner.h.
#include <UnitTestRunner.h>
Public Types | |
typedef void(UnitTestClass::* | MethodPtr) () |
Pointer to a test method of the associated UnitTest class. More... | |
Public Member Functions | |
UnitTestRunner () | |
Constructor. More... | |
~UnitTestRunner () | |
Destructor. More... | |
void | addTestMethod (MethodPtr methodPtr) |
Register a test method of the associated unit test class. More... | |
int | nTestMethod () |
Return the number of registered test methods. More... | |
void | method (unsigned int i) |
Run test method number i. More... | |
virtual int | run () |
Run all registered test methods in the order added. More... | |
![]() | |
TestRunner () | |
Constructor. More... | |
virtual | ~TestRunner () |
Destructor. More... | |
void | recordFailure () |
Increment counter for failed tests, and that of parent (if any). More... | |
void | recordSuccess () |
Increment counter for successful tests, and that of parent (if any). More... | |
void | setParent (TestRunner &parent) |
Set another TestRunner as the parent. More... | |
TestRunner & | parent () |
Return the parent object, if any. More... | |
bool | hasParent () const |
Does this object have a parent? More... | |
int | nSuccess () const |
Return number of successful tests run. More... | |
int | nFailure () const |
Return number of failed tests run. More... | |
void | report () const |
If this object has no parent, report success and failure counters. More... | |
bool | isIoProcessor () const |
Is this the IO processor of an MPI communicator? More... | |
virtual void | addFilePrefix (const std::string &prefix) |
Prepend argument prefix to existing filePrefix. More... | |
const std::string & | filePrefix () const |
Return file prefix by const reference. More... | |
Additional Inherited Members | |
![]() | |
std::string | filePrefix_ |
Prefix added to file names. More... | |
typedef void(UnitTestClass::* UnitTestRunner< UnitTestClass >::MethodPtr) () |
Pointer to a test method of the associated UnitTest class.
Definition at line 121 of file UnitTestRunner.h.
UnitTestRunner< UnitTestClass >::UnitTestRunner | ( | ) |
Constructor.
Definition at line 172 of file UnitTestRunner.h.
References TestRunner::isIoProcessor().
UnitTestRunner< UnitTestClass >::~UnitTestRunner | ( | ) |
Destructor.
Definition at line 189 of file UnitTestRunner.h.
void UnitTestRunner< UnitTestClass >::addTestMethod | ( | MethodPtr | methodPtr | ) |
Register a test method of the associated unit test class.
Definition at line 196 of file UnitTestRunner.h.
int UnitTestRunner< UnitTestClass >::nTestMethod | ( | ) |
Return the number of registered test methods.
Definition at line 203 of file UnitTestRunner.h.
void UnitTestRunner< UnitTestClass >::method | ( | unsigned int | i | ) |
Run test method number i.
i | index of test method |
Definition at line 212 of file UnitTestRunner.h.
References TestRunner::filePrefix(), TestException::message(), TestRunner::recordFailure(), and TestRunner::recordSuccess().
Referenced by UnitTestRunner< UnitTestClass >::run().
|
virtual |
Run all registered test methods in the order added.
Implements TestRunner.
Definition at line 306 of file UnitTestRunner.h.
References UnitTestRunner< UnitTestClass >::method(), TestRunner::nFailure(), and TestRunner::report().