blob: f57b254d65cb8e08c99b2c88a12676b7aa8e9e3e [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001Google C++ Mocking Framework
2============================
zhanyong.wand19f5862010-04-14 16:26:57 +00003
shiqiane35fdd92008-12-10 05:08:54 +00004http://code.google.com/p/googlemock/
5
6Overview
7--------
zhanyong.wand19f5862010-04-14 16:26:57 +00008
9Google's framework for writing and using C++ mock classes on a variety
10of platforms (Linux, Mac OS X, Windows, Windows CE, Symbian, etc).
11Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s
12specifics in mind, it can help you derive better designs of your
13system and write better tests.
shiqiane35fdd92008-12-10 05:08:54 +000014
15Google Mock:
16
17- provides a declarative syntax for defining mocks,
18- can easily define partial (hybrid) mocks, which are a cross of real
19 and mock objects,
20- handles functions of arbitrary types and overloaded functions,
21- comes with a rich set of matchers for validating function arguments,
22- uses an intuitive syntax for controlling the behavior of a mock,
23- does automatic verification of expectations (no record-and-replay
24 needed),
25- allows arbitrary (partial) ordering constraints on
26 function calls to be expressed,
27- lets a user extend it by defining new matchers and actions.
28- does not use exceptions, and
29- is easy to learn and use.
30
zhanyong.wand19f5862010-04-14 16:26:57 +000031Please see the project page above for more information as well as the
32mailing list for questions, discussions, and development. There is
33also an IRC channel on OFTC (irc.oftc.net) #gtest available. Please
34join us!
shiqiane35fdd92008-12-10 05:08:54 +000035
36Please note that code under scripts/generator/ is from the cppclean
37project (http://code.google.com/p/cppclean/) and under the Apache
shiqianc50af1a2008-12-11 05:22:15 +000038License, which is different from Google Mock's license.
shiqiane35fdd92008-12-10 05:08:54 +000039
zhanyong.wand19f5862010-04-14 16:26:57 +000040Requirements for End Users
41--------------------------
42
43Google Mock is implemented on top of the Google Test C++ testing
44framework (http://code.google.com/p/googletest/), and includes the
45latter as part of the SVN repositary and distribution package. You
46must use the bundled version of Google Test when using Google Mock, or
47you may get compiler/linker errors.
shiqiane35fdd92008-12-10 05:08:54 +000048
zhanyong.wan62417be2009-03-19 18:39:41 +000049You can also easily configure Google Mock to work with another testing
50framework of your choice; although it will still need Google Test as
51an internal dependency. Please read
52http://code.google.com/p/googlemock/wiki/ForDummies#Using_Google_Mock_with_Any_Testing_Framework
53for how to do it.
54
shiqiane35fdd92008-12-10 05:08:54 +000055Google Mock depends on advanced C++ features and thus requires a more
56modern compiler. The following are needed to use Google Mock:
57
58### Linux Requirements ###
zhanyong.wand19f5862010-04-14 16:26:57 +000059
shiqiane35fdd92008-12-10 05:08:54 +000060These are the base requirements to build and use Google Mock from a source
61package (as described below):
zhanyong.wand19f5862010-04-14 16:26:57 +000062
shiqiane35fdd92008-12-10 05:08:54 +000063 * GNU-compatible Make or "gmake"
64 * POSIX-standard shell
65 * POSIX(-2) Regular Expressions (regex.h)
zhanyong.wand19f5862010-04-14 16:26:57 +000066 * C++98-standard-compliant compiler (e.g. GCC 3.4 or newer)
shiqiane35fdd92008-12-10 05:08:54 +000067
68### Windows Requirements ###
zhanyong.wand19f5862010-04-14 16:26:57 +000069
shiqiane35fdd92008-12-10 05:08:54 +000070 * Microsoft Visual C++ 8.0 SP1 or newer
shiqiane35fdd92008-12-10 05:08:54 +000071
72### Mac OS X Requirements ###
zhanyong.wand19f5862010-04-14 16:26:57 +000073
shiqiane35fdd92008-12-10 05:08:54 +000074 * Mac OS X 10.4 Tiger or newer
75 * Developer Tools Installed
76
zhanyong.wand19f5862010-04-14 16:26:57 +000077Requirements for Contributors
78-----------------------------
79
80We welcome patches. If you plan to contribute a patch, you need to
81build Google Mock and its own tests from an SVN checkout (described
82below), which has further requirements:
83
84 * Automake version 1.9 or newer
85 * Autoconf version 2.59 or newer
86 * Libtool / Libtoolize
87 * Python version 2.3 or newer (for running some of the tests and
88 re-generating certain source files from templates)
89
shiqiane35fdd92008-12-10 05:08:54 +000090Getting the Source
91------------------
shiqiane35fdd92008-12-10 05:08:54 +000092
zhanyong.wand19f5862010-04-14 16:26:57 +000093There are two primary ways of getting Google Mock's source code: you
94can download a stable source release in your preferred archive format,
95or directly check out the source from our Subversion (SVN) repositary.
96The SVN checkout requires a few extra steps and some extra software
97packages on your system, but lets you track development and make
98patches much more easily, so we highly encourage it.
shiqiane35fdd92008-12-10 05:08:54 +000099
zhanyong.wand19f5862010-04-14 16:26:57 +0000100### Source Package ###
shiqiane35fdd92008-12-10 05:08:54 +0000101
zhanyong.wand19f5862010-04-14 16:26:57 +0000102Google Mock is released in versioned source packages which can be
103downloaded from the download page [1]. Several different archive
104formats are provided, but the only difference is the tools needed to
105extract their contents, and the size of the resulting file. Download
106whichever you are most comfortable with.
shiqiane35fdd92008-12-10 05:08:54 +0000107
zhanyong.wand19f5862010-04-14 16:26:57 +0000108 [1] http://code.google.com/p/googlemock/downloads/list
shiqiane35fdd92008-12-10 05:08:54 +0000109
zhanyong.wand19f5862010-04-14 16:26:57 +0000110Once downloaded expand the archive using whichever tools you prefer
111for that type. This will always result in a new directory with the
112name "gmock-X.Y.Z" which contains all of the source code. Here are
113some examples on Linux:
shiqiane35fdd92008-12-10 05:08:54 +0000114
shiqianc50af1a2008-12-11 05:22:15 +0000115 tar -xvzf gmock-X.Y.Z.tar.gz
116 tar -xvjf gmock-X.Y.Z.tar.bz2
117 unzip gmock-X.Y.Z.zip
shiqiane35fdd92008-12-10 05:08:54 +0000118
zhanyong.wand19f5862010-04-14 16:26:57 +0000119### SVN Checkout ###
120
121To check out the main branch (also known as the "trunk") of Google
122Mock, run the following Subversion command:
123
124 svn checkout http://googlemock.googlecode.com/svn/trunk/ gmock-svn
125
126If you are using a *nix system and plan to use the GNU Autotools build
127system to build Google Mock (described below), you'll need to
128configure it now. Otherwise you are done with getting the source
129files.
130
131To prepare the Autotools build system, enter the target directory of
132the checkout command you used ('gmock-svn') and proceed with the
133following command:
134
135 autoreconf -fvi
136
137Once you have completed this step, you are ready to build the library.
138Note that you should only need to complete this step once. The
139subsequent 'make' invocations will automatically re-generate the bits
140of the build system that need to be changed.
141
142If your system uses older versions of the autotools, the above command
143will fail. You may need to explicitly specify a version to use. For
144instance, if you have both GNU Automake 1.4 and 1.9 installed and
145'automake' would invoke the 1.4, use instead:
146
147 AUTOMAKE=automake-1.9 ACLOCAL=aclocal-1.9 autoreconf -fvi
148
149Make sure you're using the same version of automake and aclocal.
150
151Setting up the Build
152--------------------
153
154To build Google Mock and your tests that use it, you need to tell your
155build system where to find its headers and source files. The exact
156way to do it depends on which build system you use, and is usually
157straightforward.
158
159### Generic Build Instructions ###
160
161This section shows how you can integrate Google Mock into your
162existing build system.
163
164Suppose you put Google Mock in directory ${GMOCK_DIR} and Google Test
165in ${GTEST_DIR} (the latter is ${GMOCK_DIR}/gtest by default). To
166build Google Mock, create a library build target (or a project as
167called by Visual Studio and Xcode) to compile
168
169 ${GTEST_DIR}/src/gtest-all.cc and ${GMOCK_DIR}/src/gmock-all.cc
170
171with
172
173 ${GTEST_DIR}/include, ${GTEST_DIR}, ${GMOCK_DIR}/include, and ${GMOCK_DIR}
174
175in the header search path. Assuming a Linux-like system and gcc,
176something like the following will do:
177
178 g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -I${GMOCK_DIR}/include \
179 -I${GMOCK_DIR} -c ${GTEST_DIR}/src/gtest-all.cc
180 g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -I${GMOCK_DIR}/include \
181 -I${GMOCK_DIR} -c ${GMOCK_DIR}/src/gmock-all.cc
182 ar -rv libgmock.a gtest-all.o gmock-all.o
183
184Next, you should compile your test source file with
185${GTEST_DIR}/include and ${GMOCK_DIR}/include in the header search
186path, and link it with gmock and any other necessary libraries:
187
188 g++ -I${GTEST_DIR}/include -I${GMOCK_DIR}/include \
189 path/to/your_test.cc libgmock.a -o your_test
190
191As an example, the make/ directory contains a Makefile that you can
192use to build Google Mock on systems where GNU make is available
193(e.g. Linux, Mac OS X, and Cygwin). It doesn't try to build Google
194Mock's own tests. Instead, it just builds the Google Mock library and
195a sample test. You can use it as a starting point for your own build
196script.
197
198If the default settings are correct for your environment, the
199following commands should succeed:
200
201 cd ${GMOCK_DIR}/make
202 make
203 ./gmock_test
204
205If you see errors, try to tweak the contents of make/Makefile to make
206them go away. There are instructions in make/Makefile on how to do
207it.
208
209### Windows ###
210
211The msvc/ directory contains VC++ 2005 projects for building Google
212Mock and selected tests.
213
214Open msvc/gmock.sln and build the library and tests. If you want to
215create your own project to use with Google Mock, you'll have to
216configure it to use the gmock_config propety sheet. For that:
217
218 * Open the Property Manager window (View | Other Windows | Property Manager)
219 * Right-click on your project and select "Add Existing Property Sheet..."
220 * Navigate to gmock_config.vsprops and select it.
221 * In Project Properties | Configuration Properties | General | Additional
222 Include Directories, type <path to Google Mock>/include.
223
224Tweaking Google Mock
225--------------------
226
227Google Mock can be used in diverse environments. The default
228configuration may not work (or may not work well) out of the box in
229some environments. However, you can easily tweak Google Mock by
230defining control macros on the compiler command line. Generally,
231these macros are named like GTEST_XYZ and you define them to either 1
232or 0 to enable or disable a certain feature.
233
234We list the most frequently used macros below. For a complete list,
235see file ${GTEST_DIR}/include/gtest/internal/gtest-port.h.
236
237### Choosing a TR1 Tuple Library ###
238
zhanyong.wan90c90f92009-06-17 22:11:04 +0000239Google Mock uses the C++ Technical Report 1 (TR1) tuple library
240heavily. Unfortunately TR1 tuple is not yet widely available with all
241compilers. The good news is that Google Test 1.4.0+ implements a
242subset of TR1 tuple that's enough for Google Mock's need. Google Mock
243will automatically use that implementation when the compiler doesn't
244provide TR1 tuple.
245
246Usually you don't need to care about which tuple library Google Test
247and Google Mock use. However, if your project already uses TR1 tuple,
248you need to tell Google Test and Google Mock to use the same TR1 tuple
zhanyong.wand19f5862010-04-14 16:26:57 +0000249library the rest of your project uses, or the two tuple
zhanyong.wan90c90f92009-06-17 22:11:04 +0000250implementations will clash. To do that, add
251
252 -DGTEST_USE_OWN_TR1_TUPLE=0
253
254to the compiler flags while compiling Google Test, Google Mock, and
zhanyong.wand19f5862010-04-14 16:26:57 +0000255your tests. If you want to force Google Test and Google Mock to use
256their own tuple library, just add
257
258 -DGTEST_USE_OWN_TR1_TUPLE=1
259
260to the compiler flags instead.
zhanyong.wan90c90f92009-06-17 22:11:04 +0000261
262If you want to use Boost's TR1 tuple library with Google Mock, please
263refer to the Boost website (http://www.boost.org/) for how to obtain
264it and set it up.
265
zhanyong.wand19f5862010-04-14 16:26:57 +0000266### Tweaking Google Test ###
shiqiane35fdd92008-12-10 05:08:54 +0000267
zhanyong.wand19f5862010-04-14 16:26:57 +0000268Most of Google Test's control macros apply to Google Mock as well.
269Please see file ${GTEST_DIR}/README for how to tweak them.
shiqianc50af1a2008-12-11 05:22:15 +0000270
zhanyong.wand19f5862010-04-14 16:26:57 +0000271Upgrading from an Earlier Version
272---------------------------------
shiqianc50af1a2008-12-11 05:22:15 +0000273
zhanyong.wand19f5862010-04-14 16:26:57 +0000274We strive to keep Google Mock releases backward compatible.
275Sometimes, though, we have to make some breaking changes for the
276users' long-term benefits. This section describes what you'll need to
277do if you are upgrading from an earlier version of Google Mock.
278
279### Upgrading from 1.2.0 or Earlier ###
280
281You may need to explicitly enable or disable Google Test's own TR1
282tuple library. See the instructions in section "Choosing a TR1 Tuple
283Library".
284
285### Upgrading from 1.4.0 or Earlier ###
286
287On platforms where the pthread library is available, Google Test and
288Google Mock use it in order to be thread-safe. For this to work, you
289may need to tweak your compiler and/or linker flags. Please see the
290"Multi-threaded Tests" section in file ${GTEST_DIR}/README for what
291you may need to do.
292
293If you have custom matchers defined using MatcherInterface or
294MakePolymorphicMatcher(), you'll need to update their definitions to
295use the new matcher API [2]. Matchers defined using MATCHER() or
296MATCHER_P*() aren't affected.
297
298 [2] http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Monomorphic_Matchers,
299 http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Polymorphic_Matchers
300
301Developing Google Mock
302----------------------
303
304This section discusses how to make your own changes to Google Mock.
305
306### Testing Google Mock Itself ###
307
308To make sure your changes work as intended and don't break existing
309functionality, you'll want to compile and run Google Test's own tests.
310For that you'll need Autotools. First, make sure you have followed
311the instructions in section "SVN Checkout" to configure Google Mock.
312Then, create a build output directory and enter it. Next,
313
314 ${GMOCK_DIR}/configure # Standard GNU configure script, --help for more info
315
316Once you have successfully configured Google Mock, the build steps are
317standard for GNU-style OSS packages.
318
319 make # Standard makefile following GNU conventions
320 make check # Builds and runs all tests - all should pass.
shiqiane35fdd92008-12-10 05:08:54 +0000321
shiqianc50af1a2008-12-11 05:22:15 +0000322Note that when building your project against Google Mock, you are building
zhanyong.wand19f5862010-04-14 16:26:57 +0000323against Google Test as well. There is no need to configure Google Test
shiqianc50af1a2008-12-11 05:22:15 +0000324separately.
shiqiane35fdd92008-12-10 05:08:54 +0000325
zhanyong.wand19f5862010-04-14 16:26:57 +0000326### Regenerating Source Files ###
shiqianc6cece72008-12-10 07:50:41 +0000327
shiqiane35fdd92008-12-10 05:08:54 +0000328Some of Google Mock's source files are generated from templates (not
329in the C++ sense) using a script. A template file is named FOO.pump,
330where FOO is the name of the file it will generate. For example, the
331file include/gmock/gmock-generated-actions.h.pump is used to generate
332gmock-generated-actions.h in the same directory.
333
334Normally you don't need to worry about regenerating the source files,
zhanyong.wand19f5862010-04-14 16:26:57 +0000335unless you need to modify them. In that case, you should modify the
336corresponding .pump files instead and run the 'pump' script (for Pump
337is Useful for Meta Programming) to regenerate them. You can find
338pump.py in the ${GTEST_DIR}/scripts/ directory. Read the Pump manual
339[3] for how to use it.
340
341 [3] http://code.google.com/p/googletest/wiki/PumpManual.
342
343### Contributing a Patch ###
344
345We welcome patches. Please read the Google Mock developer's guide [4]
346for how you can contribute. In particular, make sure you have signed
347the Contributor License Agreement, or we won't be able to accept the
348patch.
349
350 [4] http://code.google.com/p/googlemock/wiki/DevGuide
shiqiane35fdd92008-12-10 05:08:54 +0000351
352Happy testing!