Workspace 6.21.5
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
TestHarness Class Referenceabstract

Basic test harness for running test cases. More...

#include <Workspace/testcommon.h>

Inheritance diagram for TestHarness:
[legend]

Public Member Functions

virtual ~TestHarness ()
 
void fail (const std::string &msg="") const
 
const std::string & getDescription () const
 
const std::string & getName () const
 
void pass () const
 
int run (bool captureOutput=false)
 
void setDescription (const std::string &desc)
 
void setName (const std::string &name)
 
bool shouldSkipRemainingTests () const
 
void skip (const std::string &msg="") const
 
int skipAll (const std::string &msg="")
 

Static Public Member Functions

static bool compareTextFiles (const std::string &file1, const std::string &file2)
 

Protected Member Functions

 TestHarness (const std::string &name, const std::string &description="")
 
 TestHarness (StdCapture &stdCap)
 
bool fuzzyZero (double val, double tol)
 
virtual void runTest ()=0
 

Detailed Description

This class has absolutely no reliance on any other workspace code. It only uses classes and typedef's found in the C++ standard (eg in the STL) and all client test code which uses the class will not need to link in any other workspace library unless the client code calls workspace code itself.

Constructor & Destructor Documentation

◆ TestHarness() [1/2]

TestHarness ( const std::string &  name,
const std::string &  description = "" 
)
inlineprotected
Parameters
nameAn informational name associated with the test. This is usually printed out after the result of the test to help the user identify which test has completed.
descriptionOptional description giving more details about the test. If not empty, this is shown as part of the output for failed tests (it is not shown for tests that pass or that are skipped).
See also
setName(), setDescription()

◆ TestHarness() [2/2]

TestHarness ( StdCapture stdCap)
inlineprotected

◆ ~TestHarness()

virtual ~TestHarness ( )
inlinevirtual

Member Function Documentation

◆ compareTextFiles()

static bool compareTextFiles ( const std::string &  file1,
const std::string &  file2 
)
inlinestatic
Parameters
file1Name of the first file to compare.
file2Name of the second file to compare.
Returns
True if the contents of both files are the same.

The comparison is done by reading in both files into strings. If the strings are different lengths, this is reported. If they are the same length but they still differ, a message is printed stating at which line the first difference was encountered.

◆ fail()

void fail ( const std::string &  msg = "") const
inline
Parameters
msgSome information about why the test failed.

This function generates an exception which will be caught and handled appropriately by the run() function to indicate that the test failed. Unlike the functions for the other exit conditions, this function outputs the text messages now rather than letting the runTest() function handle it. This is required so that output is not lost if throwing an exception will result in a segmentation fault (and therefore an instant termination).

Most clients should prefer to use the failTest() macro, since it calls this function but with the source file and line number embedded within msg. This generally makes it easier to trace the source of the failure.

◆ fuzzyZero()

bool fuzzyZero ( double  val,
double  tol 
)
inlineprotected
Returns
True if the absolute value of val is less than tol.

◆ getDescription()

const std::string & getDescription ( ) const
inline
Returns
The description for this test. This will frequently be an empty string.

◆ getName()

const std::string & getName ( ) const
inline
Returns
The name for this test.
See also
setName(), getDescription()

◆ pass()

void pass ( ) const
inline

◆ run()

int run ( bool  captureOutput = false)
inline
Parameters
captureOutputIf true, the test harness will capture all output sent to std::cerr and std::cout. If the test passes, the captured output will be discarded, but for any other exit conditions, the captured output will be included after the line indicating the exit condition. If captureOutput is false, then all text sent to std::cerr and std::cout will be sent to these streams as per normal.

This used to be default to true but has been changed to false as there seems no reason why you wouldn't want as much information available as possible all the time.

Wrapper around runTest() to catch all exceptions. This function will output one of the following three strings followed by the name of the test:

  • PASSED
  • SKIPPED
  • FAILED

In the case of FAILED, the next line will usually contain some text explaining why the test failed.

Returns
0 if the test passed, 1 if the test was skipped, 2 if the test failed for a reason initiated by the test code, 3 if a standard C++ exception was spontaneously thrown and 4 if some other C++ exception was spontaneously thrown.
See also
runTest

◆ runTest()

virtual void runTest ( )
protectedpure virtual

Subclasses override this to perform the work of the test. If the test completes successfully, then runTest simply needs to return normally. If the test should be skipped, the subclass should call skip(), at which point an exception will be generated which will be caught by the run() function. If the test fails, the subclass should call fail() and again an exception will be generated which will be caught by run().

It is not normally necessary to call runTest directly. Instead, the test should be run by calling the public run() function, since this takes care of printing standard text to specify the outcome of the test and it catches all exceptions so that the outcome is always known.

Implemented in TestSuite::TestCase< TestNum >.

◆ setDescription()

void setDescription ( const std::string &  desc)
inline
Parameters
descThe new description for the test.

This function is rarely used for single tests, but tests that are part of a test suite may call this to provide additional information about that particular test. The description should not be particularly long, generally being no more than a few words and rarely longer than one line of text. It is quite common for the description to be empty.

A useful feature of the description is that for failed tests, it is shown immediately after the FAILED line to provide additional context for the failure.

See also
getDescription()

◆ setName()

void setName ( const std::string &  name)
inline
Parameters
nameThe new name for the test.

This function is rarely used. The test name is almost always set by the constructor.

See also
getName(), setDescription()

◆ shouldSkipRemainingTests()

bool shouldSkipRemainingTests ( ) const
inline
Returns
True if all remaining tests are to be skipped. This is used by the TestSuite class to help determine whether any tests after the current one should be skipped.

◆ skip()

void skip ( const std::string &  msg = "") const
inline
Parameters
msgSome information about why the test was skipped.

Skips this test. It generates an exception which will be caught and handled appropriately by the run() function. This function should only be called from within the runTest() function. If you need to skip the entire test (ie never call run() at all), use the skipAll() function instead.

◆ skipAll()

int skipAll ( const std::string &  msg = "")
inline
Parameters
msgSome information about why the test was skipped.

This function differs from skip() in that it may be called from either inside a test or outside the the run() function. If the test is part of a TestSuite, calling skipAll() within a test's run() function will result in all remaining tests in the test suite being skipped as well (they will not have their run() function called, the skipping is performed by TestSuite::run()).

The skipAll() function outputs an appropriate SKIPPED message for this test and then returns the code the application should exit with.

Returns
Always the code for a SKIPPED test (currently 1). This should be the exit code of the application unless an earlier test failed.