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.
Contents
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
- gmock.lib
- gmock_main.lib
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]
This indicates that (most of) gmock functions correctly.
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:
- #include “gtest/gtest.h”
- TEST(setup_test_case, testWillPass)
- {
- EXPECT_EQ(42, 42);
- }
- TEST(setup_test_case, testWillFail)
- {
- EXPECT_EQ(42, 0);
- }
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
- \gmock-1.6.0\include
- \gmock-1.6.0\gtest\include
- Add gmock libraries (instead of gtest libs)
- Linker -> General -> Addition Library Directories
- \gmock-1.6.0\msvc\2010\Debug
- Linker -> Input -> Additional Dependencies
- gmock.lib
- gmock_main.lib
- Modify Runtime Library:
- C/C++ -> Code Generation -> Runtime Library
- Multi-threaded Debug (/MTd).
Note here that we don’t have to include the gtest libraries as these are embedded in the gmock libraries. Build and run and we will see the familiar gtest output:
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).
Interface Class
- class IWidget
- {
- public:
- virtual void On() = 0;
- virtual void Off() = 0;
- };
Unit Under Test
- class IWidget;
- class WidgetController
- {
- public:
- WidgetController(IWidget& w);
- ~WidgetController(void);
- void exec();
- private:
- IWidget& myWidget;
- };
- #include “WidgetController.h”
- #include “IWidget.h”
- WidgetController::WidgetController(IWidget& w):myWidget(w)
- {
- myWidget.Off();
- }
- WidgetController::~WidgetController()
- {
- myWidget.Off();
- }
- void WidgetController::exec()
- {
- myWidget.On();
- }
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.
- // testFixture.cpp
- #include “gtest/gtest.h”
- #include “gmock/gmock.h”
- #include “IWidget.h”
- class MockWidget : public IWidget
- {
- public:
- MOCK_METHOD0(On, void());
- MOCK_METHOD0(Off, void());
- };
- #include “WidgetController.h”
- TEST(TestWidgetController, testConstructor)
- {
- MockWidget mw;
- EXPECT_CALL(mw, Off()).Times(2);
- WidgetController wc(mw);
- }
Build and run
You can see gmock in action by simply changing the expectation, e.g. [line 6]
- TEST(TestWidgetController, testConstructor)
- {
- MockWidget mw;
- // EXPECT_CALL(mw, Off()).Times(2);
- EXPECT_CALL(mw, Off());
- WidgetController wc(mw);
- }
will result in the following failed test output:
Where next?
Once you have a working project, the documentation on the Googlemock site is excellent. Start with Googlemock for Dummies.
- Navigating Memory in C++: A Guide to Using std::uintptr_t for Address Handling - February 22, 2024
- Embedded Expertise: Beyond Fixed-Size Integers; Exploring Fast and Least Types - January 15, 2024
- Disassembling a Cortex-M raw binary file with Ghidra - December 20, 2022
Co-Founder and Director of Feabhas since 1995.
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.
Oof. I've been thinking about moving (part of) our group to a dual-target, test-as-you-go kinda system rather than our current ad-hoc style.
I'm a little worried about the first couple times we try it because I know setting up the dual-target environment is going to be time consuming, but I'm sick of finding missed corner cases 3 months into hardware integration where they start pushing the schedule out.
Thanks for the link to the Grenning book, the excerpts look like they have some good advice to get me started.
if anyone has a problem building the gmock solution itself, that is can't generate the .lib files, this did it for me:
https://groups.google.com/forum/#!topic/googlemock/FaEAHedxpAQ
there was an outdated GTestDir definition in gmock_config.props, a change from "../../gtest" to "../../../googletest" did it. At least the libs were compiled, i still got the gmock_test error though.