PSCF v1.1
Public Types | Public Member Functions | List of all members
UnitTestRunner< UnitTestClass > Class Template Reference

Template for a TestRunner that runs test methods of an associated UnitTest. More...

#include <UnitTestRunner.h>

Inheritance diagram for UnitTestRunner< UnitTestClass >:
TestRunner

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...
 
int nFailure () const
 Return number of failed tests run. More...
 
bool isIoProcessor () const
 Is this the IO processor of an MPI communicator? More...
 
- Public Member Functions inherited from TestRunner
 TestRunner ()
 Constructor. More...
 
virtual ~TestRunner ()
 Destructor. More...
 
virtual int run ()=0
 Run all tests. 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...
 
TestRunnerparent ()
 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

- Protected Attributes inherited from TestRunner
std::string filePrefix_
 Prefix added to file names. More...
 

Detailed Description

template<class UnitTestClass>
class UnitTestRunner< UnitTestClass >

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:

// Define a UnitTest class
class MyTest : public UnitTest {
public:
test1()
{ .... }
test2
{ ... }
};
// Define a UnitTestRunner associated with MyTest
class MyTest_Runner : public UnitTestRunner<MyTest> {
public:
MyTest_Runner(){
addTestMethod(&MyTest::test1);
addTestMethod(&MyTest::test2);
}
}
// Run the tests.
MyTest_Runner runner;
runner.run();
Template for a TestRunner that runs test methods of an associated UnitTest.
UnitTest is a base class for classes that define unit tests.
Definition: UnitTest.h:51

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:

TEST_BEGIN(MyTest)
TEST_ADD(MyTest, test1)
TEST_ADD(MyTest, test2)
TEST_END(MyTest)
TEST_RUNNER(MyTest) runner;
runner.run();

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.

Member Typedef Documentation

◆ MethodPtr

template<class UnitTestClass >
typedef void(UnitTestClass::* UnitTestRunner< UnitTestClass >::MethodPtr) ()

Pointer to a test method of the associated UnitTest class.

Definition at line 121 of file UnitTestRunner.h.

Constructor & Destructor Documentation

◆ UnitTestRunner()

template<class UnitTestClass >
UnitTestRunner< UnitTestClass >::UnitTestRunner

Constructor.

Definition at line 172 of file UnitTestRunner.h.

References UnitTestRunner< UnitTestClass >::isIoProcessor().

◆ ~UnitTestRunner()

template<class UnitTestClass >
UnitTestRunner< UnitTestClass >::~UnitTestRunner

Destructor.

Definition at line 189 of file UnitTestRunner.h.

Member Function Documentation

◆ addTestMethod()

template<class UnitTestClass >
void UnitTestRunner< UnitTestClass >::addTestMethod ( MethodPtr  methodPtr)

Register a test method of the associated unit test class.

Definition at line 196 of file UnitTestRunner.h.

◆ nTestMethod()

template<class UnitTestClass >
int UnitTestRunner< UnitTestClass >::nTestMethod

Return the number of registered test methods.

Definition at line 203 of file UnitTestRunner.h.

◆ method()

template<class UnitTestClass >
void UnitTestRunner< UnitTestClass >::method ( unsigned int  i)

Run test method number i.

Parameters
iindex of test method

Definition at line 212 of file UnitTestRunner.h.

References TestException::message().

◆ run()

template<class UnitTestClass >
int UnitTestRunner< UnitTestClass >::run
virtual

Run all registered test methods in the order added.

Implements TestRunner.

Definition at line 306 of file UnitTestRunner.h.

◆ nFailure()

template<class UnitTestClass >
int TestRunner::nFailure ( ) const
inline

Return number of failed tests run.

Definition at line 130 of file TestRunner.h.

◆ isIoProcessor()

template<class UnitTestClass >
bool TestRunner::isIoProcessor ( ) const
inline

Is this the IO processor of an MPI communicator?

Definition at line 140 of file TestRunner.h.

Referenced by UnitTestRunner< UnitTestClass >::UnitTestRunner().


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