Mocking about

Lately I’ve been fascinated by the opportunities a mock framework gives me to speed up unit test development (see wikipedia for a more detailed explanation).

So far, I’ve tried to write my own using the C++ preprocessor and I almost succeeded. The following works just fine for simple virtual members:

/** Define a mock function with 1 parameter */
#define MOCK1(returntype, name, type1, parm1, retval)\
private:\
      DefaultZeroInt _##name##_callcount;\
      type1 _##name##_##parm1;\
public:\
      /* Function definition*/\
      virtual returntype name(type1 parm1)\
      {\
         ++_##name##_callcount;\
         _##name##_##parm1 = parm1;\
         return retval;\
      }\
      \
      /* Mock utility functions*/\
      void name##_reset()\
      {\
         _##name##_callcount = 0;\
         _##name##_##parm1 = type1();\
      }\
      bool name##_called_once(type1 parm1)\
      {\
         return ((_##name##_callcount == 1) && (_##name##_##parm1 == parm1));\
      }\
      bool name##_last_params_match(type1 parm1)\
      {\
         return ((_##name##_callcount > 0) && (_##name##_##parm1 == parm1));\
      }\
      int name##_call_count()\
      {\
         return _##name##_callcount;\
      }

class DefaultZeroInt
{
public:
   DefaultZeroInt() : val(0) {}
   operator int() { return val; }

   DefaultZeroInt& operator= (const int f)
   {
      val = f;
      return *this;
   }

   DefaultZeroInt& operator++ (void)
    {
       ++val;
       return *this;
    }

private:
   int val;
};

And of course there are variations for multiple parameters (just expanded versions of this 1-argument example). This works fine for arguments like string, int, etc. But not for pointer parameters or reference parameters!

So after some fun time trying to make this work, a little voice in my head started telling me to look around for an existing library. I don’t consider it wasted time to have tried myself, it will help me evaluate other libs better :)

As far as I’m concerned, there are two serious candiates:

  1. Google mock objects (new BSD license) http://code.google.com/p/googlemock/
  2. Hippomocks (LGPL) http://www.assembla.com/spaces/hippomocks

Both are actively maintained. My evaluation:

  • Gmock requires linking to their library code, Hippomocks is header-only and therefore very easy to include.
  • Gmock includes gtest lib and that defines ‘TEST’. This rules out a combination with UnitTest++ because of symbol conflicts. The trick is to include gmock first, then #undef TEST and then include UnitTest++. Not nice!
  • Gmock by default doesn’t require the expectations to be met in the order they are specified. It’s possible but requires extra configuration. Hippomocks does do this by default which is nicer in my opinion.
  • Gmock uses macros to define mock method implementations, Eclipse can expand and autocomplete them just fine. Hippomocks is a bit trickier so no autocomplete there.
  • Gmock is tightly coupled with gtest, some pre-main() handholding is necessary to make it work with my current unit test framework of choice (UnitTest++). It’s documented quite well so this is not a big problem./
  • GMock documentation is extensive and includes a big tutorial and a big cookbook for almost any scenario you might want to implement. Hippomocks documentation also includes a very nice, short and to-the-point tutorial. I was up and running much quicker with Hippomocks.
  • Specification of mock method behaviour is more versatile in Gmock but also a little bit more complex than Hippomocks. Somehow the Hippomocks way feels a little bit more natural.

Conclusion: I wil try to use Hippomocks, it seems like an excellent fit for my usage scenario’s at the moment. If I need something more advanced, a migration to Gmock could be an option