Skip to content

Commit 4962080

Browse files
committed
update recap sidebar
1 parent 22ae3c9 commit 4962080

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

chapter_19_mocking.asciidoc

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,48 +2169,52 @@ In the meantime, do a commit and enjoy this recap:
21692169
*******************************************************************************
21702170
21712171
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")))
21792179
21802180
21812181
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")))
21862187
21872188
21882189
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.
21932193
It contains most everything you might need for mocking in Python.
2194+
((("mocks", "Python Mock library")))
2195+
((("Python 3", "Mock library")))
21942196
21952197
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.
22022205
22032206
22042207
Mocks can leave you tightly coupled to the implementation::
22052208
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.
22082211
22092212
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")))
22142218
22152219
There's lots more discussion of the pros and cons of mocks
22162220
<<chapter_purist_unit_tests,coming up soon>>. Read on!

0 commit comments

Comments
 (0)