blob: 9498041a99e27fa5ce4b5fc3e3e9df6fc0fc14d7 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001Google C++ Mocking Framework
2============================
3http://code.google.com/p/googlemock/
4
5Overview
6--------
7Google's framework for writing and using C++ mock classes on Linux,
8Mac OS X, and Windows. Inspired by jMock, EasyMock, and Hamcrest, and
9designed with C++'s specifics in mind, it can help you derive better
10designs of your system and write better tests.
11
12Google Mock:
13
14- provides a declarative syntax for defining mocks,
15- can easily define partial (hybrid) mocks, which are a cross of real
16 and mock objects,
17- handles functions of arbitrary types and overloaded functions,
18- comes with a rich set of matchers for validating function arguments,
19- uses an intuitive syntax for controlling the behavior of a mock,
20- does automatic verification of expectations (no record-and-replay
21 needed),
22- allows arbitrary (partial) ordering constraints on
23 function calls to be expressed,
24- lets a user extend it by defining new matchers and actions.
25- does not use exceptions, and
26- is easy to learn and use.
27
28Please see the project page above for more information as well as mailing lists
29for questions, discussions, and development. There is also an IRC channel on
30OFTC (irc.oftc.net) #gtest available. Please join us!
31
32Please note that code under scripts/generator/ is from the cppclean
33project (http://code.google.com/p/cppclean/) and under the Apache
shiqianc50af1a2008-12-11 05:22:15 +000034License, which is different from Google Mock's license.
shiqiane35fdd92008-12-10 05:08:54 +000035
36Requirements
37------------
38Google Mock is not a testing framework itself. Instead, it needs a
39testing framework for writing tests. Currently Google Mock only works
40with Google Test (http://code.google.com/p/googletest/), although
41eventually we plan to support other C++ testing frameworks. You can
42use either the copy of Google Test that comes with Google Mock, or a
shiqian281b1d22008-12-11 00:13:55 +000043compatible version you already have. This version of Google Mock
44requires Google Test 1.2.1.
shiqiane35fdd92008-12-10 05:08:54 +000045
46Google Mock depends on advanced C++ features and thus requires a more
47modern compiler. The following are needed to use Google Mock:
48
49### Linux Requirements ###
50These are the base requirements to build and use Google Mock from a source
51package (as described below):
52 * GNU-compatible Make or "gmake"
53 * POSIX-standard shell
54 * POSIX(-2) Regular Expressions (regex.h)
zhanyong.wan19e49af2009-01-14 21:09:22 +000055 * gcc 4.0 or newer, or gcc 3.4 or newer with the tr1 tuple library
56 (from Boost or other vendors).
shiqiane35fdd92008-12-10 05:08:54 +000057
58Furthermore, if you are building Google Mock from a VCS Checkout (also
59described below), there are further requirements:
60 * Automake version 1.9 or newer
61 * Autoconf version 2.59 or newer
62 * Libtool / Libtoolize
63 * Python version 2.3 or newer
64
65### Windows Requirements ###
66 * Microsoft Visual C++ 8.0 SP1 or newer
zhanyong.wan19e49af2009-01-14 21:09:22 +000067 * An implementation of the tr1 tuple C++ library (You can get it for
68 free from http://www.boost.org/. We have verified that version
69 1.36.0 works. One caveat is this implementation exposes a bug in
70 Visual C++'s <type_info> header when exceptions are disabled.
71 Therefore your project must enable exceptions for this
72 configuration to work.)
shiqiane35fdd92008-12-10 05:08:54 +000073
74### Mac OS X Requirements ###
75 * Mac OS X 10.4 Tiger or newer
76 * Developer Tools Installed
77
78Getting the Source
79------------------
80There are two primary ways of getting Google Mock's source code: you can
81download a source release in your preferred archive format, or directly check
82out the source from a Version Control System (VCS, we use Google Code's
83Subversion hosting). The VCS checkout requires a few extra steps and some extra
84software packages on your system, but lets you track development, and make
85patches to contribute much more easily, so we highly encourage it.
86
87### VCS Checkout: ###
88The first step is to select whether you want to check out the main line of
89development on Google Mock, or one of the released branches. The former will be
90much more active and have the latest features, but the latter provides much
91more stability and predictability. Choose whichever fits your needs best, and
92proceed with the following Subversion commands:
93
shiqianc50af1a2008-12-11 05:22:15 +000094 svn checkout http://googlemock.googlecode.com/svn/trunk/ gmock-svn
shiqiane35fdd92008-12-10 05:08:54 +000095
96or for a release version X.Y.*'s branch:
97
shiqianc50af1a2008-12-11 05:22:15 +000098 svn checkout http://googlemock.googlecode.com/svn/branches/release-X.Y/ \
shiqiane35fdd92008-12-10 05:08:54 +000099 gmock-X.Y-svn
100
101Next you will need to prepare the GNU Autotools build system, if you
102are using Linux or Mac OS X. Enter the target directory of the
103checkout command you used ('gmock-svn' or 'gmock-X.Y-svn' above) and
shiqian281b1d22008-12-11 00:13:55 +0000104proceed with the following command:
shiqiane35fdd92008-12-10 05:08:54 +0000105
shiqianc50af1a2008-12-11 05:22:15 +0000106 autoreconf -fvi
shiqiane35fdd92008-12-10 05:08:54 +0000107
shiqianc50af1a2008-12-11 05:22:15 +0000108Once you have completed this step, you are ready to build the library. Note
109that you should only need to complete this step once. The subsequent `make'
110invocations will automatically re-generate the bits of the build system that
111need to be changed.
shiqiane35fdd92008-12-10 05:08:54 +0000112
shiqian281b1d22008-12-11 00:13:55 +0000113If your system uses older versions of the autotools, the above command will
shiqianc50af1a2008-12-11 05:22:15 +0000114fail. You may need to explicitly specify a version to use. For instance, if you
115have both GNU Automake 1.4 and 1.9 installed and `automake' would invoke the
1161.4, use instead:
shiqian281b1d22008-12-11 00:13:55 +0000117
shiqianc50af1a2008-12-11 05:22:15 +0000118 AUTOMAKE=automake-1.9 ACLOCAL=aclocal-1.9 autoreconf -fvi
shiqian281b1d22008-12-11 00:13:55 +0000119
120Make sure you're using the same version of automake and aclocal.
shiqiane35fdd92008-12-10 05:08:54 +0000121
122### Source Package: ###
123Google Mock is also released in source packages which can be downloaded from
124its Google Code download page[1]. Several different archive formats are
shiqianc50af1a2008-12-11 05:22:15 +0000125provided, but the only difference is the tools needed to extract their
126contents, and the size of the resulting file. Download whichever you are most
127comfortable with.
shiqiane35fdd92008-12-10 05:08:54 +0000128
129 [1] Google Mock Downloads: http://code.google.com/p/googlemock/downloads/list
130
131Once downloaded expand the archive using whichever tools you prefer for that
132type. This will always result in a new directory with the name "gmock-X.Y.Z"
133which contains all of the source code. Here are some examples in Linux:
134
shiqianc50af1a2008-12-11 05:22:15 +0000135 tar -xvzf gmock-X.Y.Z.tar.gz
136 tar -xvjf gmock-X.Y.Z.tar.bz2
137 unzip gmock-X.Y.Z.zip
shiqiane35fdd92008-12-10 05:08:54 +0000138
139Building the Source
140-------------------
141### Linux and Mac OS X (without Xcode) ###
142There are two primary options for building the source at this point: build it
143inside the source code tree, or in a separate directory. We recommend building
144in a separate directory as that tends to produce both more consistent results
145and be easier to clean up should anything go wrong, but both patterns are
146supported. The only hard restriction is that while the build directory can be
147a subdirectory of the source directory, the opposite is not possible and will
148result in errors. Once you have selected where you wish to build Google Mock,
149create the directory if necessary, and enter it. The following steps apply for
150either approach by simply substituting the shell variable SRCDIR with "." for
151building inside the source directory, and the relative path to the source
152directory otherwise.
153
shiqianc50af1a2008-12-11 05:22:15 +0000154 ${SRCDIR}/configure # Standard GNU configure script, --help for more info
155
156The default behavior of the configure script with respect to locating and using
157Google Test is to first search for a 'gtest-config' in the system path, and
158lacking this, build an internal copy of Google Test. You may optionally specify
159a custom Google Test you wish to build Google Mock against, provided it is
160a new enough version.
161
162 # Configure against an installation in '/opt' with '/opt/bin/gtest-config'.
163 ${SRCDIR}/configure --with-gtest=/opt
164
165This can also be used to specify a Google Test which hasn't yet been installed.
166However, it must have been configured and built as described in the Google Test
167README before you configure Google Mock. To enable this feature, simply pass
168the directory where you configured and built Google Test (which is not
169necessarily its source directory) to Google Mock's configure script.
170
171 # Configure against a build of Google Test in an arbitrary directory.
172 ${SRCDIR}/configure --with-gtest=../../my_gtest_build
173
174Finally, if you have a version of Google Test installed but for some reason
175wish to forcibly prevent it from being used, we provide a special option.
176Typically this is not needed as we fall back to the internal Google Test
177packaged with Google Mock if an installed version is either unavailable or too
178old to build Google Mock. When using the internally packaged Google Test, the
179user does *not* need to configure or build it, that is automatically handled by
180Google Mock's build system.
181
182 # Force the use of the internally packaged Google Test, despite
183 # 'gtest-config' being in your PATH.
184 ${SRCDIR}/configure --disable-external-gtest
185
186Once you have successfully configured Google Mock, the build steps are standard
187for GNU-style OSS packages.
188
189 make # Standard makefile following GNU conventions
190 make check # Builds and runs all tests - all should pass
shiqiane35fdd92008-12-10 05:08:54 +0000191
192Other programs will only be able to use Google Mock's functionality if you
193install it in a location which they can access, in Linux this is typically
194under '/usr/local'. The following command will install all of the Google Mock
195libraries, public headers, and utilities necessary for other programs and
shiqianc50af1a2008-12-11 05:22:15 +0000196libraries to leverage it. Note that if Google Mock was unable to find an
197external Google Test to build against, it will also install the internally
198packaged Google Test in order to allow the installed Google Mock to function
199properly. This Google Test install will be fully functional, and if installed
200will also be uninstalled by uninstalling Google Mock.
shiqiane35fdd92008-12-10 05:08:54 +0000201
shiqianc50af1a2008-12-11 05:22:15 +0000202 sudo make install # Not necessary, but allows use by other programs
shiqiane35fdd92008-12-10 05:08:54 +0000203
shiqianc50af1a2008-12-11 05:22:15 +0000204Should you need to remove Google Mock from your system after having installed
205it, run the following command, and it will back out its changes. However, note
206carefully that you must run this command on the *same* Google Mock build that
207you ran the install from, or the results are not predictable. If you install
208Google Mock on your system, and are working from a VCS checkout, make sure you
209run this *before* updating your checkout of the source in order to uninstall
210the same version which you installed.
shiqiane35fdd92008-12-10 05:08:54 +0000211
shiqianc50af1a2008-12-11 05:22:15 +0000212 sudo make uninstall # Must be run against the exact same build as "install"
shiqiane35fdd92008-12-10 05:08:54 +0000213
shiqianc50af1a2008-12-11 05:22:15 +0000214Your project can build against Google Mock and Google Test simply by leveraging
215the 'gmock-config' script. This script can be invoked directly out of the
216'scripts' subdirectory of the build tree, and it will be installed in the
217binary directory specified during the 'configure'. Here are some examples of
218its use, see 'gmock-config --help' for more detailed information.
shiqiane35fdd92008-12-10 05:08:54 +0000219
shiqianc50af1a2008-12-11 05:22:15 +0000220 gmock-config --min-version=1.0 || echo "Insufficient Google Mock version."
221
222 g++ $(gmock-config --cppflags --cxxflags) -o foo.o -c foo.cpp
223 g++ $(gmock-config --ldflags --libs) -o foo foo.o
224
225 # When using a built but not installed Google Mock:
226 g++ $(../../my_gmock_build/scripts/gmock-config ...) ...
227
228Note that when building your project against Google Mock, you are building
229against Google Test as well. There is no need to configure Google Test
230separately.
shiqiane35fdd92008-12-10 05:08:54 +0000231
232### Windows ###
shiqian44a8cf12008-12-22 23:06:35 +0000233The msvc/ directory contains VC++ 2005 projects for building Google
234Mock and selected tests. In order to build Google Mock you must have
235an implementation of TR1 tuple. One library that provides such
236implementation is Boost. If you choose to use Boost, download it from
237www.boost.org and install it on your system. Note that Boost TR1 tuple
238is a header-only library, so the installation only involves unpacking
239it to a suitable location - you don't need to compile it or download a
240pre-compiled Boost binary.
241
zhanyong.wan19e49af2009-01-14 21:09:22 +0000242Since Boost is quite large, you may prefer to only install the files
243actually needed by Google Mock. If so, you can download TR1 tuple
244without other parts of Boost from
245http://code.google.com/p/googlemock/downloads/list.
246
shiqian44a8cf12008-12-22 23:06:35 +0000247After that you have two options: either set up Boost globally or
248modify the Google Mock project to point to your copy of Boost. The
249former will let all your tests use the same Boost library while the
250latter will allow each of your projects use its own copy. You can also
251use a hybrid solution: your project settings will override the
252system-wide one.
shiqianc6cece72008-12-10 07:50:41 +0000253
254For example, if you unpacked boost v1.36.0 into C:\boost:
shiqian44a8cf12008-12-22 23:06:35 +0000255To set up Boost such that all projects can use it:
shiqianc50af1a2008-12-11 05:22:15 +0000256 * Assuming you are using the Visual Studio 2005 IDE, select Tools |
shiqianc6cece72008-12-10 07:50:41 +0000257 Options | Projects And Solutions | VC++ Directories.
258 * In the "Show directories for" drop-down select Include Files. Add
shiqian44a8cf12008-12-22 23:06:35 +0000259 C:\boost\boost_1_36_0\boost\tr1\tr1 and C:\boost\boost_1_36_0 to the
260 list of directories.
shiqianc6cece72008-12-10 07:50:41 +0000261
262To configure your project to point to that version of Boost, replace
shiqian44a8cf12008-12-22 23:06:35 +0000263the value of the BoostDir user macro with C:\boost\boost_1_36_0 in the
shiqian9dd55ad2008-12-11 19:44:55 +0000264msvc/gmock_config.vsprops file. You can use any text editor to edit
265that file.
shiqianc6cece72008-12-10 07:50:41 +0000266
267If you want to use a version of Google Test other then the one bundled with
268Google Mock, change the value of the GTestDir macro in gmock_config.vsprop
269to point to the new location.
270
271After configuring Boost, just open msvc/gmock.sln and build the library and
272tests. If you want to create your own project to use with Google Mock, you'll
273have to configure it to use the gmock_config propety sheet. For that:
shiqianc50af1a2008-12-11 05:22:15 +0000274 * Open the Property Manager window (View | Other Windows | Property Manager)
shiqianc6cece72008-12-10 07:50:41 +0000275 * Right-click on your project and select "Add Existing Property Sheet..."
276 * Navigate to gmock_config.vsprops and select it.
shiqianc50af1a2008-12-11 05:22:15 +0000277 * In Project Properties | Configuration Properties | General | Additional
278 Include Directories, type <path to Google Mock>/include.
279
280TODO(wan@google.com): update the .vsprops and .vcproj files such that the
281last step is unnecessary.
shiqiane35fdd92008-12-10 05:08:54 +0000282
283### Using GNU Make ###
284The make/ directory contains a Makefile that you can use to build
285Google Mock on systems where GNU make is available (e.g. Linux and Mac
286OS X). It doesn't try to build Google Mock's own tests. Instead, it
287just builds the Google Mock libraries and some sample tests. You can
288use it as a starting point for your own Makefile.
289
290If the default settings are correct for your environment, the
291following commands should succeed:
292
shiqianc50af1a2008-12-11 05:22:15 +0000293 cd ${SRCDIR}/make
294 make
295 ./gmock_test
shiqiane35fdd92008-12-10 05:08:54 +0000296
297If you see errors, try to tweak the contents of make/Makefile to make
298them go away. There are instructions in make/Makefile on how to do
299it.
300
301### Using Your Own Build System ###
302If none of the build solutions we provide works for you, or if you
303prefer your own build system, you just need to compile
304${GTEST_SRCDIR}/src/gtest-all.cc (where GTEST_SRCDIR is the root of
305the Google Test source tree) and src/gmock-all.cc into a library and
306link your tests with it. Assuming a Linux-like system and gcc,
307something like the following will do:
308
shiqianc50af1a2008-12-11 05:22:15 +0000309 cd ${SRCDIR}
310 g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
shiqiane35fdd92008-12-10 05:08:54 +0000311 -c {GTEST_SRCDIR}/src/gtest-all.cc
shiqianc50af1a2008-12-11 05:22:15 +0000312 g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
shiqiane35fdd92008-12-10 05:08:54 +0000313 -c src/gmock-all.cc
shiqianc50af1a2008-12-11 05:22:15 +0000314 ar -rv libgmock.a gtest-all.o gmock-all.o
315 g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
shiqiane35fdd92008-12-10 05:08:54 +0000316 path/to/your_test.cc libgmock.a -o your_test
317
318On Windows, you'll also need to add the include path for the boost
319headers to the compiler command line. See
320http://www.boost.org/doc/libs/1_36_0/doc/html/boost_tr1/usage.html for
321how to do it.
322
323Regenerating Source Files
324-------------------------
325Some of Google Mock's source files are generated from templates (not
326in the C++ sense) using a script. A template file is named FOO.pump,
327where FOO is the name of the file it will generate. For example, the
328file include/gmock/gmock-generated-actions.h.pump is used to generate
329gmock-generated-actions.h in the same directory.
330
331Normally you don't need to worry about regenerating the source files,
332unless you need to modify them (e.g. if you are working on a patch for
333Google Mock). In that case, you should modify the corresponding .pump
334files instead and run the 'pump' script (for Pump is Useful for Meta
335Programming) to regenerate them. We are still working on releasing
336the script and its documentation. If you need it now, please email
337googlemock@googlegroups.com such that we know to make it happen
338sooner.
339
340Happy testing!