Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 1 | ## Google Mock ## |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 2 | |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 3 | The Google C++ mocking framework. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 4 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 5 | ### Overview ### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 6 | |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 7 | Google's framework for writing and using C++ mock classes. |
| 8 | It can help you derive better designs of your system and write better tests. |
| 9 | |
| 10 | It is inspired by: |
| 11 | |
| 12 | * [jMock](http://www.jmock.org/), |
| 13 | * [EasyMock](http://www.easymock.org/), and |
| 14 | * [Hamcrest](http://code.google.com/p/hamcrest/), |
| 15 | |
| 16 | and designed with C++'s specifics in mind. |
| 17 | |
| 18 | Google mock: |
| 19 | |
| 20 | * lets you create mock classes trivially using simple macros. |
| 21 | * supports a rich set of matchers and actions. |
| 22 | * handles unordered, partially ordered, or completely ordered expectations. |
| 23 | * is extensible by users. |
| 24 | |
| 25 | We hope you find it useful! |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 26 | |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 27 | ### Features ### |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 28 | |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 29 | * Provides a declarative syntax for defining mocks. |
| 30 | * Can easily define partial (hybrid) mocks, which are a cross of real |
| 31 | and mock objects. |
| 32 | * Handles functions of arbitrary types and overloaded functions. |
| 33 | * Comes with a rich set of matchers for validating function arguments. |
| 34 | * Uses an intuitive syntax for controlling the behavior of a mock. |
| 35 | * Does automatic verification of expectations (no record-and-replay needed). |
| 36 | * Allows arbitrary (partial) ordering constraints on |
| 37 | function calls to be expressed,. |
Jerry Turcios | 11319f1 | 2018-10-31 12:56:21 -0400 | [diff] [blame] | 38 | * Lets a user extend it by defining new matchers and actions. |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 39 | * Does not use exceptions. |
| 40 | * Is easy to learn and use. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 41 | |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 42 | Please see the project page above for more information as well as the |
| 43 | mailing list for questions, discussions, and development. There is |
| 44 | also an IRC channel on OFTC (irc.oftc.net) #gtest available. Please |
| 45 | join us! |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 46 | |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 47 | Please note that code under [scripts/generator](scripts/generator/) is |
| 48 | from [cppclean](http://code.google.com/p/cppclean/) and released under |
| 49 | the Apache License, which is different from Google Mock's license. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 50 | |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 51 | ## Getting Started ## |
| 52 | |
| 53 | If you are new to the project, we suggest that you read the user |
| 54 | documentation in the following order: |
| 55 | |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 56 | * Learn the [basics](../googletest/docs/primer.md) of |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 57 | Google Test, if you choose to use Google Mock with it (recommended). |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 58 | * Read [Google Mock for Dummies](../googlemock/docs/ForDummies.md). |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 59 | * Read the instructions below on how to build Google Mock. |
| 60 | |
| 61 | You can also watch Zhanyong's [talk](http://www.youtube.com/watch?v=sYpCyLI47rM) on Google Mock's usage and implementation. |
| 62 | |
| 63 | Once you understand the basics, check out the rest of the docs: |
| 64 | |
Krystian Kuzniarek | 275bbc7 | 2019-07-13 07:47:14 +0200 | [diff] [blame^] | 65 | * [CheatSheet](../googlemock/docs/cheat_sheet.md) - all the commonly used stuff |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 66 | at a glance. |
Gennadiy Civil | 5ed950c | 2019-06-19 16:48:38 -0400 | [diff] [blame] | 67 | * [CookBook](../googlemock/docs/cook_book.md) - recipes for getting things done, |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 68 | including advanced techniques. |
| 69 | |
| 70 | If you need help, please check the |
Samuel Roth | 995db99 | 2016-09-12 12:27:36 -0400 | [diff] [blame] | 71 | [KnownIssues](docs/KnownIssues.md) and |
| 72 | [FrequentlyAskedQuestions](docs/FrequentlyAskedQuestions.md) before |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 73 | posting a question on the |
| 74 | [discussion group](http://groups.google.com/group/googlemock). |
| 75 | |
| 76 | |
| 77 | ### Using Google Mock Without Google Test ### |
| 78 | |
| 79 | Google Mock is not a testing framework itself. Instead, it needs a |
| 80 | testing framework for writing tests. Google Mock works seamlessly |
Herbert Thielen | e033d8c | 2017-08-31 18:12:17 +0200 | [diff] [blame] | 81 | with [Google Test](https://github.com/google/googletest), but |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 82 | you can also use it with [any C++ testing framework](../googlemock/docs/ForDummies.md#using-google-mock-with-any-testing-framework). |
Billy Donahue | 23bd6d8 | 2015-08-30 12:36:37 -0400 | [diff] [blame] | 83 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 84 | ### Requirements for End Users ### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 85 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 86 | Google Mock is implemented on top of [Google Test]( |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 87 | http://github.com/google/googletest/), and depends on it. |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 88 | You must use the bundled version of Google Test when using Google Mock. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 89 | |
zhanyong.wan | 62417be | 2009-03-19 18:39:41 +0000 | [diff] [blame] | 90 | You can also easily configure Google Mock to work with another testing |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 91 | framework, although it will still need Google Test. Please read |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 92 | ["Using_Google_Mock_with_Any_Testing_Framework"]( |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 93 | ../googlemock/docs/ForDummies.md#using-google-mock-with-any-testing-framework) |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 94 | for instructions. |
zhanyong.wan | 62417be | 2009-03-19 18:39:41 +0000 | [diff] [blame] | 95 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 96 | Google Mock depends on advanced C++ features and thus requires a more |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 97 | modern compiler. The following are needed to use Google Mock: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 98 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 99 | #### Linux Requirements #### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 100 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 101 | * GNU-compatible Make or "gmake" |
| 102 | * POSIX-standard shell |
| 103 | * POSIX(-2) Regular Expressions (regex.h) |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 104 | * C++98-standard-compliant compiler (e.g. GCC 3.4 or newer) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 105 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 106 | #### Windows Requirements #### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 107 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 108 | * Microsoft Visual C++ 8.0 SP1 or newer |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 109 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 110 | #### Mac OS X Requirements #### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 111 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 112 | * Mac OS X 10.4 Tiger or newer |
| 113 | * Developer Tools Installed |
| 114 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 115 | ### Requirements for Contributors ### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 116 | |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 117 | We welcome patches. If you plan to contribute a patch, you need to |
| 118 | build Google Mock and its tests, which has further requirements: |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 119 | |
| 120 | * Automake version 1.9 or newer |
| 121 | * Autoconf version 2.59 or newer |
| 122 | * Libtool / Libtoolize |
| 123 | * Python version 2.3 or newer (for running some of the tests and |
| 124 | re-generating certain source files from templates) |
| 125 | |
Billy Donahue | 3f4e79b | 2015-08-26 21:45:10 -0400 | [diff] [blame] | 126 | ### Building Google Mock ### |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 127 | |
danilcha | b2521c8 | 2017-03-11 01:02:21 +0100 | [diff] [blame] | 128 | #### Using CMake #### |
| 129 | |
Craig Scott | cb502b7 | 2015-12-08 00:14:24 +1100 | [diff] [blame] | 130 | If you have CMake available, it is recommended that you follow the |
| 131 | [build instructions][gtest_cmakebuild] |
Gennadiy Civil | de47b0d | 2018-06-11 11:26:16 -0400 | [diff] [blame] | 132 | as described for Google Test. |
danilcha | b2521c8 | 2017-03-11 01:02:21 +0100 | [diff] [blame] | 133 | |
| 134 | If are using Google Mock with an |
Craig Scott | cb502b7 | 2015-12-08 00:14:24 +1100 | [diff] [blame] | 135 | existing CMake project, the section |
| 136 | [Incorporating Into An Existing CMake Project][gtest_incorpcmake] |
Gennadiy Civil | de47b0d | 2018-06-11 11:26:16 -0400 | [diff] [blame] | 137 | may be of particular interest. |
| 138 | To make it work for Google Mock you will need to change |
danilcha | b2521c8 | 2017-03-11 01:02:21 +0100 | [diff] [blame] | 139 | |
| 140 | target_link_libraries(example gtest_main) |
| 141 | |
Gennadiy Civil | de47b0d | 2018-06-11 11:26:16 -0400 | [diff] [blame] | 142 | to |
danilcha | b2521c8 | 2017-03-11 01:02:21 +0100 | [diff] [blame] | 143 | |
danilcha | 5ff6805 | 2017-03-12 18:11:22 +0100 | [diff] [blame] | 144 | target_link_libraries(example gmock_main) |
Gennadiy Civil | de47b0d | 2018-06-11 11:26:16 -0400 | [diff] [blame] | 145 | |
danilcha | 5ff6805 | 2017-03-12 18:11:22 +0100 | [diff] [blame] | 146 | This works because `gmock_main` library is compiled with Google Test. |
Craig Scott | cb502b7 | 2015-12-08 00:14:24 +1100 | [diff] [blame] | 147 | |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 148 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 149 | ### Tweaking Google Mock ### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 150 | |
| 151 | Google Mock can be used in diverse environments. The default |
| 152 | configuration may not work (or may not work well) out of the box in |
| 153 | some environments. However, you can easily tweak Google Mock by |
| 154 | defining control macros on the compiler command line. Generally, |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 155 | these macros are named like `GTEST_XYZ` and you define them to either 1 |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 156 | or 0 to enable or disable a certain feature. |
| 157 | |
| 158 | We list the most frequently used macros below. For a complete list, |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 159 | see file [${GTEST\_DIR}/include/gtest/internal/gtest-port.h]( |
| 160 | ../googletest/include/gtest/internal/gtest-port.h). |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 161 | |
vladlosev | bce8134 | 2011-05-20 21:15:36 +0000 | [diff] [blame] | 162 | ### As a Shared Library (DLL) ### |
| 163 | |
| 164 | Google Mock is compact, so most users can build and link it as a static |
| 165 | library for the simplicity. Google Mock can be used as a DLL, but the |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 166 | same DLL must contain Google Test as well. See |
| 167 | [Google Test's README][gtest_readme] |
| 168 | for instructions on how to set up necessary compiler settings. |
vladlosev | bce8134 | 2011-05-20 21:15:36 +0000 | [diff] [blame] | 169 | |
| 170 | ### Tweaking Google Mock ### |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 171 | |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 172 | Most of Google Test's control macros apply to Google Mock as well. |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 173 | Please see [Google Test's README][gtest_readme] for how to tweak them. |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 174 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 175 | ### Upgrading from an Earlier Version ### |
shiqian | c50af1a | 2008-12-11 05:22:15 +0000 | [diff] [blame] | 176 | |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 177 | We strive to keep Google Mock releases backward compatible. |
| 178 | Sometimes, though, we have to make some breaking changes for the |
| 179 | users' long-term benefits. This section describes what you'll need to |
| 180 | do if you are upgrading from an earlier version of Google Mock. |
| 181 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 182 | #### Upgrading from 1.1.0 or Earlier #### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 183 | |
| 184 | You may need to explicitly enable or disable Google Test's own TR1 |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 185 | tuple library. See the instructions in section "[Choosing a TR1 Tuple |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 186 | Library](#choosing-a-tr1-tuple-library)". |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 187 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 188 | #### Upgrading from 1.4.0 or Earlier #### |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 189 | |
| 190 | On platforms where the pthread library is available, Google Test and |
| 191 | Google Mock use it in order to be thread-safe. For this to work, you |
| 192 | may need to tweak your compiler and/or linker flags. Please see the |
Arkady Shapkin | e6c407d | 2018-09-04 23:07:18 +0300 | [diff] [blame] | 193 | "[Multi-threaded Tests](../googletest/README.md#multi-threaded-tests)" section in file Google Test's README for what you may need to do. |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 194 | |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 195 | If you have custom matchers defined using `MatcherInterface` or |
| 196 | `MakePolymorphicMatcher()`, you'll need to update their definitions to |
| 197 | use the new matcher API ( |
Gennadiy Civil | 5ed950c | 2019-06-19 16:48:38 -0400 | [diff] [blame] | 198 | [monomorphic](./docs/cook_book.md#writing-new-monomorphic-matchers), |
| 199 | [polymorphic](./docs/cook_book.md#writing-new-polymorphic-matchers)). |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 200 | Matchers defined using `MATCHER()` or `MATCHER_P*()` aren't affected. |
zhanyong.wan | d19f586 | 2010-04-14 16:26:57 +0000 | [diff] [blame] | 201 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 202 | Happy testing! |
Billy Donahue | 06fcd9f | 2015-08-25 13:51:43 -0400 | [diff] [blame] | 203 | |
Billy Donahue | 37b5c29 | 2015-08-26 18:28:20 -0400 | [diff] [blame] | 204 | [gtest_readme]: ../googletest/README.md "googletest" |
Craig Scott | cb502b7 | 2015-12-08 00:14:24 +1100 | [diff] [blame] | 205 | [gtest_cmakebuild]: ../googletest/README.md#using-cmake "Using CMake" |
| 206 | [gtest_incorpcmake]: ../googletest/README.md#incorporating-into-an-existing-cmake-project "Incorporating Into An Existing CMake Project" |