Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cppunit Framework

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

What is Unit Testing?

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.

Unit Tests isolate a section of code and verify its correctness.

A unit may be an individual function, method, procedure, module, or object.


Inheritance diagram for TestFixture:

Wraps a test case with setUp and tearDown methods.

A TestFixture is used to provide a common environment for a set of test cases.

To define a test fixture, do the following:

 implement a subclass of TestCase


 the fixture is defined by instance variables
 initialize the fixture state by overriding setUp (i.e. construct the instance variables of
the fixture)
 clean-up after a test by overriding tearDown.

Each test runs in its own fixture so there can be no side effects among test runs. Here is an
example:

Public Member Functions


virtual  ~TestFixture ()
virtual void  setUp ()
  Set up context before running a test.

virtual void  tearDown ()


  Clean up after the test run.
class MathTest : public CppUnit::TestFixture {
protected:
int m_value1, m_value2;
public:
MathTest() {}
void setUp () {
m_value1 = 2;
m_value2 = 3;
}
}

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.

CppUnit::Test *test = new CppUnit::TestCaller<MathTest>( "testAdd",


&MathTest::testAdd );
test->run();

The tests to be run can be collected into a TestSuite.

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 CPPUNIT_ASSERT_THROW_MESSAGE(message, expression,


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

#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_FAIL_MESSAGE (message,


ne  assertion)   CPPUNIT_ASSERT_THROW_MESSAGE ( message,
assertion, CPPUNIT_NS::Exception )
  Asserts that an assertion fail, with a user-supplied message in case of error.

#defi CPPUNIT_ASSERT_ASSERTION_PASS(assertion)   CPPUNIT_AS
ne  SERT_NO_THROW( assertion )
  Asserts that an assertion pass.

#defi CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE (message,


ne  assertion)   CPPUNIT_ASSERT_NO_THROW_MESSAGE ( message,
assertion )
  Asserts that an assertion pass, with a user-supplied message in case of failure.
Macros
#defin
e  CPPUNIT_TEST_SUITE(ATestFixtureType)
  Begin test suite.

#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_FAIL(testMethod)   CPPUNIT_TEST_EXCEPTION( testMeth


e  od, CPPUNIT_NS::Exception )
  Adds a test case which is excepted to fail.

#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

Macros intended to ease the definition of test suites.

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

static CppUnit::TestSuite *suite()


returns a pointer to the suite of tests defined by the CPPUNIT_TEST() macros.

Rather than invoking suite() directly, the


macro CPPUNIT_TEST_SUITE_REGISTRATION() is used to create a static variable that
automatically registers its test suite in a global registry. The registry yields a Test instance
containing all the registered suites.

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:
...
};

You need to add in an implementation file:

CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<char> );
CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<wchar_t> );

You might also like