Google Mock

Google Mock, used in many of the book’s examples, is a mocking and matcher framework that includes the unit testing tool Google Test. I refer to the two terms interchangeably throughout the book but most of the time refer to Google Mock to keep things simple. You may need to peruse Google’s documentation for Google Test in order to understand features that I refer to as belonging to Google Mock.

You will be linking Google Mock into your examples, which means you must first build the Google Mock library. The following instructions may help you get started. You might also choose to refer to the README.txt file supplied with Google Mock for more detailed installation instructions: https://code.google.com/p/googlemock/source/browse/trunk/README.

Installing Google Mock

The official Google Mock site is https://code.google.com/p/googlemock/. You can find downloads at https://code.google.com/p/googlemock/downloads/list. The version used for building the examples is Google Mock 1.6.0.

Unzip the installation zip file (for example, gmock-1.6.0.zip), perhaps in your home directory.

Create an environment variable called GMOCK_HOME that refers to this directory. Here’s an example:

 
export GMOCK_HOME=/home/jeff/gmock-1.6.0

Here it is on Windows:

 
setx GMOCK_HOME c:Usersjlangrgmock-1.6.0

Unix

For Unix, if you want to skip the README build instructions, you might also have success by following the steps I took. I chose to build Google Mock using CMake. From the root of your Google Mock installation ($GMOCK_HOME, henceforth), do the following:

 
mkdir mybuild
 
cd mybuild
 
cmake ..
 
make

The build directory name mybuild is arbitrary. However, the build scripts used by the examples in the book assume this name. If you change it, you’ll need to alter all of the CMakeLists.txt files.

You will also need to build Google Test, which is nested within Google Mock.

 
cd $GMOCK_HOME/gtest
 
mkdir mybuild
 
cd mybuild
 
cmake ..
 
make

Windows

Within the Google Mock distribution, you’ll find the file .msvc2010gmock.sln, which should work for Visual Studio 2010 and newer versions. (You’ll also find .msvc2005.gmock.sln, which should presumably work for Visual Studio 2005 and 2008.)

To compile Google Mock from Visual Studio 2010 and Visual Studio 2012, you will need to configure the projects to use the November 2012 CTP. From the project properties, navigate to Configuration PropertiesGeneralPlatform Toolset and select the CTP.

The CTP does not have support for variadic templates (Visual Studio 2013 might). They are instead artificially simulated.[3] You will need to add a preprocessor definition to bump up _VARIADIC_MAX above its default of 5. A value of 10 should work fine.

When creating projects that use Google Mock, you’ll need to point them to the proper location for include and library files. Under Configuration PropertiesVC++ Directories, do the following:

  • Add $(GMOCK_HOME)msvc2010Debug to Library Directories.

  • Add $(GMOCK_HOME)include to Include Directories.

  • Add $(GMOCK_HOME)gtestinclude to Include Directories.

Under LinkerInput, add gmock.lib to Additional Dependencies.

You’ll want to ensure that both Google Mock and your project are built with the same memory model. By default, Google Mock builds using /MTd.

Creating a Main for Running Google Mock Tests

Code for each of the examples in this book includes a main.cpp file designed for use with Google Mock.

c2/1/main.cpp
 
#include "gmock/gmock.h"
 
 
int​ main(​int​ argc, ​char​** argv) {
 
testing::InitGoogleMock(&argc, argv);
 
return​ RUN_ALL_TESTS();
 
}

The main function shown here first initializes Google Mock, passing along any command-line arguments. It then runs all of the tests.

Most of the time, this is all you need in main. Google Mock also provides a default main implementation that you can link to. Refer to http://code.google.com/p/googletest/wiki/Primer#Writing_the_main()_Function for further information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169