blob: 34a7ed99988444dfbea9a7930dd1666087b7291f [file] [log] [blame] [view]
Billy Donahue23bd6d82015-08-30 12:36:37 -04001## Google Mock ##
zhanyong.wand19f5862010-04-14 16:26:57 +00002
Billy Donahue23bd6d82015-08-30 12:36:37 -04003The Google C++ mocking framework.
shiqiane35fdd92008-12-10 05:08:54 +00004
Billy Donahue37b5c292015-08-26 18:28:20 -04005### Overview ###
zhanyong.wand19f5862010-04-14 16:26:57 +00006
Billy Donahue23bd6d82015-08-30 12:36:37 -04007Google's framework for writing and using C++ mock classes.
8It can help you derive better designs of your system and write better tests.
9
10It 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
16and designed with C++'s specifics in mind.
17
18Google 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
25We hope you find it useful!
shiqiane35fdd92008-12-10 05:08:54 +000026
Billy Donahue3f4e79b2015-08-26 21:45:10 -040027### Features ###
shiqiane35fdd92008-12-10 05:08:54 +000028
Billy Donahue3f4e79b2015-08-26 21:45:10 -040029 * 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 Turcios11319f12018-10-31 12:56:21 -040038 * Lets a user extend it by defining new matchers and actions.
Billy Donahue3f4e79b2015-08-26 21:45:10 -040039 * Does not use exceptions.
40 * Is easy to learn and use.
shiqiane35fdd92008-12-10 05:08:54 +000041
zhanyong.wand19f5862010-04-14 16:26:57 +000042Please see the project page above for more information as well as the
43mailing list for questions, discussions, and development. There is
44also an IRC channel on OFTC (irc.oftc.net) #gtest available. Please
45join us!
shiqiane35fdd92008-12-10 05:08:54 +000046
Billy Donahue3f4e79b2015-08-26 21:45:10 -040047Please note that code under [scripts/generator](scripts/generator/) is
48from [cppclean](http://code.google.com/p/cppclean/) and released under
49the Apache License, which is different from Google Mock's license.
shiqiane35fdd92008-12-10 05:08:54 +000050
Billy Donahue23bd6d82015-08-30 12:36:37 -040051## Getting Started ##
52
53If you are new to the project, we suggest that you read the user
54documentation in the following order:
55
Arkady Shapkine6c407d2018-09-04 23:07:18 +030056 * Learn the [basics](../googletest/docs/primer.md) of
Billy Donahue23bd6d82015-08-30 12:36:37 -040057 Google Test, if you choose to use Google Mock with it (recommended).
Arkady Shapkine6c407d2018-09-04 23:07:18 +030058 * Read [Google Mock for Dummies](../googlemock/docs/ForDummies.md).
Billy Donahue23bd6d82015-08-30 12:36:37 -040059 * Read the instructions below on how to build Google Mock.
60
61You can also watch Zhanyong's [talk](http://www.youtube.com/watch?v=sYpCyLI47rM) on Google Mock's usage and implementation.
62
63Once you understand the basics, check out the rest of the docs:
64
Krystian Kuzniarek275bbc72019-07-13 07:47:14 +020065 * [CheatSheet](../googlemock/docs/cheat_sheet.md) - all the commonly used stuff
Billy Donahue23bd6d82015-08-30 12:36:37 -040066 at a glance.
Gennadiy Civil5ed950c2019-06-19 16:48:38 -040067 * [CookBook](../googlemock/docs/cook_book.md) - recipes for getting things done,
Billy Donahue23bd6d82015-08-30 12:36:37 -040068 including advanced techniques.
69
70If you need help, please check the
Samuel Roth995db992016-09-12 12:27:36 -040071[KnownIssues](docs/KnownIssues.md) and
72[FrequentlyAskedQuestions](docs/FrequentlyAskedQuestions.md) before
Billy Donahue23bd6d82015-08-30 12:36:37 -040073posting a question on the
74[discussion group](http://groups.google.com/group/googlemock).
75
76
77### Using Google Mock Without Google Test ###
78
79Google Mock is not a testing framework itself. Instead, it needs a
80testing framework for writing tests. Google Mock works seamlessly
Herbert Thielene033d8c2017-08-31 18:12:17 +020081with [Google Test](https://github.com/google/googletest), but
Arkady Shapkine6c407d2018-09-04 23:07:18 +030082you can also use it with [any C++ testing framework](../googlemock/docs/ForDummies.md#using-google-mock-with-any-testing-framework).
Billy Donahue23bd6d82015-08-30 12:36:37 -040083
Billy Donahue37b5c292015-08-26 18:28:20 -040084### Requirements for End Users ###
zhanyong.wand19f5862010-04-14 16:26:57 +000085
Billy Donahue37b5c292015-08-26 18:28:20 -040086Google Mock is implemented on top of [Google Test](
Billy Donahue3f4e79b2015-08-26 21:45:10 -040087http://github.com/google/googletest/), and depends on it.
Billy Donahue37b5c292015-08-26 18:28:20 -040088You must use the bundled version of Google Test when using Google Mock.
shiqiane35fdd92008-12-10 05:08:54 +000089
zhanyong.wan62417be2009-03-19 18:39:41 +000090You can also easily configure Google Mock to work with another testing
Billy Donahue37b5c292015-08-26 18:28:20 -040091framework, although it will still need Google Test. Please read
Billy Donahue37b5c292015-08-26 18:28:20 -040092["Using_Google_Mock_with_Any_Testing_Framework"](
Arkady Shapkine6c407d2018-09-04 23:07:18 +030093 ../googlemock/docs/ForDummies.md#using-google-mock-with-any-testing-framework)
Billy Donahue37b5c292015-08-26 18:28:20 -040094for instructions.
zhanyong.wan62417be2009-03-19 18:39:41 +000095
shiqiane35fdd92008-12-10 05:08:54 +000096Google Mock depends on advanced C++ features and thus requires a more
Billy Donahue37b5c292015-08-26 18:28:20 -040097modern compiler. The following are needed to use Google Mock:
shiqiane35fdd92008-12-10 05:08:54 +000098
Billy Donahue37b5c292015-08-26 18:28:20 -040099#### Linux Requirements ####
zhanyong.wand19f5862010-04-14 16:26:57 +0000100
shiqiane35fdd92008-12-10 05:08:54 +0000101 * GNU-compatible Make or "gmake"
102 * POSIX-standard shell
103 * POSIX(-2) Regular Expressions (regex.h)
zhanyong.wand19f5862010-04-14 16:26:57 +0000104 * C++98-standard-compliant compiler (e.g. GCC 3.4 or newer)
shiqiane35fdd92008-12-10 05:08:54 +0000105
Billy Donahue37b5c292015-08-26 18:28:20 -0400106#### Windows Requirements ####
zhanyong.wand19f5862010-04-14 16:26:57 +0000107
shiqiane35fdd92008-12-10 05:08:54 +0000108 * Microsoft Visual C++ 8.0 SP1 or newer
shiqiane35fdd92008-12-10 05:08:54 +0000109
Billy Donahue37b5c292015-08-26 18:28:20 -0400110#### Mac OS X Requirements ####
zhanyong.wand19f5862010-04-14 16:26:57 +0000111
shiqiane35fdd92008-12-10 05:08:54 +0000112 * Mac OS X 10.4 Tiger or newer
113 * Developer Tools Installed
114
Billy Donahue37b5c292015-08-26 18:28:20 -0400115### Requirements for Contributors ###
zhanyong.wand19f5862010-04-14 16:26:57 +0000116
Billy Donahue3f4e79b2015-08-26 21:45:10 -0400117We welcome patches. If you plan to contribute a patch, you need to
118build Google Mock and its tests, which has further requirements:
zhanyong.wand19f5862010-04-14 16:26:57 +0000119
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 Donahue3f4e79b2015-08-26 21:45:10 -0400126### Building Google Mock ###
shiqiane35fdd92008-12-10 05:08:54 +0000127
danilchab2521c82017-03-11 01:02:21 +0100128#### Using CMake ####
129
Craig Scottcb502b72015-12-08 00:14:24 +1100130If you have CMake available, it is recommended that you follow the
131[build instructions][gtest_cmakebuild]
Gennadiy Civilde47b0d2018-06-11 11:26:16 -0400132as described for Google Test.
danilchab2521c82017-03-11 01:02:21 +0100133
134If are using Google Mock with an
Craig Scottcb502b72015-12-08 00:14:24 +1100135existing CMake project, the section
136[Incorporating Into An Existing CMake Project][gtest_incorpcmake]
Gennadiy Civilde47b0d2018-06-11 11:26:16 -0400137may be of particular interest.
138To make it work for Google Mock you will need to change
danilchab2521c82017-03-11 01:02:21 +0100139
140 target_link_libraries(example gtest_main)
141
Gennadiy Civilde47b0d2018-06-11 11:26:16 -0400142to
danilchab2521c82017-03-11 01:02:21 +0100143
danilcha5ff68052017-03-12 18:11:22 +0100144 target_link_libraries(example gmock_main)
Gennadiy Civilde47b0d2018-06-11 11:26:16 -0400145
danilcha5ff68052017-03-12 18:11:22 +0100146This works because `gmock_main` library is compiled with Google Test.
Craig Scottcb502b72015-12-08 00:14:24 +1100147
zhanyong.wand19f5862010-04-14 16:26:57 +0000148
Billy Donahue37b5c292015-08-26 18:28:20 -0400149### Tweaking Google Mock ###
zhanyong.wand19f5862010-04-14 16:26:57 +0000150
151Google Mock can be used in diverse environments. The default
152configuration may not work (or may not work well) out of the box in
153some environments. However, you can easily tweak Google Mock by
154defining control macros on the compiler command line. Generally,
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400155these macros are named like `GTEST_XYZ` and you define them to either 1
zhanyong.wand19f5862010-04-14 16:26:57 +0000156or 0 to enable or disable a certain feature.
157
158We list the most frequently used macros below. For a complete list,
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400159see file [${GTEST\_DIR}/include/gtest/internal/gtest-port.h](
160../googletest/include/gtest/internal/gtest-port.h).
zhanyong.wand19f5862010-04-14 16:26:57 +0000161
vladlosevbce81342011-05-20 21:15:36 +0000162### As a Shared Library (DLL) ###
163
164Google Mock is compact, so most users can build and link it as a static
165library for the simplicity. Google Mock can be used as a DLL, but the
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400166same DLL must contain Google Test as well. See
167[Google Test's README][gtest_readme]
168for instructions on how to set up necessary compiler settings.
vladlosevbce81342011-05-20 21:15:36 +0000169
170### Tweaking Google Mock ###
shiqiane35fdd92008-12-10 05:08:54 +0000171
zhanyong.wand19f5862010-04-14 16:26:57 +0000172Most of Google Test's control macros apply to Google Mock as well.
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400173Please see [Google Test's README][gtest_readme] for how to tweak them.
shiqianc50af1a2008-12-11 05:22:15 +0000174
Billy Donahue37b5c292015-08-26 18:28:20 -0400175### Upgrading from an Earlier Version ###
shiqianc50af1a2008-12-11 05:22:15 +0000176
zhanyong.wand19f5862010-04-14 16:26:57 +0000177We strive to keep Google Mock releases backward compatible.
178Sometimes, though, we have to make some breaking changes for the
179users' long-term benefits. This section describes what you'll need to
180do if you are upgrading from an earlier version of Google Mock.
181
Billy Donahue37b5c292015-08-26 18:28:20 -0400182#### Upgrading from 1.1.0 or Earlier ####
zhanyong.wand19f5862010-04-14 16:26:57 +0000183
184You may need to explicitly enable or disable Google Test's own TR1
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400185tuple library. See the instructions in section "[Choosing a TR1 Tuple
Arkady Shapkine6c407d2018-09-04 23:07:18 +0300186Library](#choosing-a-tr1-tuple-library)".
zhanyong.wand19f5862010-04-14 16:26:57 +0000187
Billy Donahue37b5c292015-08-26 18:28:20 -0400188#### Upgrading from 1.4.0 or Earlier ####
zhanyong.wand19f5862010-04-14 16:26:57 +0000189
190On platforms where the pthread library is available, Google Test and
191Google Mock use it in order to be thread-safe. For this to work, you
192may need to tweak your compiler and/or linker flags. Please see the
Arkady Shapkine6c407d2018-09-04 23:07:18 +0300193"[Multi-threaded Tests](../googletest/README.md#multi-threaded-tests)" section in file Google Test's README for what you may need to do.
zhanyong.wand19f5862010-04-14 16:26:57 +0000194
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400195If you have custom matchers defined using `MatcherInterface` or
196`MakePolymorphicMatcher()`, you'll need to update their definitions to
197use the new matcher API (
Gennadiy Civil5ed950c2019-06-19 16:48:38 -0400198[monomorphic](./docs/cook_book.md#writing-new-monomorphic-matchers),
199[polymorphic](./docs/cook_book.md#writing-new-polymorphic-matchers)).
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400200Matchers defined using `MATCHER()` or `MATCHER_P*()` aren't affected.
zhanyong.wand19f5862010-04-14 16:26:57 +0000201
shiqiane35fdd92008-12-10 05:08:54 +0000202Happy testing!
Billy Donahue06fcd9f2015-08-25 13:51:43 -0400203
Billy Donahue37b5c292015-08-26 18:28:20 -0400204[gtest_readme]: ../googletest/README.md "googletest"
Craig Scottcb502b72015-12-08 00:14:24 +1100205[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"