Following on from my last post about setting up googletest to use with Visual Studio 2010 express edition, this post builds on that by showing how to build, setup and test the googlemock libraries.
If you have read the previous post, then the basic steps are very similar.
First, download the googlemock zip file and unzip it to known location. As before, I suggest something easy, either C:\gmock-1.6.0, or as in my case C:\src\gmock-1.6.0. One useful fact is that all the gtest code is included as part of the gmock distribution. It can be found at \gmock-1.6.0\gtest.
Building the gtest libraries
It is the same process as build the gtest libraries. One importany note is that
The gmock libraries contain all the gtest code as well.
Navigate to the directory \gmock-1.6.0\msvc\2010 and open the Visual C++ project gmock (gmock.sln). You will end up with three projects.
Go ahead and build these (F7) and ignore any warnings. Once successfully built, look in the directory \gmock-1.6.0\msvc\2010\gtest\Debug and you will find two library files
Test the GMock Library
As part of the standard build two executable are created that allow a quick self-test of googlemock (gtest does have the same but I neglected to mention those in my previous post).
I recommend opening a command window and navigating to directory \gmock-1.6.0\msvc\2010\Debug. There you will fine the file gmock_test.exe; go ahead and execute that [NOTE: I had two (of the 830) tests fail, which I’m not sure why (yet) – to be investigated]
Building a Base GMock Project
A gmock project is the same as a gtest project but with different project properties. Create a newWin32 Console Application project. Add a test fixture file to the project:
and exclude file containing default main from build.
Modify the project properties (Alt+F7) as follows:
- Set gmock and gtest header include directories
- C/C++ -> General -> Additional Include Directories
- Add gmock libraries (instead of gtest libs)
- Linker -> General -> Addition Library Directories
- Linker -> Input -> Additional Dependencies
- Modify Runtime Library:
- C/C++ -> Code Generation -> Runtime Library
- Multi-threaded Debug (/MTd).
To test the gmock setup we need to create two classes:
- The class to become the Unit-Under-Test (UUT)
- An interface class that the UUT calls upon, which doesn’t have any implementation (or the implementation is target/hardware specific).
Unit Under Test
Testing using the Mock framework
To test using the Mock we need to:
- Include the gmock header [line 3]
- Create a mock class derived from the Interface class [lines 7-12]
- Create a test where the UUT calls on the interface on the mock object [lines 16-23]
- Set the mock objects expectation [line 20]. The mock expectation is that the Off member function will be called twice, once during WidgetController construction and once during destruction.
Build and run
You can see gmock in action by simply changing the expectation, e.g. [line 6]
will result in the following failed test output:
Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking.
His current interest lie in IoT Security and Agile for Embedded Systems.
Latest posts by Niall Cooling (see all)
- Using a Raspberry Pi as a remote headless J-Link Server - July 4, 2019
- Peripheral register access using C Struct’s – part 1 - January 24, 2019
- An Introduction to Docker for Embedded Developers – Part 5 Multi-Stage Builds - October 11, 2018