@@ -2169,48 +2169,52 @@ In the meantime, do a commit and enjoy this recap:
2169
2169
*******************************************************************************
2170
2170
2171
2171
Mocking and external dependencies::
2172
- (((" external dependencies")))We
2173
- use mocking in unit tests when we have an external dependency that we
2174
- don't want to actually use in our tests. A mock is used to simulate the
2175
- third-party API. Whilst it is possible to "roll your own" mocks in
2176
- Python, a mocking framework like the +mock+ module provides a lot of helpful
2177
- shortcuts which will make it easier to write (and more importantly, read)
2178
- your tests.
2172
+ We use mocking in unit tests when we have an external dependency
2173
+ that we don't want to actually use in our tests.
2174
+ A mock is used to simulate the third-party API.
2175
+ Whilst it is possible to "roll your own" mocks in Python,
2176
+ a mocking framework like the +unittest. mock+ module provides a lot of helpful shortcuts
2177
+ which will make it easier to write (and more importantly, read) your tests.
2178
+ ((("external dependencies")))
2179
2179
2180
2180
2181
2181
Monkeypatching::
2182
- ((("monkeypatching")))Replacing
2183
- an object in a namespace at runtime. We use it in our unit
2184
- tests to replace a real function which has undesirable side effects with a
2185
- mock object, using the `patch` decorator.
2182
+ Replacing an object in a namespace at runtime.
2183
+ We use it in our unit tests to replace a real function
2184
+ which has undesirable side effects
2185
+ with a mock object, using the `mock.patch` decorator.
2186
+ ((("monkeypatching")))
2186
2187
2187
2188
2188
2189
The Mock library::
2189
- ((("mocks", "Python Mock library")))((("Python 3", "Mock library")))Michael
2190
- Foord (who used to work for the company that spawned
2191
- PythonAnywhere, just before I joined) wrote the excellent "Mock"
2192
- library that's now been integrated into the standard library of Python 3.
2190
+ Michael Foord (who used to work for the company that spawned PythonAnywhere,
2191
+ just before I joined) wrote the excellent "Mock" library
2192
+ that's now been integrated into the standard library of Python 3.
2193
2193
It contains most everything you might need for mocking in Python.
2194
+ ((("mocks", "Python Mock library")))
2195
+ ((("Python 3", "Mock library")))
2194
2196
2195
2197
2196
- The patch decorator::
2197
- `unittest.mock` ((("patch decorator")))provides
2198
- a function called `patch`, which can be used
2199
- to "mock out" any object from the module you're testing. It's commonly
2200
- used as a decorator on a test method, or even at the class level, where
2201
- it's applied to all the test methods of that class.
2198
+ The mock.patch decorator::
2199
+ `unittest.mock` ((("patch decorator")))provides a function called `patch`,
2200
+ which can be used to "mock out" (monkeypatch)
2201
+ any object from the module you're testing.
2202
+ It's commonly used as a decorator on a test method.
2203
+ Importantly, it "undoes" the mocking at the end of the test for you,
2204
+ to avoid contamination between tests.
2202
2205
2203
2206
2204
2207
Mocks can leave you tightly coupled to the implementation::
2205
2208
As we saw in <<mocks-tightly-coupled-sidebar>>,
2206
- mocks can leave you tightly coupled to your implementation. For that
2207
- reason, you shouldn't use them unless you have a good reason.
2209
+ mocks can leave you tightly coupled to your implementation.
2210
+ For that reason, you shouldn't use them unless you have a good reason.
2208
2211
2209
2212
Mocks can save you from duplication in your tests::
2210
- ((("mocks", "reducing duplication with")))((("duplication, eliminating")))On
2211
- the other hand, there's no point in duplicating all of your tests
2212
- for a function inside a higher-level piece of code that uses that
2213
- function. Using a mock in this case reduces duplication.
2213
+ With that said, there is an argument for using mocks to remove duplication;
2214
+ used extensively, this approach leads to "London-style" TDD,
2215
+ a variation on the style I mostly follow and show in this book.
2216
+ ((("mocks", "reducing duplication with")))
2217
+ ((("duplication, eliminating")))
2214
2218
2215
2219
There's lots more discussion of the pros and cons of mocks
2216
2220
<<chapter_purist_unit_tests,coming up soon>>. Read on!
0 commit comments