CppUnit: Some More Discoveries


Tuesday 12th of August 2008 21:24

One thing I don't think I mentioned in the last post is that in the ClassNameTest.cpp file we need to register the test suite using a macro:

[code]
    CPPUNIT_TEST_SUITE_REGISTRATION( ClassNameTest );
[/code]

which registers the test suite so that it will be run.

The assertions which are possible include (lifted directly from the api documentation at the CppUnit sourceforge page):

CPPUNIT_ASSERT(condition)
Aserts that condition is true.

CPPUNIT_ASSERT_MESSAGE(message, condition)
Asert condition is true, and display message on failure.

CPPUNIT_FAIL(message)
Fails with the message.

CPPUNIT_ASSERT_EQUAL(expected, actual)
Checks that both values are equal.

CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)
As above but fails with the message.

CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
Checks that expected is within delta of actual (delta should actually be epsilon!)

CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message, expected, actual, delta)
As above but fails with message.

CPPUNIT_ASSERT_THROW(expression, ExceptionType)
Asserts that the given expression throws an exception of the type given.

CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, ExceptionType)
As above but with a message on failure.

CPPUNIT_ASSERT_NO_THROW(expression)
This is the opposite of the above the expression does NOT throw an exception.

CPPUNIT_ASSERT_NO_THROW_MESSAGE(message, expression)
If it does throw an exception then we fail with the given message.

CPPUNIT_ASSERT_ASSERTION_FAIL(assertion)
Assert that a CPPUNIT assertion will fail.

CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE(message, assertion)
As above but with a message if the assertion passes.

CPPUNIT_ASSERT_ASSERTION_PASS(assertion)
Assert that a CPPUNIT assertion will pass.

CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(message, assertion)
As above but displays he message of it fails.

I think there are more possible assertions as the CppUnit Cookbook uses a statement in setting up the tests which checks for a given exception type being thrown.

Enough for today.