Online Book Reader

Home Category

Professional C__ - Marc Gregoire [447]

By Root 1215 0
file:

#include

class ObjectPoolTest : public CppUnit::TestFixture

{

public:

void setUp();

void tearDown();

void testSimple(); // Our first test!

};

Code snippet from ObjectPoolTest\ObjectPoolTest.h

The test definition uses the CPPUNIT_ASSERT macro to perform the actual test. CPPUNIT_ASSERT, like other assert macros you may have used, surrounds an expression that should be true. Chapter 27 explains asserts in more detail. In this case, the test claims that 0 is less than 1, so it surrounds the statement 0 < 1 in a CPPUNIT_ASSERT macro call. This macro is defined in the file:

#include "ObjectPoolTest.h"

#include

void ObjectPoolTest::setUp() { }

void ObjectPoolTest::tearDown() { }

void ObjectPoolTest::testSimple()

{

CPPUNIT_ASSERT(0 < 1);

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

That’s it. Of course, most of your unit tests will do something a bit more interesting than a simple assert. As you will see, the common pattern is to perform some sort of calculation and assert that the result is the value you expected. With cppunit, you don’t even need to worry about exceptions — the framework will catch and report them as necessary.

Building a Suite of Tests

There are a few more steps before the simple test can be run. cppunit runs a group of tests as a suite. A suite tells cppunit which tests to run, as opposed to a fixture, which groups tests together logically. The common pattern is to give your fixture class a static method that builds a suite containing all of its tests. In the updated versions of ObjectPoolTest.h and ObjectPoolTest.cpp, the suite() method is used for this purpose:

#include

#include

#include

class ObjectPoolTest : public CppUnit::TestFixture

{

public:

void setUp();

void tearDown();

void testSimple(); // Our first test!

static CppUnit::Test* suite();

};

Code snippet from ObjectPoolTest\ObjectPoolTest.h

#include "ObjectPoolTest.h"

#include

#include

void ObjectPoolTest::setUp() { }

void ObjectPoolTest::tearDown() { }

void ObjectPoolTest::testSimple()

{

CPPUNIT_ASSERT(0 < 1);

}

CppUnit::Test* ObjectPoolTest::suite()

{

CppUnit::TestSuite* suiteOfTests =

new CppUnit::TestSuite("ObjectPoolTest");

suiteOfTests->addTest(new CppUnit::TestCaller(

"testSimple",

&ObjectPoolTest::testSimple));

return suiteOfTests; // Note that Test is a superclass of TestSuite

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

The template syntax for creating a TestCaller is a bit dense, but just about every single test you write will follow this exact pattern, so you can ignore the implementation of TestSuite and TestCaller for the most part.

To actually run the suite of tests and see the results, you will need a test runner. cppunit is a flexible framework. It contains several different runners that operate in different environments, such as the MFC Runner, which is designed to run within a program written with the Microsoft Foundation Classes. For a text-based environment, you should use the Text Runner, which is defined in the CppUnit::TextUi namespace.

The code to run the suite of tests defined by the ObjectPoolTest fixture follows. It creates a runner, adds the tests returned by the suite() method, and calls run().

#include "ObjectPoolTest.h"

#include

int main()

{

CppUnit::TextUi::TestRunner runner;

runner.addTest(ObjectPoolTest::suite());

runner.run();

return 0;

}

Code snippet from ObjectPoolTest\ObjectPoolTest.cpp

To compile and run these unit tests, you will need to download, build, and install cppunit. Please consult the cppunit documentation on their website for installation instructions specific to your platform. After cppunit is installed, the code can be built. For example, on Linux, this can be done as follows:

> export LD_LIBRARY_PATH=/usr/local/lib

> g++ -I/usr/local/include/cppunit -L/usr/local/lib -lcppunit

Return Main Page Previous Page Next Page

®Online Book Reader