We'll stick with the chopsticks, thanks...

I recently started a C++ project at work. I hadn't done any serious C++ programming since 1998, so this was a great chance to shake off the rust.

The project had been underway for some weeks when I joined. I was pleased to see a number of CppUnit tests had already been written for most of the non-GUI functionality. A number of things about the tests surprised me, though:

  • A suite() method explicitly on each test class, including an AllTests class whose suite() explicitly aggregates every other test class's suite()
  • Explicitly defined TestClassCtor( std::string name ) and ~TestClassCtor() on the test classes
  • The assertions took the form of Asserter::failIf( x != y, "x is not equal to y" )

After perusing the CppUnit cookbook for a few minutes, I found the macros CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(), CPPUNIT_TEST_SUITE_END(), CPPUNIT_TEST_SUITE_REGISTRATION(), and all the assert macros which help you out by capturing the line number of the assertion that popped, especially CPPUNIT_ASSERT_EQUAL( expected, actual ), which will not only tell you that x != y, but what x and y really were. The test suite creation and registration macros eliminate the need for AllTests and suite() methods by priming the test classes to be created and aggregated by CppUnit::TestRegistryFactory::getRegistry().makeTest(). Nice, eh? Way less boilerplate in the test classes, and nicer, more helpful assertions. Granted, to use the CPPUNIT_ASSERT_EQUAL stuff you may end up writing assertion traits for some of your test subjects...but those are pretty simple, and livable.

After introducing these items, the team seemed to get on board with using CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(), and CPPUNIT_TEST_SUITE_END(), but not the test suite registration/eliminating AllTests and not the handier assertion macros.

I guess sometimes even newly-formed habits die hard.

I'm reminded of a Seinfeld episode in which Jerry ponders chopsticks:

...Because, if you think about it, you know, they've seen the fork... by now. I'm sure they've seen the spoon, they're going, "Yeah, yeah, they're OK... We're going to stay with the sticks." I mean, I don't know how they've missed it: thousands of years ago, Chinese farmer gets up, has his breakfast with the chopsticks, goes out and works all day in the field with a shovel... Hello?... Shovel! Not going out there ploughing forty acres with a couple of pool cues.

Written on August 9, 2006