PSCF v1.1
Public Member Functions | Protected Attributes | List of all members
TestRunner Class Referenceabstract

Abstract base class for classes that run tests. More...

#include <TestRunner.h>

Inheritance diagram for TestRunner:
CompositeTestRunner UnitTestRunner< UnitTestClass >

Public Member Functions

 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...
 

Protected Attributes

std::string filePrefix_
 Prefix added to file names. More...
 

Detailed Description

Abstract base class for classes that run tests.

TestRunner is an abstract base class with two types of subclass: The UnitTestRunner class template defines a TestRunner that runs the tests for an associated UnitTest. A CompositeTestRunner runs the tests for a sequence of other TestRunner objects, each of which can be a UnitTestRunner or another CompositeTestRunner.

An implementation of the pure virtual run() method of must run all of the associated test methods, and records the number nSuccess() of tests that succeed and the number nFailure() that fail. A test fails if it throws a TestException. Test methods use the TEST_ASSERT(expr) macro to assert the truth of a logical exprression expr, which throws a TestException if expr is false. The implementation of run() for a UnitTestRunner runs each unit test method of the associated UnitTest in a try-catch block and catches any thrown TestExceptions. The implementation of run for a TestComposite calls the run() method for each of its children.

Each TestRunner may optionally have a parent TestRunner. The parent if any, is always a TestComposite. A TestComposite can have any number of children.

The recordFailure() and recordSuccess() methods of a TestRunner, which can be called by the run method, increment the nSuccess or nFailure counters. If the TestRunner has a parent, each function also calls the corresponding function of the parent, thus incrementing the corresponding counter of the parent. The nSuccess and nFailure counters for a TestComposite thereby keep track of the total number of successful and failed unit test methods run by all descendants.

Each TestRunner has a filePrefix string. The filePrefix is initialized to an empty string, and may be modified by the virtual addFilePrefix() function. The default implementation of this function prepends a string argument to the existing filePrefix.

The UnitTestRunner class template supplies the filePrefix to instances of the associated UnitTest class when a UnitTest is created. (See the notes for UnitTestRunner for details of how). The filePrefix string of the UnitTest is then prepended to the names of any files opened by the functions openInputFile(), openOutputFile(), and openFile of the UnitTest subclass.

The implementation of addFilePrefix() by the TestComposite subclass calls the addFilePrefix method of each of its children, and thus allows a common prefix to be added to the file paths used by all of its children.

Definition at line 73 of file TestRunner.h.

Constructor & Destructor Documentation

◆ TestRunner()

TestRunner::TestRunner ( )

Constructor.

Definition at line 252 of file TestRunner.h.

◆ ~TestRunner()

TestRunner::~TestRunner ( )
virtual

Destructor.

Definition at line 278 of file TestRunner.h.

Member Function Documentation

◆ run()

virtual int TestRunner::run ( )
pure virtual

Run all tests.

Returns
number of failures.

Implemented in CompositeTestRunner, and UnitTestRunner< UnitTestClass >.

◆ recordFailure()

void TestRunner::recordFailure ( )

Increment counter for failed tests, and that of parent (if any).

Definition at line 284 of file TestRunner.h.

References hasParent(), isIoProcessor(), parent(), and recordFailure().

Referenced by recordFailure().

◆ recordSuccess()

void TestRunner::recordSuccess ( )

Increment counter for successful tests, and that of parent (if any).

Definition at line 297 of file TestRunner.h.

References hasParent(), isIoProcessor(), parent(), and recordSuccess().

Referenced by recordSuccess().

◆ setParent()

void TestRunner::setParent ( TestRunner parent)
inline

Set another TestRunner as the parent.

Parameters
parentparent CompositeTestRunner object

Definition at line 199 of file TestRunner.h.

References parent().

Referenced by CompositeTestRunner::addChild().

◆ parent()

TestRunner & TestRunner::parent ( )
inline

Return the parent object, if any.

Definition at line 205 of file TestRunner.h.

Referenced by pscfpp.make.MakeMaker::__init__(), recordFailure(), recordSuccess(), and setParent().

◆ hasParent()

bool TestRunner::hasParent ( ) const
inline

Does this object have a parent?

Definition at line 211 of file TestRunner.h.

Referenced by recordFailure(), recordSuccess(), and report().

◆ nSuccess()

int TestRunner::nSuccess ( ) const
inline

Return number of successful tests run.

Definition at line 217 of file TestRunner.h.

◆ nFailure()

int TestRunner::nFailure ( ) const
inline

Return number of failed tests run.

Definition at line 223 of file TestRunner.h.

Referenced by CompositeTestRunner::run().

◆ report()

void TestRunner::report ( ) const

If this object has no parent, report success and failure counters.

Definition at line 310 of file TestRunner.h.

References hasParent(), and isIoProcessor().

Referenced by CompositeTestRunner::run().

◆ isIoProcessor()

bool TestRunner::isIoProcessor ( ) const
inline

Is this the IO processor of an MPI communicator?

Definition at line 236 of file TestRunner.h.

Referenced by recordFailure(), recordSuccess(), and report().

◆ addFilePrefix()

void TestRunner::addFilePrefix ( const std::string &  prefix)
virtual

Prepend argument prefix to existing filePrefix.

Reimplemented in CompositeTestRunner.

Definition at line 323 of file TestRunner.h.

References filePrefix_.

Referenced by CompositeTestRunner::addChild(), and CompositeTestRunner::addFilePrefix().

◆ filePrefix()

const std::string & TestRunner::filePrefix ( ) const
inline

Return file prefix by const reference.

Definition at line 230 of file TestRunner.h.

References filePrefix_.

Member Data Documentation

◆ filePrefix_

std::string TestRunner::filePrefix_
protected

Prefix added to file names.

Definition at line 167 of file TestRunner.h.

Referenced by addFilePrefix(), and filePrefix().


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