Skip to content

Commit fbd1977

Browse files
committed
Merge pull request #16 from tigarmo/tigarmo-contextmanager-docs
Update README.rst with contextmanager warning
2 parents 0534047 + 9f7dc33 commit fbd1977

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ The supported methods are:
6060
* ``mocker.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict.
6161
* ``mocker.stopall()``: stops all active patches at this point.
6262

63+
Note that, although mocker's API is intentionally the same as ``mock.patch``'s, its uses as context managers and function decorators are **not** supported. The purpose of this plugin is to make the use of context managers and function decorators for mocking unnecessary. Indeed, trying to use the functionality in ``mocker`` in this manner can lead to non-intuitive errors:
64+
65+
.. code-block:: python
66+
67+
def test_context_manager(mocker):
68+
a = A()
69+
with mocker.patch.object(a, 'doIt', return_value=True, autospec=True):
70+
assert a.doIt() == True
71+
72+
.. code-block:: console
73+
74+
================================== FAILURES ===================================
75+
____________________________ test_context_manager _____________________________
76+
in test_context_manager
77+
with mocker.patch.object(a, 'doIt', return_value=True, autospec=True):
78+
E AttributeError: __exit__
79+
80+
6381
You can also access ``Mock`` and ``MagicMock`` directly using from ``mocker``
6482
fixture:
6583

0 commit comments

Comments
 (0)