CppUnit project page FAQ

Macros
Making assertions

Macros

#define CPPUNIT_ASSERT(condition)
 Assertions that a condition is true. More...
 
#define CPPUNIT_ASSERT_MESSAGE(message, condition)
 Assertion with a user specified message. More...
 
#define CPPUNIT_FAIL(message)
 Fails with the specified message. More...
 
#define CPPUNIT_ASSERT_EQUAL(expected, actual)
 Asserts that two values are equals. More...
 
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)
 Asserts that two values are equals, provides additional message on failure. More...
 
#define CPPUNIT_ASSERT_LESS(expected, actual)
 Asserts that actual is less than expected, provides additional message on failure. More...
 
#define CPPUNIT_ASSERT_GREATER(expected, actual)
 Asserts that actual is greater than expected, provides additional message on failure. More...
 
#define CPPUNIT_ASSERT_LESSEQUAL(expected, actual)
 Asserts that actual is less or equal than expected, provides additional message on failure. More...
 
#define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual)
 Asserts that actual is greater than expected, provides additional message on failure. More...
 
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
 Macro for primitive double value comparisons. More...
 
#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message, expected, actual, delta)
 Macro for primitive double value comparisons, setting a user-supplied message in case of failure. More...
 
#define CPPUNIT_ASSERT_THROW(expression, ExceptionType)
 Asserts that the given expression throws an exception of the specified type. More...
 
#define CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, ExceptionType)
 Asserts that the given expression throws an exception of the specified type, setting a user supplied message in case of failure. More...
 
#define CPPUNIT_ASSERT_NO_THROW(expression)
 Asserts that the given expression does not throw any exceptions. More...
 
#define CPPUNIT_ASSERT_NO_THROW_MESSAGE(message, expression)
 Asserts that the given expression does not throw any exceptions, setting a user supplied message in case of failure. More...
 
#define CPPUNIT_ASSERT_ASSERTION_FAIL(assertion)    CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
 Asserts that an assertion fail. More...
 
#define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(message, assertion)    CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
 Asserts that an assertion fail, with a user-supplied message in case of error. More...
 
#define CPPUNIT_ASSERT_ASSERTION_PASS(assertion)    CPPUNIT_ASSERT_NO_THROW( assertion )
 Asserts that an assertion pass. More...
 
#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(message, assertion)    CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
 Asserts that an assertion pass, with a user-supplied message in case of failure. More...
 

Detailed Description

Macro Definition Documentation

◆ CPPUNIT_ASSERT

#define CPPUNIT_ASSERT (   condition)
Value:
( CPPUNIT_NS::Asserter::failIf( !(condition), \
CPPUNIT_NS::Message( "assertion failed", \
"Expression: " #condition), \
#define CPPUNIT_SOURCELINE()
Constructs a SourceLine object initialized with the location where the macro is expanded....
Definition: SourceLine.h:13

Assertions that a condition is true.

◆ CPPUNIT_ASSERT_ASSERTION_FAIL

#define CPPUNIT_ASSERT_ASSERTION_FAIL (   assertion)     CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )

Asserts that an assertion fail.

Use to test assertions. Example of usage:

#define CPPUNIT_ASSERT(condition)
Assertions that a condition is true.
Definition: TestAssert.h:273
#define CPPUNIT_ASSERT_ASSERTION_FAIL(assertion)
Asserts that an assertion fail.
Definition: TestAssert.h:615

◆ CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE

#define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE (   message,
  assertion 
)     CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )

Asserts that an assertion fail, with a user-supplied message in case of error.

Use to test assertions. Example of usage:

#define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(message, assertion)
Asserts that an assertion fail, with a user-supplied message in case of error.
Definition: TestAssert.h:628

◆ CPPUNIT_ASSERT_ASSERTION_PASS

#define CPPUNIT_ASSERT_ASSERTION_PASS (   assertion)     CPPUNIT_ASSERT_NO_THROW( assertion )

Asserts that an assertion pass.

Use to test assertions. Example of usage:

#define CPPUNIT_ASSERT_ASSERTION_PASS(assertion)
Asserts that an assertion pass.
Definition: TestAssert.h:640

◆ CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE

#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE (   message,
  assertion 
)     CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )

Asserts that an assertion pass, with a user-supplied message in case of failure.

Use to test assertions. Example of usage:

#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(message, assertion)
Asserts that an assertion pass, with a user-supplied message in case of failure.
Definition: TestAssert.h:653

◆ CPPUNIT_ASSERT_DOUBLES_EQUAL

#define CPPUNIT_ASSERT_DOUBLES_EQUAL (   expected,
  actual,
  delta 
)
Value:
(actual), \
(delta), \
"" ) )
void CPPUNIT_API assertDoubleEquals(double expected, double actual, double delta, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two double are equals given a tolerance. Use CPPUNIT_ASSERT_DOUBLES_EQU...
Definition: TestAssert.cpp:8

Macro for primitive double value comparisons.

The assertion pass if both expected and actual are finite and fabs( expected - actual ) <= delta. If either expected or actual are infinite (+/- inf), the assertion pass if expected == actual. If either expected or actual is a NaN (not a number), then the assertion fails.

◆ CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE

#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE (   message,
  expected,
  actual,
  delta 
)
Value:
(actual), \
(delta), \
std::string message_to_string(const std::string &s)
Message traits used by CPPUNIT_ASSERT* macros.
Definition: TestAssert.h:135

Macro for primitive double value comparisons, setting a user-supplied message in case of failure.

See also
CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion.

◆ CPPUNIT_ASSERT_EQUAL

#define CPPUNIT_ASSERT_EQUAL (   expected,
  actual 
)
Value:
( CPPUNIT_NS::assertEquals( (expected), \
(actual), \
"" ) )
void assertEquals(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_EQUAL inste...
Definition: TestAssert.h:161

Asserts that two values are equals.

Equality and string representation can be defined with an appropriate CppUnit::assertion_traits class.

A diagnostic is printed if actual and expected values disagree.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator ==.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

◆ CPPUNIT_ASSERT_EQUAL_MESSAGE

#define CPPUNIT_ASSERT_EQUAL_MESSAGE (   message,
  expected,
  actual 
)
Value:
( CPPUNIT_NS::assertEquals( (expected), \
(actual), \

Asserts that two values are equals, provides additional message on failure.

Equality and string representation can be defined with an appropriate assertion_traits class.

A diagnostic is printed if actual and expected values disagree. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator ==.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

◆ CPPUNIT_ASSERT_GREATER

#define CPPUNIT_ASSERT_GREATER (   expected,
  actual 
)
Value:
( CPPUNIT_NS::assertGreater( (expected), \
(actual), \
"" ) )
void assertGreater(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition: TestAssert.h:213

Asserts that actual is greater than expected, provides additional message on failure.

String representation can be defined with an appropriate assertion_traits class. For comparison assertLess is used.

A diagnostic is printed if actual is less than expected. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator<.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

See also
CPPUNIT_ASSERT_LESS

◆ CPPUNIT_ASSERT_GREATEREQUAL

#define CPPUNIT_ASSERT_GREATEREQUAL (   expected,
  actual 
)
Value:
(actual), \
"" ) )
void assertGreaterEqual(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_LESSEQUAL,...
Definition: TestAssert.h:251

Asserts that actual is greater than expected, provides additional message on failure.

String representation can be defined with an appropriate assertion_traits class. For comparison assertLess is used.

A diagnostic is printed if actual is less than expected. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator<=.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

See also
CPPUNIT_ASSERT_LESSEQUAL

◆ CPPUNIT_ASSERT_LESS

#define CPPUNIT_ASSERT_LESS (   expected,
  actual 
)
Value:
( CPPUNIT_NS::assertLess( (expected), \
(actual), \
"" ) )
void assertLess(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that an object is less than another one of the same type Use CPPUNIT_ASSERT_...
Definition: TestAssert.h:193

Asserts that actual is less than expected, provides additional message on failure.

Less and string representation can be defined with an appropriate assertion_traits class.

A diagnostic is printed if actual is less than expected. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator <.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

See also
CPPUNIT_ASSERT_GREATER

◆ CPPUNIT_ASSERT_LESSEQUAL

#define CPPUNIT_ASSERT_LESSEQUAL (   expected,
  actual 
)
Value:
( CPPUNIT_NS::assertLessEqual( (expected), \
(actual), \
"" ) )
void assertLessEqual(const T &expected, const T &actual, SourceLine sourceLine, const std::string &message)
(Implementation) Asserts that two objects of the same type are equals. Use CPPUNIT_ASSERT_LESSEQUAL,...
Definition: TestAssert.h:232

Asserts that actual is less or equal than expected, provides additional message on failure.

LessEqual and string representation can be defined with an appropriate assertion_traits class.

A diagnostic is printed if actual is greater than expected. The message is printed in addition to the expected and actual value to provide additional information.

Requirement for expected and actual parameters:

  • They are exactly of the same type
  • They are serializable into a std::strstream using operator <<.
  • They can be compared using operator <=.

The last two requirements (serialization and comparison) can be removed by specializing the CppUnit::assertion_traits.

See also
CPPUNIT_ASSERT_GREATEREQUAL

◆ CPPUNIT_ASSERT_MESSAGE

#define CPPUNIT_ASSERT_MESSAGE (   message,
  condition 
)
Value:
( CPPUNIT_NS::Asserter::failIf( !(condition), \
CPPUNIT_NS::Message( "assertion failed", \
"Expression: " \
#condition, \

Assertion with a user specified message.

Parameters
messageMessage reported in diagnostic if condition evaluates to false.
conditionIf this condition evaluates to false then the test failed.

◆ CPPUNIT_ASSERT_NO_THROW

#define CPPUNIT_ASSERT_NO_THROW (   expression)
Value:
CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
expression )
#define CPPUNIT_ASSERT_NO_THROW_MESSAGE(message, expression)
Asserts that the given expression does not throw any exceptions, setting a user supplied message in c...
Definition: TestAssert.h:585

Asserts that the given expression does not throw any exceptions.

Example of usage:

std::vector<int> v;
v.push_back( 10 );
#define CPPUNIT_ASSERT_NO_THROW(expression)
Asserts that the given expression does not throw any exceptions.
Definition: TestAssert.h:570

◆ CPPUNIT_ASSERT_NO_THROW_MESSAGE

#define CPPUNIT_ASSERT_NO_THROW_MESSAGE (   message,
  expression 
)
Value:
do { \
CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
\
try { \
expression; \
} catch ( const std::exception &e ) { \
cpputMsg_.addDetail( "Caught: " + \
"std::exception or derived" ) ); \
cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
CPPUNIT_NS::Asserter::fail( cpputMsg_, \
} catch ( ... ) { \
cpputMsg_.addDetail( "Caught: unknown." ); \
CPPUNIT_NS::Asserter::fail( cpputMsg_, \
} \
} while ( false )
#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_(exception, no_rtti_message)
Definition: TestAssert.h:516

Asserts that the given expression does not throw any exceptions, setting a user supplied message in case of failure.

Example of usage:

std::vector<int> v;
v.push_back( 10 );
CPPUNIT_ASSERT_NO_THROW( "std::vector<int> v;", v.at( 0 ) );

◆ CPPUNIT_ASSERT_THROW

#define CPPUNIT_ASSERT_THROW (   expression,
  ExceptionType 
)
Value:
CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
expression, \
ExceptionType )
#define CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, ExceptionType)
Asserts that the given expression throws an exception of the specified type, setting a user supplied ...
Definition: TestAssert.h:532

Asserts that the given expression throws an exception of the specified type.

Example of usage:

std::vector<int> v;
CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );
#define CPPUNIT_ASSERT_THROW(expression, ExceptionType)
Asserts that the given expression throws an exception of the specified type.
Definition: TestAssert.h:505

◆ CPPUNIT_ASSERT_THROW_MESSAGE

#define CPPUNIT_ASSERT_THROW_MESSAGE (   message,
  expression,
  ExceptionType 
)
Value:
do { \
bool cpputCorrectExceptionThrown_ = false; \
CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
cpputMsg_.addDetail( CPPUNIT_NS::message_to_string(message) ); \
cpputMsg_.addDetail( "Expected: " \
CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
\
try { \
expression; \
} catch ( const ExceptionType & ) { \
cpputCorrectExceptionThrown_ = true; \
} catch ( const std::exception &e) { \
cpputMsg_.addDetail( "Actual : " + \
"std::exception or derived") ); \
cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
} catch ( ... ) { \
cpputMsg_.addDetail( "Actual : unknown."); \
} \
\
if ( cpputCorrectExceptionThrown_ ) \
{ break; } \
\
CPPUNIT_NS::Asserter::fail( cpputMsg_, \
} while ( false )
#define CPPUNIT_GET_PARAMETER_STRING(parameter)
Definition: TestAssert.h:521

Asserts that the given expression throws an exception of the specified type, setting a user supplied message in case of failure.

Example of usage:

std::vector<int> v;
CPPUNIT_ASSERT_THROW_MESSAGE( "- std::vector<int> v;", v.at( 50 ), std::out_of_range );

◆ CPPUNIT_FAIL

#define CPPUNIT_FAIL (   message)
Value:
( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure", \

Fails with the specified message.

Parameters
messageMessage reported in diagnostic.

Send comments to:
CppUnit Developers