blob: 77610e2804360d2703fa353e4b5d3cd5e2779fef [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
34License.
35
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
43compatible version you already have.
44
45TODO(wan@google.com): describe which Google Test versions are
46compatible with the latest Google Mock release.
47
48Google Mock depends on advanced C++ features and thus requires a more
49modern compiler. The following are needed to use Google Mock:
50
51### Linux Requirements ###
52These are the base requirements to build and use Google Mock from a source
53package (as described below):
54 * GNU-compatible Make or "gmake"
55 * POSIX-standard shell
56 * POSIX(-2) Regular Expressions (regex.h)
57 * gcc 4.0 or newer
58
59Furthermore, if you are building Google Mock from a VCS Checkout (also
60described below), there are further requirements:
61 * Automake version 1.9 or newer
62 * Autoconf version 2.59 or newer
63 * Libtool / Libtoolize
64 * Python version 2.3 or newer
65
66### Windows Requirements ###
67 * Microsoft Visual C++ 8.0 SP1 or newer
68 * An implementation of the tr1 C++ library (You can get it for free
69 from http://www.boost.org/. We have verified that version 1.36.0
70 works. One caveat is this implementation exposes a bug in Visual
71 C++'s <type_info> header when exceptions are disabled. Therefore
72 your project must enable exceptions for this configuration to work.)
73
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
94 $ svn checkout http://googlemock.googlecode.com/svn/trunk/ gmock-svn
95
96or for a release version X.Y.*'s branch:
97
98 $ svn checkout http://googlemock.googlecode.com/svn/branches/release-X.Y/ \
99 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
104proceed with the following commands:
105
106 $ aclocal-1.9 # Where "1.9" must match the following automake command.
107 $ libtoolize -c # Use "glibtoolize -c" instead on Mac OS X.
108 $ autoheader
109 $ automake-1.9 -ac # See Automake version requirements above.
110 $ autoconf
111
112While this is a bit complicated, it will most often be automatically re-run by
113your "make" invocations, so in practice you shouldn't need to worry too much.
114Once you have completed these steps, you are ready to build the library.
115
116TODO(chandlerc@google.com): Update the above with instructions on
117preparing the build system for Google Test.
118
119### Source Package: ###
120Google Mock is also released in source packages which can be downloaded from
121its Google Code download page[1]. Several different archive formats are
122provided, but the only difference is the tools used to manipulate them, and the
123size of the resulting file. Download whichever you are most comfortable with.
124
125 [1] Google Mock Downloads: http://code.google.com/p/googlemock/downloads/list
126
127Once downloaded expand the archive using whichever tools you prefer for that
128type. This will always result in a new directory with the name "gmock-X.Y.Z"
129which contains all of the source code. Here are some examples in Linux:
130
131 $ tar -xvzf gmock-X.Y.Z.tar.gz
132 $ tar -xvjf gmock-X.Y.Z.tar.bz2
133 $ unzip gmock-X.Y.Z.zip
134
135Building the Source
136-------------------
137### Linux and Mac OS X (without Xcode) ###
138There are two primary options for building the source at this point: build it
139inside the source code tree, or in a separate directory. We recommend building
140in a separate directory as that tends to produce both more consistent results
141and be easier to clean up should anything go wrong, but both patterns are
142supported. The only hard restriction is that while the build directory can be
143a subdirectory of the source directory, the opposite is not possible and will
144result in errors. Once you have selected where you wish to build Google Mock,
145create the directory if necessary, and enter it. The following steps apply for
146either approach by simply substituting the shell variable SRCDIR with "." for
147building inside the source directory, and the relative path to the source
148directory otherwise.
149
150 $ ${SRCDIR}/configure # Standard GNU configure script, --help for more info
151 $ make # Standard makefile following GNU conventions
152 $ make check # Builds and runs all tests - all should pass
153
154Other programs will only be able to use Google Mock's functionality if you
155install it in a location which they can access, in Linux this is typically
156under '/usr/local'. The following command will install all of the Google Mock
157libraries, public headers, and utilities necessary for other programs and
158libraries to leverage it:
159
160 $ sudo make install # Not necessary, but allows use by other programs
161
162TODO(chandlerc@google.com): This section needs to be expanded when the
163'gmock-config' script is finished and Autoconf macro's are provided (or not
164provided) in order to properly reflect the process for other programs to
165locate, include, and link against Google Mock.
166
167Finally, should you need to remove Google Mock from your system after having
168installed it, run the following command, and it will back out its changes.
169However, note carefully that you must run this command on the *same* Google
170Mock build that you ran the install from, or the results are not predictable.
171If you install Google Mock on your system, and are working from a VCS checkout,
172make sure you run this *before* updating your checkout of the source in order
173to uninstall the same version which you installed.
174
175 $ sudo make uninstall # Must be run against the exact same build as "install"
176
177TODO(chandlerc@google.com): Fixes the above instructions to match the
178actual implementation.
179
180### Windows ###
shiqianc6cece72008-12-10 07:50:41 +0000181The msvc/ directory contains VC++ 2005 projects for building Google Mock and
182selected tests. In order to build Google Mock you must have an implementation
183of TR1 tuple. One library that provides such implementation is Boost. If you
184choose to use Boost, download it from www.boost.org and install it on your
185system. After that you have two options: either configure Boost as a system
186library or modify the Google Mock project to point to your copy of Boost. The
187former solution will let all your tests use the same copy of Boost while the
188latter one will let each of your projects use its own copy of Boost. You can
189also use a hybrid solution: your project settings will override the system-wide
190one.
191
192For example, if you unpacked boost v1.36.0 into C:\boost:
193To configure Boost as a system library.
194 * Assuming you are using the Visual Studio 2008 IDE, select Tools |
195 Options | Projects And Solutions | VC++ Directories.
196 * In the "Show directories for" drop-down select Include Files. Add
197 * C:\boost\boost_1_36_0\boost\tr1\tr1 and C:\boost\boost_1_36_0
198 to the list of directories.
199
200To configure your project to point to that version of Boost, replace
201the value of the BoostDir user macro with C:\boost\boost_1_36_0 in the
202msvc/gtest_dep.vsprops file. You can use any text editor to edit that file.
203
204If you want to use a version of Google Test other then the one bundled with
205Google Mock, change the value of the GTestDir macro in gmock_config.vsprop
206to point to the new location.
207
208After configuring Boost, just open msvc/gmock.sln and build the library and
209tests. If you want to create your own project to use with Google Mock, you'll
210have to configure it to use the gmock_config propety sheet. For that:
211 * Open the Property Manager window (View/Other Windows/Property Manager)
212 * Right-click on your project and select "Add Existing Property Sheet..."
213 * Navigate to gmock_config.vsprops and select it.
shiqiane35fdd92008-12-10 05:08:54 +0000214
215### Using GNU Make ###
216The make/ directory contains a Makefile that you can use to build
217Google Mock on systems where GNU make is available (e.g. Linux and Mac
218OS X). It doesn't try to build Google Mock's own tests. Instead, it
219just builds the Google Mock libraries and some sample tests. You can
220use it as a starting point for your own Makefile.
221
222If the default settings are correct for your environment, the
223following commands should succeed:
224
225 $ cd ${SRCDIR}/make
226 $ make
227 $ ./gmock_test
228
229If you see errors, try to tweak the contents of make/Makefile to make
230them go away. There are instructions in make/Makefile on how to do
231it.
232
233### Using Your Own Build System ###
234If none of the build solutions we provide works for you, or if you
235prefer your own build system, you just need to compile
236${GTEST_SRCDIR}/src/gtest-all.cc (where GTEST_SRCDIR is the root of
237the Google Test source tree) and src/gmock-all.cc into a library and
238link your tests with it. Assuming a Linux-like system and gcc,
239something like the following will do:
240
241 $ cd ${SRCDIR}
242 $ g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
243 -c {GTEST_SRCDIR}/src/gtest-all.cc
244 $ g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
245 -c src/gmock-all.cc
246 $ ar -rv libgmock.a gtest-all.o gmock-all.o
247 $ g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \
248 path/to/your_test.cc libgmock.a -o your_test
249
250On Windows, you'll also need to add the include path for the boost
251headers to the compiler command line. See
252http://www.boost.org/doc/libs/1_36_0/doc/html/boost_tr1/usage.html for
253how to do it.
254
255Regenerating Source Files
256-------------------------
257Some of Google Mock's source files are generated from templates (not
258in the C++ sense) using a script. A template file is named FOO.pump,
259where FOO is the name of the file it will generate. For example, the
260file include/gmock/gmock-generated-actions.h.pump is used to generate
261gmock-generated-actions.h in the same directory.
262
263Normally you don't need to worry about regenerating the source files,
264unless you need to modify them (e.g. if you are working on a patch for
265Google Mock). In that case, you should modify the corresponding .pump
266files instead and run the 'pump' script (for Pump is Useful for Meta
267Programming) to regenerate them. We are still working on releasing
268the script and its documentation. If you need it now, please email
269googlemock@googlegroups.com such that we know to make it happen
270sooner.
271
272Happy testing!