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