Unit Testing

Detailed Description

The Pt::Unit module provides a complete framework for effective unit testing. Simple tests are implemented by deriving from TestCase, multi-method suites by deriving from TestSuite. Setup and teardown of resources is provided by the TestFixture interface. Test conditions are verified through the Assertion class and the PT_UNIT_ASSERT macros. Execution order and data-driven repetition are controlled by a TestProtocol. Tests are auto-registered with RegisterTest and run by the Application class, which reports results through Reporter. A ready-made main() function is available by including TestMain.h.

Classes

class  Application
 Run registered tests. More...
 
class  Assertion
 Test Assertion exception More...
 
struct  RegisterTest< TestT >
 Registers tests to an application. More...
 
class  Reporter
 Test event reporter More...
 
class  Test
 Test base class More...
 
class  TestCase
 Single test with setup and teardown. More...
 
class  TestContext
 Context in which test are run. More...
 
class  TestFixture
 Fixture interface for tests. More...
 
class  TestProtocol
 Protocol for test suites. More...
 
class  TestSuite
 Protocol and data driven testing. More...
 

Macros

#define PT_UNIT_ASSERT(cond)
 Asserts that a condition is true. More...
 
#define PT_UNIT_ASSERT_MSG(cond, what)
 Asserts that a condition is true with a custom message. More...
 
#define PT_UNIT_ASSERT_EQUALS(value1, value2)
 Deprecated. Use PT_UNIT_ASSERT_EQUAL instead.
 
#define PT_UNIT_ASSERT_EQUAL(value1, value2)
 Asserts that two values are equal. More...
 
#define PT_UNIT_ASSERT_NEAR(value1, value2)
 Asserts that two floating-point values are approximately equal. More...
 
#define PT_UNIT_ASSERT_THROW(cond, EX)
 Asserts that an expression throws a specific exception. More...
 
#define PT_UNIT_ASSERT_NOTHROW(cond)
 Asserts that an expression does not throw. More...
 
#define PT_UNIT_FAIL(what)
 Fails unconditionally with a message. More...
 

Macro Definition Documentation

◆ PT_UNIT_ASSERT

#define PT_UNIT_ASSERT (   cond)
Value:
do { \
if( !(cond) ) \
throw Pt::Unit::Assertion(#cond, PT_SOURCEINFO); \
} while (::Pt::Unit::testCond)

Throws Assertion with the stringified condition and source location if cond evaluates to false.

Parameters
condThe condition to verify.

◆ PT_UNIT_ASSERT_MSG

#define PT_UNIT_ASSERT_MSG (   cond,
  what 
)
Value:
do { \
if( !(cond) ) \
{ \
std::ostringstream _pt_msg; \
_pt_msg << what; \
throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
} \
} while (::Pt::Unit::testCond)

Throws Assertion with the given message and source location if cond evaluates to false.

Parameters
condThe condition to verify.
whatA message or stream expression describing the failure.

◆ PT_UNIT_ASSERT_EQUAL

#define PT_UNIT_ASSERT_EQUAL (   value1,
  value2 
)
Value:
do { \
if( ! ((value1) == (value2)) ) \
{ \
std::ostringstream _pt_msg; \
_pt_msg << "not equal: (" #value1 ")=<" << value1 << ">, (" #value2 ")=<" << value2 << '>'; \
throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
} \
} while (::Pt::Unit::testCond)

Throws Assertion if value1 and value2 are not equal. Both values are printed in the failure message.

Parameters
value1The first value.
value2The second value.

◆ PT_UNIT_ASSERT_NEAR

#define PT_UNIT_ASSERT_NEAR (   value1,
  value2 
)
Value:
do { \
const double _pt_v1 = static_cast<double>(value1); \
const double _pt_v2 = static_cast<double>(value2); \
const double _pt_rel_eps = std::sqrt(std::numeric_limits<double>::epsilon()); \
const double _pt_abs_eps = std::numeric_limits<double>::min(); \
const double _pt_scale = std::fmax(std::fabs(_pt_v1), std::fabs(_pt_v2)); \
const double _pt_eps = std::fmax(_pt_rel_eps * _pt_scale, _pt_abs_eps); \
if( std::fabs(_pt_v1 - _pt_v2) > _pt_eps ) \
{ \
std::ostringstream _pt_msg; \
_pt_msg << "not near: (" #value1 ")=<" << _pt_v1 \
<< ">, (" #value2 ")=<" << _pt_v2 \
<< ">, eps=<" << _pt_eps \
<< ">, diff=<" << std::fabs(_pt_v1 - _pt_v2) << '>'; \
throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
} \
} while (::Pt::Unit::testCond)

Throws Assertion if the absolute difference between value1 and value2 exceeds a relative tolerance derived from machine epsilon.

Parameters
value1The first value.
value2The second value.

◆ PT_UNIT_ASSERT_THROW

#define PT_UNIT_ASSERT_THROW (   cond,
  EX 
)
Value:
do { \
struct _pt_ex { }; \
try \
{ \
cond; \
throw _pt_ex(); \
} \
catch(const _pt_ex &) \
{ \
std::ostringstream _pt_msg; \
_pt_msg << "exception of type " #EX " expected in " #cond; \
throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
} \
catch(const EX &) \
{} \
} while (::Pt::Unit::testCond)

Throws Assertion if cond does not throw an exception of type EX.

Parameters
condThe expression to evaluate.
EXThe expected exception type.

◆ PT_UNIT_ASSERT_NOTHROW

#define PT_UNIT_ASSERT_NOTHROW (   cond)
Value:
do { \
try { \
\
cond; \
} \
catch(const std::exception& e) \
{ \
std::string("unexpected exception of type ") + typeid(e).name() + ": " + e.what(), \
PT_SOURCEINFO); \
} \
catch(...) \
{ \
throw Pt::Unit::Assertion("unexpected exception." , PT_SOURCEINFO); \
} \
} while (::Pt::Unit::testCond)

Throws Assertion if cond throws any exception.

Parameters
condThe expression to evaluate.

◆ PT_UNIT_FAIL

#define PT_UNIT_FAIL (   what)
Value:
do { \
std::ostringstream _pt_msg; \
_pt_msg << what; \
throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
} while (::Pt::Unit::testCond)

Throws Assertion with the given message.

Parameters
whatA message or stream expression describing the failure.
Test Assertion exception
Definition: Assertion.h:79