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