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 |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 34 | License, which is different from Google Mock's license. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 35 | |
| 36 | Requirements |
| 37 | ------------ |
| 38 | Google Mock is not a testing framework itself. Instead, it needs a |
zhanyong.wan | 62417be | 2009-03-19 18:39:41 +0000 | [diff] [blame] | 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 |
shiqian | 281b1d2 | 2008-12-11 00:13:55 +0000 | [diff] [blame] | 42 | compatible version you already have. This version of Google Mock |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 43 | requires Google Test 1.4.0. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 44 | |
zhanyong.wan | 62417be | 2009-03-19 18:39:41 +0000 | [diff] [blame] | 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_Testing_Framework |
| 49 | for how to do it. |
| 50 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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) |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 60 | * gcc 3.4 or newer. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 61 | |
| 62 | Furthermore, if you are building Google Mock from a VCS Checkout (also |
| 63 | described below), there are further requirements: |
| 64 | * Automake version 1.9 or newer |
| 65 | * Autoconf version 2.59 or newer |
| 66 | * Libtool / Libtoolize |
| 67 | * Python version 2.3 or newer |
| 68 | |
| 69 | ### Windows Requirements ### |
| 70 | * Microsoft Visual C++ 8.0 SP1 or newer |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 92 | svn checkout http://googlemock.googlecode.com/svn/trunk/ gmock-svn |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 93 | |
| 94 | or for a release version X.Y.*'s branch: |
| 95 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 96 | svn checkout http://googlemock.googlecode.com/svn/branches/release-X.Y/ \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 104 | autoreconf -fvi |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 105 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 106 | Once you have completed this step, you are ready to build the library. Note |
| 107 | that you should only need to complete this step once. The subsequent `make' |
| 108 | invocations will automatically re-generate the bits of the build system that |
| 109 | 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 |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 112 | fail. You may need to explicitly specify a version to use. For instance, if you |
| 113 | have both GNU Automake 1.4 and 1.9 installed and `automake' would invoke the |
| 114 | 1.4, use instead: |
shiqian | 281b1d2 | 2008-12-11 00:13:55 +0000 | [diff] [blame] | 115 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 116 | AUTOMAKE=automake-1.9 ACLOCAL=aclocal-1.9 autoreconf -fvi |
shiqian | 281b1d2 | 2008-12-11 00:13:55 +0000 | [diff] [blame] | 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 |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 123 | provided, but the only difference is the tools needed to extract their |
| 124 | contents, and the size of the resulting file. Download whichever you are most |
| 125 | comfortable with. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 126 | |
| 127 | [1] Google Mock Downloads: http://code.google.com/p/googlemock/downloads/list |
| 128 | |
| 129 | Once downloaded expand the archive using whichever tools you prefer for that |
| 130 | type. This will always result in a new directory with the name "gmock-X.Y.Z" |
| 131 | which contains all of the source code. Here are some examples in Linux: |
| 132 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 133 | tar -xvzf gmock-X.Y.Z.tar.gz |
| 134 | tar -xvjf gmock-X.Y.Z.tar.bz2 |
| 135 | unzip gmock-X.Y.Z.zip |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 136 | |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 137 | Choosing a TR1 Tuple Library |
| 138 | ---------------------------- |
| 139 | Google Mock uses the C++ Technical Report 1 (TR1) tuple library |
| 140 | heavily. Unfortunately TR1 tuple is not yet widely available with all |
| 141 | compilers. The good news is that Google Test 1.4.0+ implements a |
| 142 | subset of TR1 tuple that's enough for Google Mock's need. Google Mock |
| 143 | will automatically use that implementation when the compiler doesn't |
| 144 | provide TR1 tuple. |
| 145 | |
| 146 | Usually you don't need to care about which tuple library Google Test |
| 147 | and Google Mock use. However, if your project already uses TR1 tuple, |
| 148 | you need to tell Google Test and Google Mock to use the same TR1 tuple |
| 149 | library the rest of your project uses (this requirement is new in |
| 150 | Google Test 1.4.0 and Google Mock 1.2.0, so you may need to take care |
| 151 | of it when upgrading from an earlier version), or the two tuple |
| 152 | implementations will clash. To do that, add |
| 153 | |
| 154 | -DGTEST_USE_OWN_TR1_TUPLE=0 |
| 155 | |
| 156 | to the compiler flags while compiling Google Test, Google Mock, and |
| 157 | your tests. |
| 158 | |
| 159 | If you want to use Boost's TR1 tuple library with Google Mock, please |
| 160 | refer to the Boost website (http://www.boost.org/) for how to obtain |
| 161 | it and set it up. |
| 162 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 163 | Building the Source |
| 164 | ------------------- |
| 165 | ### Linux and Mac OS X (without Xcode) ### |
| 166 | There are two primary options for building the source at this point: build it |
| 167 | inside the source code tree, or in a separate directory. We recommend building |
| 168 | in a separate directory as that tends to produce both more consistent results |
| 169 | and be easier to clean up should anything go wrong, but both patterns are |
| 170 | supported. The only hard restriction is that while the build directory can be |
| 171 | a subdirectory of the source directory, the opposite is not possible and will |
| 172 | result in errors. Once you have selected where you wish to build Google Mock, |
| 173 | create the directory if necessary, and enter it. The following steps apply for |
| 174 | either approach by simply substituting the shell variable SRCDIR with "." for |
| 175 | building inside the source directory, and the relative path to the source |
| 176 | directory otherwise. |
| 177 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 178 | ${SRCDIR}/configure # Standard GNU configure script, --help for more info |
| 179 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 180 | Once you have successfully configured Google Mock, the build steps are standard |
| 181 | for GNU-style OSS packages. |
| 182 | |
| 183 | make # Standard makefile following GNU conventions |
| 184 | make check # Builds and runs all tests - all should pass |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 185 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 186 | Note that when building your project against Google Mock, you are building |
| 187 | against Google Test as well. There is no need to configure Google Test |
| 188 | separately. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 189 | |
| 190 | ### Windows ### |
shiqian | 44a8cf1 | 2008-12-22 23:06:35 +0000 | [diff] [blame] | 191 | The msvc/ directory contains VC++ 2005 projects for building Google |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 192 | Mock and selected tests. |
shiqian | c6cece7 | 2008-12-10 07:50:41 +0000 | [diff] [blame] | 193 | |
| 194 | If you want to use a version of Google Test other then the one bundled with |
| 195 | Google Mock, change the value of the GTestDir macro in gmock_config.vsprop |
| 196 | to point to the new location. |
| 197 | |
zhanyong.wan | 90c90f9 | 2009-06-17 22:11:04 +0000 | [diff] [blame] | 198 | Open msvc/gmock.sln and build the library and tests. If you want to |
| 199 | create your own project to use with Google Mock, you'll have to |
| 200 | configure it to use the gmock_config propety sheet. For that: |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 201 | * Open the Property Manager window (View | Other Windows | Property Manager) |
shiqian | c6cece7 | 2008-12-10 07:50:41 +0000 | [diff] [blame] | 202 | * Right-click on your project and select "Add Existing Property Sheet..." |
| 203 | * Navigate to gmock_config.vsprops and select it. |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 204 | * In Project Properties | Configuration Properties | General | Additional |
| 205 | Include Directories, type <path to Google Mock>/include. |
| 206 | |
| 207 | TODO(wan@google.com): update the .vsprops and .vcproj files such that the |
| 208 | last step is unnecessary. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 209 | |
| 210 | ### Using GNU Make ### |
| 211 | The make/ directory contains a Makefile that you can use to build |
| 212 | Google Mock on systems where GNU make is available (e.g. Linux and Mac |
| 213 | OS X). It doesn't try to build Google Mock's own tests. Instead, it |
| 214 | just builds the Google Mock libraries and some sample tests. You can |
| 215 | use it as a starting point for your own Makefile. |
| 216 | |
| 217 | If the default settings are correct for your environment, the |
| 218 | following commands should succeed: |
| 219 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 220 | cd ${SRCDIR}/make |
| 221 | make |
| 222 | ./gmock_test |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 223 | |
| 224 | If you see errors, try to tweak the contents of make/Makefile to make |
| 225 | them go away. There are instructions in make/Makefile on how to do |
| 226 | it. |
| 227 | |
| 228 | ### Using Your Own Build System ### |
| 229 | If none of the build solutions we provide works for you, or if you |
| 230 | prefer your own build system, you just need to compile |
| 231 | ${GTEST_SRCDIR}/src/gtest-all.cc (where GTEST_SRCDIR is the root of |
| 232 | the Google Test source tree) and src/gmock-all.cc into a library and |
| 233 | link your tests with it. Assuming a Linux-like system and gcc, |
| 234 | something like the following will do: |
| 235 | |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 236 | cd ${SRCDIR} |
| 237 | g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 238 | -c {GTEST_SRCDIR}/src/gtest-all.cc |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 239 | g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 240 | -c src/gmock-all.cc |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 241 | ar -rv libgmock.a gtest-all.o gmock-all.o |
| 242 | g++ -I. -I./include -I${GTEST_SRCDIR} -I${GTEST_SRCDIR}/include \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 243 | path/to/your_test.cc libgmock.a -o your_test |
| 244 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 245 | Regenerating Source Files |
| 246 | ------------------------- |
| 247 | Some of Google Mock's source files are generated from templates (not |
| 248 | in the C++ sense) using a script. A template file is named FOO.pump, |
| 249 | where FOO is the name of the file it will generate. For example, the |
| 250 | file include/gmock/gmock-generated-actions.h.pump is used to generate |
| 251 | gmock-generated-actions.h in the same directory. |
| 252 | |
| 253 | Normally you don't need to worry about regenerating the source files, |
| 254 | unless you need to modify them (e.g. if you are working on a patch for |
| 255 | Google Mock). In that case, you should modify the corresponding .pump |
| 256 | files instead and run the 'pump' script (for Pump is Useful for Meta |
| 257 | Programming) to regenerate them. We are still working on releasing |
| 258 | the script and its documentation. If you need it now, please email |
| 259 | googlemock@googlegroups.com such that we know to make it happen |
| 260 | sooner. |
| 261 | |
| 262 | Happy testing! |