Using GMock with Visual Studio CppUnitTestFramework

One of the things I have been a bit disappointed with myself during the development of [OpenCover]( is the lack of unit testing around the C++ code that makes up...

Shaun Wilde
a computer, phone and tablet on a desk

One of the things I have been a bit disappointed with myself during the development of OpenCover is the lack of unit testing around the C++ code that makes up the profiler.

I did toy with GTest and got some decent tests around the instrumentation engine but I was never able to actually test the profiler callbacks, also I found the lack of GTest integration with Visual Studio quite irritating; I know I have been spoilt by ReSharper. Recently however, during handling Fakes through OpenCover, I had an opportunity to work out how to load the profiler using registry free loading and realised that perhaps such testing might be within my reach, what I was missing however was a mocking library and one that I could use with Visual Studio tooling.

Frankly GMock was the only candidate, the commercial alternatives being out as this was for an OSS project, but the instructions all seemed to want to build a number of libraries (64/32 bit Debug/Release) that I would have to statically link to and maintain these builds should the source or build options change. I decided to try a different tack that wouldn't involve building libraries and it has worked out reasonably successful, so I thought it would be worth commenting on here.

Step 1

Get the latest GMock (1.7.0) library as a zip file and uncompress it somewhere within your repository.

Step 2

From within Visual Studio update the Additional Include Directories to include the following paths


Step 3

Add the following to your "stdafx.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

Step 4

Add the following to your "stdafx.cpp"

// The following lines pull in the real gmock *.cc files.
#include "src/"
#include "src/"
#include "src/"
#include "src/"
#include "src/"

// The following lines pull in the real gtest *.cc files.
#include "src/"
#include "src/"
#include "src/"
#include "src/"
#include "src/"
#include "src/"
#include "src/"

Step 5

Now all you need to do is add initialise GMock and you are ready; as I am using the CppUnitTestFramework I do the following.

// enable google mock
::testing::GTEST_FLAG(throw_on_failure) = true;
int argc = 0;
TCHAR **argv = NULL;
::testing::InitGoogleMock(&argc, argv);

Now all you need to do is follow the GMock documentation and add some expectations etc you can as I discovered even mock COM objects and have expectations on them e.g.


Bonus Round

There were a few little niggles however the first of which is that if an expectation fails, the Visual Studio test runner takes a little too long to close down (I suspect this may be something on my machine related to DrWatson). The second was that if an expectation did fail I could only initially see the result using DebugView - ugh - however I found a solution at which involves using some extra macros; I added these to my "stdafx.h" and voila the results are now available in Visual Studio. Finally, I found the mocks were not very lightweight and in fact if I left them hooked in caused performance issues, replacing them with an admittedly less useful stubs I could avoid this when necessary.

Update 20/2/2016 The link to has stopped working, but a copy of the article can be found on tumblr - For completeness however I am posting the macros here as well with all attribution belonging to

#define _TEST_METHOD_EX_EXPANDER(_testMethod)\
_testMethod { try

// Adds support for seeing std::exception in test output. Requires TEST_METHOD_EX_END after test.
// Example:
// TEST_METHOD_EX_BEGIN(MyFailingTest){ throw std::exception("What happened"); } TEST_METHOD_EX_END;

// Use following test declared with TEST_METHOD_EX_BEGIN
catch (::std::exception& ex) \
{ \
::std::wstringstream ws; ws << "Unhandled Exception:" << ::std::endl << ex.what(); \
} \