John Lam 1997 — 2026

CppUnit revisited

I spent some time last night and this morning thinking through some test cases that I’m implementing in CppUnit. This is all part of my plan to integrate NAnt with CppUnit tests.

Since CLAW is a COM object, I needed some tests to verify that it was functioning correctly as a COM object. I needed to execute a suite of tests in a predefined order against the same COM object. This gives rise to the concept of stateful vs. stateless test fixtures in CppUnit.

If you follow the excellent CppUnit cookbook, you will wind up with a stateless fixture. Towards the end of the cookbook, the authors encourage you to use a number of macros that are defined in the cppunit\extensions\HelperMacros.h header file. They recommend that you registering a number of tests with a test suite can using:

CPPUNIT_TEST_SUITE( StatelessComTest ); CPPUNIT_TEST( testCreateInstance ); CPPUNIT_TEST( testInitialize ); CPPUNIT_TEST( testShutDown ); CPPUNIT_TEST_SUITE_END();

However, the problem with this approach is that it will construct separate StatelessComTest objects for each one of the three tests being run: testCreateInstance, testInitialize, testShutDown. This effectively prevents you from sharing the same COM object instance across all three tests.

The macros shown above construct a method that resembles:

static Test *suite() { TestSuite *testSuite = new TestSuite(); testSuite->addTest( new TestCaller< StatelessComTest >( "testCreateInstance", testCreateInstance ) ); ... }

Each call to TestSuite::addTest() adds a separate object to the suite. This is not acceptable if you want to invokve a number of methods on the same COM object; you’ll need to keep it hanging around.

To solve this problem, you need to implement an instance method called suite() as opposed to the static method shown above:

Test *suite() { TestSuite *testSuite = new TestSuite( "StatefulComTest" ); testSuite->addTest( new TestCaller< StatefulComTest >( "testCreateInstance", testCreateInstance, *this ) ); testSuite->addTest( new TestCaller< StatefulComTest >( "testInitialize", testInitialize, *this ) ); testSuite->addTest( new TestCaller< StatefulComTest >( "testShutDown", testShutDown, *this ) ) return testSuite; }

In this method, notice that *this is passed as the last parameter to TestCaller’s constructor. This ensures that the same StatefulComTest object is called for all three tests.

The next question that naturally arises is where to construct the instance of the COM object that will be shared. The correct solution is in the constructor of the StatefulComTest object. You might be tempted to use the setUp() and tearDown() methods, but you must remember that these methods are called before and after each test in your test suite, regardless of whether the tests are run on the same object or not!

If you’re interested in looking at some sample code that illustrates these points, click here to view the C++ header file for the tests.

Update: You can’t really use the constructor /destructor to setup and tear down the COM object either since you’d be tempted to throw exceptions from there. Unfortunately, CppUnit (1.8.0 is the build that I’m using) does not catch any thrown exceptions from TestFixture constructors, so they leak.

I’m currently investigating how to work around this. I suspect that each test must be stateful, and maintaining state across tests is just not the way to do things using CppUnit.