{ |one, step, back| } 1 of 1 article WikiSyndicate: full/short

The Simplest Unit Test Framework That Could Possibly Work   22 Aug 03
[ print link all ]
I was helping a friend in C++ the other day. While I religiously use a test framework in my day to day work (e.g. JUnit for Java or Test::Unit for Ruby), I haven’t used C++ for over three years and didn’t have C++ Unit handy.

Then I remembered the C/C++ assert macro. We wrote a a series of small functions that looked something like this:

  void TestCanRun () {
      assert(   CanRun( ... ) );
      assert( ! CanRun( ... ) );
      // and so on ...
   }

I made a TestAll() function that called all my little test functions and arranged to have TestAll() called as the first thing in main(). If all the assertions worked, the program completed normally. If an assertion failed, the program would abort with an informative error message.

It worked great. I wouldn’t leave production code like that, but it was a great way to introduce Test Driven Design to someone who had not seen it before, without getting into all the nitty-gritty details of setting up a full blown test suite.

I guess my motto is that friends shouldn’t let friends program without tests.

 

Formatted: 07-Sep-08 06:03
Feedback: jim@weirichhouse.org