Cppunit Framework
Cppunit Framework
Cppunit Framework
Unit Testing is a type of software testing where individual units or components of a software are tested.
The purpose is to validate that each unit of the software code performs as expected.
Unit Testing is done during the development (coding phase) of an application by the developers.
Each test runs in its own fixture so there can be no side effects among test runs. Here is an
example:
For each test implement a method which interacts with the fixture. Verify the expected
results with assertions specified by calling CPPUNIT_ASSERT on the expression you want to
test:
public:
void testAdd () {
int result = m_value1 + m_value2;
CPPUNIT_ASSERT( result == 5 );
}
Once the methods are defined you can run them. To do this, use a TestCaller.
public:
static CppUnit::TestSuite *MathTest::suite () {
CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite;
suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
"testAdd", &MathTest::testAdd));
suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
"testDivideByZero", &MathTest::testDivideByZero));
return suiteOfTests;
}
Making assertions
Macros
#defi
ne CPPUNIT_ASSERT(condition)
Assertions that a condition is true.
#defi
ne CPPUNIT_ASSERT_MESSAGE(message, condition)
Assertion with a user specified message.
#defi
ne CPPUNIT_FAIL(message)
Fails with the specified message.
#defi
ne CPPUNIT_ASSERT_EQUAL(expected, actual)
Asserts that two values are equals.
#defi
ne CPPUNIT_ASSERT_EQUAL_MESSAGE (message, expected, actual)
Asserts that two values are equals, provides additional message on failure.
#defi
ne CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
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.
#defi CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,
ne expected, actual, delta)
Macro for primitive double value comparisons, setting a user-supplied message
in case of failure.
#defi
ne CPPUNIT_ASSERT_THROW(expression, ExceptionType)
Asserts that the given expression throws an exception of the specified type.
#defi
ne CPPUNIT_ASSERT_NO_THROW(expression)
Asserts that the given expression does not throw any exceptions.
#defi
ne 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.
#defi CPPUNIT_ASSERT_ASSERTION_FAIL(assertion) CPPUNIT_ASS
ne ERT_THROW( assertion, CPPUNIT_NS::Exception )
Asserts that an assertion fail.
#defi CPPUNIT_ASSERT_ASSERTION_PASS(assertion) CPPUNIT_AS
ne SERT_NO_THROW( assertion )
Asserts that an assertion pass.
#defin
e CPPUNIT_TEST_SUB_SUITE(ATestFixtureType, ASuperClass)
Begin test suite (includes parent suite)
#defin
e CPPUNIT_TEST_SUITE_END()
End declaration of the test suite.
#defin
e CPPUNIT_TEST_SUITE_SETUP()
Setup method that is executed before all tests.
#defin
e CPPUNIT_TEST_SUITE_TEARDOWN()
Tear down method that is executed after all tests.
#defin
e CPPUNIT_TEST_SUITE_END_ABSTRACT()
End declaration of an abstract test suite.
#defin
e CPPUNIT_TEST_SUITE_ADD_TEST(test) context.addTest( test )
Add a test to the suite (for custom test macro).
#defin
e CPPUNIT_TEST(testMethod)
Add a method to the suite.
#defin
e CPPUNIT_TEST_EXCEPTION(testMethod, ExceptionType)
Add a test which fail if the specified exception is not caught.
#defin CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS(testAdderMethod) testAdde
e rMethod( context )
Adds some custom test cases.
#defin
e CPPUNIT_TEST_SUITE_PROPERTY(APropertyKey, APropertyValue)
Adds a property to the test suite builder context.
#defin
e CPPUNIT_TEST_SUITE_REGISTRATION(ATestFixtureType)
#defin CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ATestFixtureType,
e suiteName)
Adds the specified fixture suite to the specified registry suite.
#defin
e CPPUNIT_REGISTRY_ADD(which, to)
#defin
e CPPUNIT_REGISTRY_ADD_TO_DEFAULT(which)
Detailed Description
The macros CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(),
and CPPUNIT_TEST_SUITE_END() are designed to facilitate easy creation of a test suite.
For example,
#include <cppunit/extensions/HelperMacros.h>
class MyTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( MyTest );
CPPUNIT_TEST( testEquality );
CPPUNIT_TEST( testSetName );
CPPUNIT_TEST_SUITE_END();
public:
void testEquality();
void testSetName();
};
The effect of these macros is to define two methods in the class MyTest. The first method is
an auxiliary function named registerTests that you will not need to call directly. The second
function
CPPUNIT_TEST_SUITE_REGISTRATION( MyTest );
CppUnit::Test* tp =
CppUnit::TestFactoryRegistry::getRegistry().makeTest();
The test suite macros can even be used with templated test classes. For example:
template<typename CharType>
class StringTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( StringTest );
CPPUNIT_TEST( testAppend );
CPPUNIT_TEST_SUITE_END();
public:
...
};
CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<char> );
CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<wchar_t> );