Commit 48928352 authored by Abseil Team's avatar Abseil Team Committed by Derek Mauro
Browse files

Googletest export

Move all docs into top-level docs/ directory

PiperOrigin-RevId: 350211277
parent 996b65e6
...@@ -371,8 +371,8 @@ Verifies that `val1` is less than, or almost equal to, `val2`. You can replace ...@@ -371,8 +371,8 @@ Verifies that `val1` is less than, or almost equal to, `val2`. You can replace
### Asserting Using gMock Matchers ### Asserting Using gMock Matchers
[gMock](../../googlemock) comes with [gMock](gmock_index.md) comes with
[a library of matchers](../../googlemock/docs/cheat_sheet.md#MatcherList) for [a library of matchers](gmock_cheat_sheet.md#MatcherList) for
validating arguments passed to mock objects. A gMock *matcher* is basically a validating arguments passed to mock objects. A gMock *matcher* is basically a
predicate that knows how to describe itself. It can be used in these assertion predicate that knows how to describe itself. It can be used in these assertion
macros: macros:
...@@ -396,13 +396,13 @@ using ::testing::StartsWith; ...@@ -396,13 +396,13 @@ using ::testing::StartsWith;
``` ```
Read this Read this
[recipe](../../googlemock/docs/cook_book.md#using-matchers-in-googletest-assertions) [recipe](gmock_cook_book.md#using-matchers-in-googletest-assertions)
in the gMock Cookbook for more details. in the gMock Cookbook for more details.
gMock has a rich set of matchers. You can do many things googletest cannot do gMock has a rich set of matchers. You can do many things googletest cannot do
alone with them. For a list of matchers gMock provides, read alone with them. For a list of matchers gMock provides, read
[this](../../googlemock/docs/cook_book.md##using-matchers). It's easy to write [this](gmock_cook_book.md##using-matchers). It's easy to write
your [own matchers](../../googlemock/docs/cook_book.md#NewMatchers) too. your [own matchers](gmock_cook_book.md#NewMatchers) too.
gMock is bundled with googletest, so you don't need to add any build dependency gMock is bundled with googletest, so you don't need to add any build dependency
in order to take advantage of this. Just include `"gmock/gmock.h"` in order to take advantage of this. Just include `"gmock/gmock.h"`
...@@ -414,7 +414,7 @@ and you're ready to go. ...@@ -414,7 +414,7 @@ and you're ready to go.
you haven't.) you haven't.)
You can use the gMock You can use the gMock
[string matchers](../../googlemock/docs/cheat_sheet.md#string-matchers) with [string matchers](gmock_cheat_sheet.md#string-matchers) with
`EXPECT_THAT()` or `ASSERT_THAT()` to do more string comparison tricks `EXPECT_THAT()` or `ASSERT_THAT()` to do more string comparison tricks
(sub-string, prefix, suffix, regular expression, and etc). For example, (sub-string, prefix, suffix, regular expression, and etc). For example,
...@@ -915,7 +915,7 @@ handlers registered with `pthread_atfork(3)`. ...@@ -915,7 +915,7 @@ handlers registered with `pthread_atfork(3)`.
Note: If you want to put a series of test assertions in a subroutine to check Note: If you want to put a series of test assertions in a subroutine to check
for a complex condition, consider using for a complex condition, consider using
[a custom GMock matcher](../../googlemock/docs/cook_book.md#NewMatchers) [a custom GMock matcher](gmock_cook_book.md#NewMatchers)
instead. This lets you provide a more readable error message in case of failure instead. This lets you provide a more readable error message in case of failure
and avoid all of the issues described below. and avoid all of the issues described below.
......
...@@ -179,7 +179,7 @@ Example usage: ...@@ -179,7 +179,7 @@ Example usage:
To customize the default action for a particular method of a specific mock To customize the default action for a particular method of a specific mock
object, use `ON_CALL()`. `ON_CALL()` has a similar syntax to `EXPECT_CALL()`, object, use `ON_CALL()`. `ON_CALL()` has a similar syntax to `EXPECT_CALL()`,
but it is used for setting default behaviors (when you do not require that the but it is used for setting default behaviors (when you do not require that the
mock method is called). See [here](cook_book.md#UseOnCall) for a more detailed mock method is called). See [here](gmock_cook_book.md#UseOnCall) for a more detailed
discussion. discussion.
```cpp ```cpp
...@@ -477,7 +477,7 @@ You can make a matcher from one or more other matchers: ...@@ -477,7 +477,7 @@ You can make a matcher from one or more other matchers:
| Matcher | Description | | Matcher | Description |
| :---------------------- | :------------------------------------ | | :---------------------- | :------------------------------------ |
| `MatcherCast<T>(m)` | casts matcher `m` to type `Matcher<T>`. | | `MatcherCast<T>(m)` | casts matcher `m` to type `Matcher<T>`. |
| `SafeMatcherCast<T>(m)` | [safely casts](cook_book.md#casting-matchers) matcher `m` to type `Matcher<T>`. | | `SafeMatcherCast<T>(m)` | [safely casts](gmock_cook_book.md#casting-matchers) matcher `m` to type `Matcher<T>`. |
| `Truly(predicate)` | `predicate(argument)` returns something considered by C++ to be true, where `predicate` is a function or functor. | | `Truly(predicate)` | `predicate(argument)` returns something considered by C++ to be true, where `predicate` is a function or functor. |
<!-- mdformat on --> <!-- mdformat on -->
...@@ -774,7 +774,7 @@ class MockFunction<R(A1, ..., An)> { ...@@ -774,7 +774,7 @@ class MockFunction<R(A1, ..., An)> {
}; };
``` ```
See this [recipe](cook_book.md#using-check-points) for one application of it. See this [recipe](gmock_cook_book.md#using-check-points) for one application of it.
## Flags ## Flags
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
<!-- GOOGLETEST_CM0012 DO NOT DELETE --> <!-- GOOGLETEST_CM0012 DO NOT DELETE -->
You can find recipes for using gMock here. If you haven't yet, please read You can find recipes for using gMock here. If you haven't yet, please read
[the dummy guide](for_dummies.md) first to make sure you understand the basics. [the dummy guide](gmock_for_dummies.md) first to make sure you understand the
basics.
**Note:** gMock lives in the `testing` name space. For readability, it is **Note:** gMock lives in the `testing` name space. For readability, it is
recommended to write `using ::testing::Foo;` once in your file before using the recommended to write `using ::testing::Foo;` once in your file before using the
...@@ -1141,11 +1142,11 @@ Hamcrest project, which adds `assertThat()` to JUnit. ...@@ -1141,11 +1142,11 @@ Hamcrest project, which adds `assertThat()` to JUnit.
### Using Predicates as Matchers ### Using Predicates as Matchers
gMock provides a [built-in set](cheat_sheet.md#MatcherList) of matchers. In case gMock provides a [built-in set](gmock_cheat_sheet.md#MatcherList) of matchers.
you find them lacking, you can use an arbitrary unary predicate function or In case you find them lacking, you can use an arbitrary unary predicate function
functor as a matcher - as long as the predicate accepts a value of the type you or functor as a matcher - as long as the predicate accepts a value of the type
want. You do this by wrapping the predicate inside the `Truly()` function, for you want. You do this by wrapping the predicate inside the `Truly()` function,
example: for example:
```cpp ```cpp
using ::testing::Truly; using ::testing::Truly;
...@@ -1710,7 +1711,7 @@ the test should reflect our real intent, instead of being overly constraining. ...@@ -1710,7 +1711,7 @@ the test should reflect our real intent, instead of being overly constraining.
gMock allows you to impose an arbitrary DAG (directed acyclic graph) on the gMock allows you to impose an arbitrary DAG (directed acyclic graph) on the
calls. One way to express the DAG is to use the calls. One way to express the DAG is to use the
[After](cheat_sheet.md#AfterClause) clause of `EXPECT_CALL`. [After](gmock_cheat_sheet.md#AfterClause) clause of `EXPECT_CALL`.
Another way is via the `InSequence()` clause (not the same as the `InSequence` Another way is via the `InSequence()` clause (not the same as the `InSequence`
class), which we borrowed from jMock 2. It's less flexible than `After()`, but class), which we borrowed from jMock 2. It's less flexible than `After()`, but
...@@ -3714,8 +3715,8 @@ A cardinality is used in `Times()` to tell gMock how many times you expect a ...@@ -3714,8 +3715,8 @@ A cardinality is used in `Times()` to tell gMock how many times you expect a
call to occur. It doesn't have to be exact. For example, you can say call to occur. It doesn't have to be exact. For example, you can say
`AtLeast(5)` or `Between(2, 4)`. `AtLeast(5)` or `Between(2, 4)`.
If the [built-in set](cheat_sheet.md#CardinalityList) of cardinalities doesn't If the [built-in set](gmock_cheat_sheet.md#CardinalityList) of cardinalities
suit you, you are free to define your own by implementing the following doesn't suit you, you are free to define your own by implementing the following
interface (in namespace `testing`): interface (in namespace `testing`):
```cpp ```cpp
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
### When I call a method on my mock object, the method for the real object is invoked instead. What's the problem? ### When I call a method on my mock object, the method for the real object is invoked instead. What's the problem?
In order for a method to be mocked, it must be *virtual*, unless you use the In order for a method to be mocked, it must be *virtual*, unless you use the
[high-perf dependency injection technique](cook_book.md#MockingNonVirtualMethods). [high-perf dependency injection technique](gmock_cook_book.md#MockingNonVirtualMethods).
### Can I mock a variadic function? ### Can I mock a variadic function?
...@@ -93,9 +93,9 @@ trace, you'll gain insights on why the expectations you set are not met. ...@@ -93,9 +93,9 @@ trace, you'll gain insights on why the expectations you set are not met.
If you see the message "The mock function has no default action set, and its If you see the message "The mock function has no default action set, and its
return type has no default value set.", then try return type has no default value set.", then try
[adding a default action](for_dummies.md#DefaultValue). Due to a known issue, [adding a default action](gmock_for_dummies.md#DefaultValue). Due to a known
unexpected calls on mocks without default actions don't print out a detailed issue, unexpected calls on mocks without default actions don't print out a
comparison between the actual arguments and the expected arguments. detailed comparison between the actual arguments and the expected arguments.
### My program crashed and `ScopedMockLog` spit out tons of messages. Is it a gMock bug? ### My program crashed and `ScopedMockLog` spit out tons of messages. Is it a gMock bug?
...@@ -388,8 +388,8 @@ doesn't say what the return value should be. You need `DoAll()` to chain a ...@@ -388,8 +388,8 @@ doesn't say what the return value should be. You need `DoAll()` to chain a
`SetArgPointee()` with a `Return()` that provides a value appropriate to the API `SetArgPointee()` with a `Return()` that provides a value appropriate to the API
being mocked. being mocked.
See this [recipe](cook_book.md#mocking-side-effects) for more details and an See this [recipe](gmock_cook_book.md#mocking-side-effects) for more details and
example. an example.
### I have a huge mock class, and Microsoft Visual C++ runs out of memory when compiling it. What can I do? ### I have a huge mock class, and Microsoft Visual C++ runs out of memory when compiling it. What can I do?
......
...@@ -150,7 +150,7 @@ follow: ...@@ -150,7 +150,7 @@ follow:
* Derive a class `MockTurtle` from `Turtle`. * Derive a class `MockTurtle` from `Turtle`.
* Take a *virtual* function of `Turtle` (while it's possible to * Take a *virtual* function of `Turtle` (while it's possible to
[mock non-virtual methods using templates](cook_book.md#MockingNonVirtualMethods), [mock non-virtual methods using templates](gmock_cook_book.md#MockingNonVirtualMethods),
it's much more involved). it's much more involved).
* In the `public:` section of the child class, write `MOCK_METHOD();` * In the `public:` section of the child class, write `MOCK_METHOD();`
* Now comes the fun part: you take the function signature, cut-and-paste it * Now comes the fun part: you take the function signature, cut-and-paste it
...@@ -376,8 +376,8 @@ convenient way of saying "any value". ...@@ -376,8 +376,8 @@ convenient way of saying "any value".
In the above examples, `100` and `50` are also matchers; implicitly, they are In the above examples, `100` and `50` are also matchers; implicitly, they are
the same as `Eq(100)` and `Eq(50)`, which specify that the argument must be the same as `Eq(100)` and `Eq(50)`, which specify that the argument must be
equal (using `operator==`) to the matcher argument. There are many equal (using `operator==`) to the matcher argument. There are many
[built-in matchers](cheat_sheet.md#MatcherList) for common types (as well as [built-in matchers](gmock_cheat_sheet.md#MatcherList) for common types (as well
[custom matchers](cook_book.md#NewMatchers)); for example: as [custom matchers](gmock_cook_book.md#NewMatchers)); for example:
```cpp ```cpp
using ::testing::Ge; using ::testing::Ge;
...@@ -399,7 +399,7 @@ EXPECT_CALL(turtle, GoTo); ...@@ -399,7 +399,7 @@ EXPECT_CALL(turtle, GoTo);
This works for all non-overloaded methods; if a method is overloaded, you need This works for all non-overloaded methods; if a method is overloaded, you need
to help gMock resolve which overload is expected by specifying the number of to help gMock resolve which overload is expected by specifying the number of
arguments and possibly also the arguments and possibly also the
[types of the arguments](cook_book.md#SelectOverload). [types of the arguments](gmock_cook_book.md#SelectOverload).
### Cardinalities: How Many Times Will It Be Called? ### Cardinalities: How Many Times Will It Be Called?
...@@ -416,7 +416,7 @@ called. ...@@ -416,7 +416,7 @@ called.
We've seen `AtLeast(n)` as an example of fuzzy cardinalities earlier. For the We've seen `AtLeast(n)` as an example of fuzzy cardinalities earlier. For the
list of built-in cardinalities you can use, see list of built-in cardinalities you can use, see
[here](cheat_sheet.md#CardinalityList). [here](gmock_cheat_sheet.md#CardinalityList).
The `Times()` clause can be omitted. **If you omit `Times()`, gMock will infer The `Times()` clause can be omitted. **If you omit `Times()`, gMock will infer
the cardinality for you.** The rules are easy to remember: the cardinality for you.** The rules are easy to remember:
...@@ -485,7 +485,7 @@ the *default* action for the function every time (unless, of course, you have a ...@@ -485,7 +485,7 @@ the *default* action for the function every time (unless, of course, you have a
What can we do inside `WillOnce()` besides `Return()`? You can return a What can we do inside `WillOnce()` besides `Return()`? You can return a
reference using `ReturnRef(*variable*)`, or invoke a pre-defined function, among reference using `ReturnRef(*variable*)`, or invoke a pre-defined function, among
[others](cook_book.md#using-actions). [others](gmock_cook_book.md#using-actions).
**Important note:** The `EXPECT_CALL()` statement evaluates the action clause **Important note:** The `EXPECT_CALL()` statement evaluates the action clause
only once, even though the action may be performed many times. Therefore you only once, even though the action may be performed many times. Therefore you
...@@ -563,7 +563,7 @@ overloaded). This makes any calls to the method expected. This is not necessary ...@@ -563,7 +563,7 @@ overloaded). This makes any calls to the method expected. This is not necessary
for methods that are not mentioned at all (these are "uninteresting"), but is for methods that are not mentioned at all (these are "uninteresting"), but is
useful for methods that have some expectations, but for which other calls are useful for methods that have some expectations, but for which other calls are
ok. See ok. See
[Understanding Uninteresting vs Unexpected Calls](cook_book.md#uninteresting-vs-unexpected). [Understanding Uninteresting vs Unexpected Calls](gmock_cook_book.md#uninteresting-vs-unexpected).
### Ordered vs Unordered Calls {#OrderedCalls} ### Ordered vs Unordered Calls {#OrderedCalls}
...@@ -600,7 +600,7 @@ order as written. If a call is made out-of-order, it will be an error. ...@@ -600,7 +600,7 @@ order as written. If a call is made out-of-order, it will be an error.
(What if you care about the relative order of some of the calls, but not all of (What if you care about the relative order of some of the calls, but not all of
them? Can you specify an arbitrary partial order? The answer is ... yes! The them? Can you specify an arbitrary partial order? The answer is ... yes! The
details can be found [here](cook_book.md#OrderedCalls).) details can be found [here](gmock_cook_book.md#OrderedCalls).)
### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations} ### All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations}
...@@ -699,4 +699,4 @@ For example, in some tests we may not care about how many times `GetX()` and ...@@ -699,4 +699,4 @@ For example, in some tests we may not care about how many times `GetX()` and
In gMock, if you are not interested in a method, just don't say anything about In gMock, if you are not interested in a method, just don't say anything about
it. If a call to this method occurs, you'll see a warning in the test output, it. If a call to this method occurs, you'll see a warning in the test output,
but it won't be a failure. This is called "naggy" behavior; to change, see but it won't be a failure. This is called "naggy" behavior; to change, see
[The Nice, the Strict, and the Naggy](cook_book.md#NiceStrictNaggy). [The Nice, the Strict, and the Naggy](gmock_cook_book.md#NiceStrictNaggy).
...@@ -166,10 +166,11 @@ exp ::= simple_expression_in_Python_syntax ...@@ -166,10 +166,11 @@ exp ::= simple_expression_in_Python_syntax
## Code ## Code
You can find the source code of Pump in [scripts/pump.py](../scripts/pump.py). You can find the source code of Pump in
It is still very unpolished and lacks automated tests, although it has been [googlemock/scripts/pump.py](../googlemock/scripts/pump.py). It is still very
successfully used many times. If you find a chance to use it in your project, unpolished and lacks automated tests, although it has been successfully used
please let us know what you think! We also welcome help on improving Pump. many times. If you find a chance to use it in your project, please let us know
what you think! We also welcome help on improving Pump.
## Real Examples ## Real Examples
......
# Content Moved
We are working on updates to the GoogleTest documentation, which has moved to
the top-level [docs](../../docs) directory.
# How to Contribute
googletest development is Piper-First. Just create a regular Piper CL. When the
CL is accepted and submitted, it will make its way to OSS via regular releasing
process.
This diff is collapsed.
# googletest gMock Users Guide
go/gmockguide
Welcome to googletest: Google's C++ testing and mocking framework. gMock is a
mocking part of googletest.
* [OSS Version](https://github.com/google/googletest)
* [Google3](http://google3/third_party/googletest/)
* If you are new to gMock, start with [*gMock for Dummies*](for_dummies.md) to
learn the basic usage.
* Read [gMock Cookbook](cook_book.md) to learn more advanced usage and useful
tips.
* For a quick reference, check out [gMock Cheat Sheet](cheat_sheet.md).
* If you have questions, search [gMock FAQ](#GMockFaq) and the gmock-users@
archive before sending them to gmock-users@.
<!-- GOOGLETEST_CM0035 DO NOT DELETE -->
<!--#include file="for_dummies.md"-->
#### Side Effects
<!-- mdformat off(github rendering does not support multiline tables) -->
| Matcher | Description |
| :--------------------------------- | :-------------------------------------- |
| `Assign(&variable, value)` | Assign `value` to variable. |
| `DeleteArg<N>()` | Delete the `N`-th (0-based) argument, which must be a pointer. |
| `SaveArg<N>(pointer)` | Save the `N`-th (0-based) argument to `*pointer`. |
| `SaveArgPointee<N>(pointer)` | Save the value pointed to by the `N`-th (0-based) argument to `*pointer`. |
| `SetArgReferee<N>(value)` | Assign `value` to the variable referenced by the `N`-th (0-based) argument. |
| `SetArgPointee<N>(value)` | Assign `value` to the variable pointed by the `N`-th (0-based) argument. |
| `SetArgumentPointee<N>(value)` | Same as `SetArgPointee<N>(value)`. Deprecated. Will be removed in v1.7.0. |
| `SetArrayArgument<N>(first, last)` | Copies the elements in source range [`first`, `last`) to the array pointed to by the `N`-th (0-based) argument, which can be either a pointer or an iterator. The action does not take ownership of the elements in the source range. |
| `SetErrnoAndReturn(error, value)` | Set `errno` to `error` and return `value`. |
| `Throw(exception)` | Throws the given exception, which can be any copyable value. Available since v1.1.0. |
<!-- mdformat on -->
* When compiling with exceptions in google3, it's not enough to specify
`-fexceptions` to copts in your cc_test target. That flag will not be
inherited by gmock, and various headers will be compiled both with and
without `-fexceptions` causing subtle bugs. Instead you must pass
`--copt=-fexceptions` to the blaze command so the flag gets passed to all
targets... but this is Google and we don't use exceptions so it shouldn't
really be an issue.
# googletest Home
go/gmock
Googletest is Google's C++ testing and mocking framework. Please note that there
are legacy names you may encounter "gUnit" and "gMock" - these names are now
merged into "googletest"
<!-- GOOGLETEST_CM0035 DO NOT DELETE -->
## Testimonials
> "I'm really enjoying trying it, and it's amazing to me how far you've taken
> this in C++. It's changed the way I program (and therefore changed my life ;),
> and one of my teams has adopted it for all/most tests (and I'm working on the
> other)." \
> -- *Derek Thomson*, Google Mountain View
<section></section>
> "I started using mocks with EasyMock in Java a few years ago and found them
> **invaluable** for making unit testing as painless and effective as possible.
> I'm very glad (and amazed) to see you've managed to create something similar
> for C++. It's making the transition much more pleasant." \
> -- *David Harkness*, Google Mountain View
<section></section>
> "I #included `gmock.h` and lived to tell the tale... Kept me from having to
> depend on `MockBigtable` thus far, which is **huge**." \
> -- *Matthew Simmons*, Google NYC
<section></section>
> "I like the approach of `EXPECT_CALL` much more than EasyMock's mock modes
> (record, replay). It's the best way to ensure the user will never forget to
> verify the expectations: do it automatically!" \
> -- *Tiago Silverira*, Google Brazil
<section></section>
> "It's by far the best mocking library for C++, by a long-shot." \
> -- *Joe Walnes*, co-creator of jMock, Google London
## Learning googletest mocking
Please see the [*googletest Users Guide*](guide.md) for the combined gMock
mocking documentation.
## Resources for Users
* More docs:
* [Interview with gMock's Creator](http://www.corp.google.com/eng/testing/codegreen/v10/gMock.htm)
on the
[Feb 2008](http://www.corp.google.com/eng/testing/codegreen/v10/index.htm)
issue of [Code Green](http://go/codegreen) - discusses gMock's history
and philosophy.
* "Mockers of the (C++) world, delight!": TotT
[episode 66](http://big.corp.google.com/~jmcmaster/testing/2007/12/episode-68-mockers-of-c-world-delight.html) -
quick intro on gMock's benefits and usage
* "Mock logs better than gold": TotT
[episode 76](http://big.corp.google.com/~jmcmaster/testing/2008/02/episode-76-mock-logs-better-than-gold_21.html) -
how to test LOGs using gMock
* "Testing legacy code gently": TotT
[episode 84](http://big.corp.google.com/~jmcmaster/testing/2008/04/episode-84-testing-legacy-code-gently.html) -
using mock callbacks to test legacy code without a big refactoring
* "Literate testing with matchers": TotT
[episode 135](http://big.corp.google.com/~jmcmaster/testing/2009/06/episode-135-literate-testing-with_08.html) -
using matchers to get readable test code and readable test messages
* "Making a perfect matcher": TotT
[episode 139](http://big.corp.google.com/~jmcmaster/testing/2009/07/episode-139-making-perfect-matcher.html) -
defining custom matchers easily
* Talks
* "Declarative C++ Testing Using DSLs" talk (6/4/2008):
[abstract](https://wiki.corp.google.com/twiki/bin/view/Main/WanTalks#Declarative_C_Testing_Using_DSLs),
[slides](http://wiki.corp.google.com/twiki/pub/Main/WanTalks/0806-declarative-cpp-testing.xul#Eva)
(requires Firefox) - gMock's design and implementation tricks
* "Mocks made easy in C++ and Java" talk (4/23/2008):
[slides](http://go/MockTalk),
[fish](http://fish.corp.google.com/talks/8729/)
* "C++ mocks made easy - an introduction to gMock" talk (1/22/2008)):
[slides](http://wiki.corp.google.com/twiki/pub/Main/WanTalks/0801-mv-gmock.xul#eva)
(requires Firefox),
[video](https://video.google.com/a/google.com/?AuthEventSource=SSO#/Play/contentId=bd07003d4193a646)
* "A preview to gMock" talk (6/28/2007):
[PowerPoint slides](http://wiki.corp.google.com/twiki/pub/Main/WanTalks/0706-beijing-gmock-preview.ppt)
* Tools
* `/google/src/head/depot/google3/third_party/googletest/googlemock/scripts/generator/gmock_gen.py
*your.h ClassNames*` generates mocks for the given base classes (if no
class name is given, all classes in the file are emitted).
* Mocks
* [mock-log.h](http://s/?fileprint=//depot/google3/testing/base/public/mock-log.h) -
a sample on using gMock to create a mock class
* [gmock-sample-mock-log.cc](http://s/?fileprint=//depot/google3/testing/base/internal/gmock-sample-mock-log.cc) -
a sample on using gMock to test LOG()s
* Folks
* Meet the
[users](http://piano.kir.corp.google.com:8080/lica/?e=use%3Agmock).
* `gmock-users` list:
[subscribe](https://groups.google.com/a/google.com/group/gmock-users/topics),
[archive](https://groups.google.com/a/google.com/group/gmock-users/topics),
[smile!](http://piano.kir.corp.google.com:8080/lica/?e=gmock-users) Send
questions here if you still need help after consulting the on-line docs.
* `gmock-announce` list:
[subscribe](https://groups.google.com/a/google.com/group/gmock-announce/topics)
to this instead of `gmock-users` if you are interested in announcements
only.
## Resources for Contributors
* [Dashboard](http://unittest.corp.google.com/project/gunit-gmock/)
* [*gMock Design*](design.md) (go/gmockdesign) - the design doc
* `c-mock-dev` list (deprecated) -
[old archive](https://mailman.corp.google.com/pipermail/c/c-mock-dev/),
[new archive](https://g.corp.google.com/group/c-mock-dev-archive)
* `opensource-gmock` list - discussions on the development of gMock:
[subscribe](https://groups.google.com/a/google.com/group/opensource-gmock/subscribe),
[archive](https://g.corp.google.com/group/opensource-gmock-archive),
[smile!](http://piano.kir.corp.google.com:8080/lica/?e=opensource-gmock)
## Acknowledgments
We'd like to thank the following people for their contribution to gMock: Piotr
Kaminski, Jeffrey Yasskin (who/jyasskin), Joe Walnes, Bradford Cross, Keith Ray,
Craig Silverstein, Matthew Simmons (who/simmonmt), Hal Burch (who/hburch), Russ
Rufer, Rushabh Doshi (who/rdoshi), Gene Volovich (who/genev), Mike Bland, Neal
Norwitz (who/nnorwitz), Mark Zuber, Vadim Berman (who/vadimb).
# GMock
<!-- GOOGLETEST_CM0035 DO NOT DELETE -->
## What is gMock?
gMock is Google's framework for creating and using C++ mock classes. It helps
you design better systems and write better tests. A mock object is an object
that you use in a test instead of a real object. A mock object implements the
same interface as a real object but lets you specify at run time how the object
will be used. When you write tests that use a mock, you define expectations
about how the mock's methods will be called. Your test then verifies how your
real code behaves when interacting with the mock. See the
[Mock Objects Best Practices Guide](http://go/mock-objects#mocks-stubs-fakes)
for a comparison of mocks with stubs, fakes, and other kinds of test doubles.
For example, gMock provides a simple syntax for declaring "I expect the
RetryQuery method on this mock object to be called three times in the course of
this test". Your test will fail if the expectation isn't met.
The gMock library provides a mock framework for C++ similar to jMock or
EasyMock[?](http://go/easymock-codelab) for Java. In gMock you use macros to
define methods for your mock objects and set expectations for those methods.
gMock runs on Linux, Windows, and Mac OS X.
## What is gMock good for?
Mocks in general are good for:
- prototyping and designing new code and APIs.
- removing unnecessary, expensive, or unreliable dependencies from your tests.
gMock in particular is good for writing quality C++ mocks. Without the help of a
mocking framework like gMock, good C++ mocks are hard to create.
## What is gMock NOT good for?
gMock is not good for testing the behavior of dependencies. The point of testing
with mocks is to test the classes that use the mocks, not the mocks themselves.
Objects that have working toy implementations are called fakes instead of mocks.
For example, you could use an in-memory file system to fake disk operations.
Mocks aren't useful for very simple classes like
[Dumb Data Objects](http://big.corp.google.com/~jmcmaster/testing/2011/04/episode-220-blast-from-tott-past-dont.html).
If it's more trouble to use a mock than the real class, just use the real class.
## Who uses gMock?
There are over 30K tests using gmock. Virtually every C++ test at Google that
needs a mock object uses gMock.
## Practical matters
gMock is bundled with [gUnit](/third_party/googletest/googletest/docs/). To use
gMock,
[include a dependency](/third_party/googletest/googletest/docs/howto_cpp#LinuxTarget)
on `//testing/base/public:gunit` in the BUILD rule for your mocks, and use the
following include statement in the file that defines your mock class:
```
#include "gmock/gmock.h"
```
&nbsp; | &nbsp;
--------------------------- | ------------------------------------------
**Implementation language** | C++
**Code location** | google3/third_party/googletest/googlemock/
**Build target** | //testing/base/public:gunit
## Best practices
Use [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) to
enable easy mocking. If you define dependencies as interfaces rather than
concrete classes, you can swap out the production version of a class for a mock
during testing.
You can also use gMock during the design phase for your system. By sketching
your architecture using mocks rather than full implementations, you can evolve
your design more quickly.
## History and evolution
In January 2007 Zhanyong Wan and the Testing Technology team met with
experienced C++ engineers to find out about C++ testing needs. The team learned
that creating mocks in C++ was a major pain point. They looked around for
existing frameworks but didn't find anything satisfactory. So Zhanyong Wan
tackled the problem of creating a usable C++ mocking framework.
C++ posed a unique problem for mocking: while
[reflection](http://en.wikipedia.org/wiki/Reflection_\(computer_programming\))
in Java and Python make it easy to generate a mock implementation of any
interface, C++ does not have reflection. Wan hit on macros as a way to simplify
mock writing in C++, and gMock was born.
## Who to contact
- g/gmock-users
- g/gmock-announce
## Additional resources
- [gMock](http://go/gmock) - homepage
- [GMock for Dummies](http://<!-- GOOGLETEST_CM0013 DO NOT DELETE -->) - gets you started with gMock
quickly
- [GMock Cookbook](http://<!-- GOOGLETEST_CM0012 DO NOT DELETE -->) - recipes for common scenarios; covers
advanced usage.
- [GMock Cheat Sheet](http://<!-- GOOGLETEST_CM0020 DO NOT DELETE -->) - a quick reference
- [GMock FAQ](http://<!-- GOOGLETEST_CM0021 DO NOT DELETE -->) - frequently asked questions
- [gUnit GDH page](http://go/gunit-overview)
- [gUnit User's Guide](http://goto.corp.google.com/gunit) - gets you started
with gUnit, which is closely related to gMock
Googletest Mocking (gMock)
* [Home](index.md)
* [Overview](overview.md)
* [User's Guide](guide.md)
* [gMock For Dummies](for_dummies.md)
* [gMock Cookbook](cook_book.md)
* [gMock Cheat Sheet](cheat_sheet.md)
* [Design](design.md)
* [How To Contribute](contribute.md)
* [gMock FAQ](gmock_faq.md)
**WARNING:** This document was recently migrated from
[Goowiki](http://wtf/goowiki) (b/35424903) and may still require additional
updates or formatting. You can still access the original document on Goowiki
until the cleanup is complete:
## BUILD Rule
Add *one* of the following to your `deps`:
```build
"//testing/base/public:gunit",
"//testing/base/public:gtest_main",
```
Add this to your `.cc` file:
```cpp
#include "gmock/gmock.h"
```
Unless noted, *all functions and classes* are defined in the `::testing`
namespace.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment