METADATA 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. Metadata-Version: 2.1
  2. Name: pytest-mock
  3. Version: 1.10.0
  4. Summary: Thin-wrapper around the mock package for easier use with py.test
  5. Home-page: https://github.com/pytest-dev/pytest-mock/
  6. Author: Bruno Oliveira
  7. Author-email: nicoddemus@gmail.com
  8. License: MIT
  9. Keywords: pytest mock
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Framework :: Pytest
  13. Classifier: Intended Audience :: Developers
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Operating System :: OS Independent
  16. Classifier: Programming Language :: Python :: 2
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3.4
  20. Classifier: Programming Language :: Python :: 3.5
  21. Classifier: Programming Language :: Python :: 3.6
  22. Classifier: Topic :: Software Development :: Testing
  23. Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
  24. Requires-Dist: pytest (>=2.7)
  25. Requires-Dist: mock; python_version < "3.0"
  26. ===========
  27. pytest-mock
  28. ===========
  29. This plugin installs a ``mocker`` fixture which is a thin-wrapper around the patching API
  30. provided by the `mock package <http://pypi.python.org/pypi/mock>`_,
  31. but with the benefit of not having to worry about undoing patches at the end
  32. of a test:
  33. .. code-block:: python
  34. import os
  35. class UnixFS:
  36. @staticmethod
  37. def rm(filename):
  38. os.remove(filename)
  39. def test_unix_fs(mocker):
  40. mocker.patch('os.remove')
  41. UnixFS.rm('file')
  42. os.remove.assert_called_once_with('file')
  43. .. Using PNG badges because PyPI doesn't support SVG
  44. |python| |version| |anaconda| |ci| |appveyor| |coverage|
  45. .. |version| image:: http://img.shields.io/pypi/v/pytest-mock.svg
  46. :target: https://pypi.python.org/pypi/pytest-mock
  47. .. |anaconda| image:: https://anaconda.org/conda-forge/pytest-mock/badges/version.svg
  48. :target: https://anaconda.org/conda-forge/pytest-mock
  49. .. |ci| image:: http://img.shields.io/travis/pytest-dev/pytest-mock.svg
  50. :target: https://travis-ci.org/pytest-dev/pytest-mock
  51. .. |appveyor| image:: https://ci.appveyor.com/api/projects/status/pid1t7iuwhkm9eh6/branch/master?svg=true
  52. :target: https://ci.appveyor.com/project/pytestbot/pytest-mock
  53. .. |coverage| image:: http://img.shields.io/coveralls/pytest-dev/pytest-mock.svg
  54. :target: https://coveralls.io/r/pytest-dev/pytest-mock
  55. .. |python| image:: https://img.shields.io/pypi/pyversions/pytest-mock.svg
  56. :target: https://pypi.python.org/pypi/pytest-mock/
  57. .. image:: http://www.opensourcecitizen.org/badge?url=github.com/pytest-dev/pytest-mock
  58. :target: http://www.opensourcecitizen.org/project?url=github.com/pytest-dev/pytest-mock
  59. If you found this library useful, donate some CPU cycles to its
  60. development efforts by clicking above. Thank you! 😇
  61. Usage
  62. =====
  63. The ``mocker`` fixture has the same API as
  64. `mock.patch <http://www.voidspace.org.uk/python/mock/patch.html#patch-decorators>`_,
  65. supporting the same arguments:
  66. .. code-block:: python
  67. def test_foo(mocker):
  68. # all valid calls
  69. mocker.patch('os.remove')
  70. mocker.patch.object(os, 'listdir', autospec=True)
  71. mocked_isfile = mocker.patch('os.path.isfile')
  72. The supported methods are:
  73. * ``mocker.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
  74. * ``mocker.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
  75. * ``mocker.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
  76. * ``mocker.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict.
  77. * ``mocker.stopall()``: stops all active patches up to this point.
  78. * ``mocker.resetall()``: calls ``reset_mock()`` in all mocked objects up to this point.
  79. Some objects from the ``mock`` module are accessible directly from ``mocker`` for convenience:
  80. * `Mock <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock>`_
  81. * `MagicMock <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock>`_
  82. * `PropertyMock <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock>`_
  83. * `ANY <https://docs.python.org/3/library/unittest.mock.html#any>`_
  84. * `DEFAULT <https://docs.python.org/3/library/unittest.mock.html#default>`_ *(Version 1.4)*
  85. * `call <https://docs.python.org/3/library/unittest.mock.html#call>`_ *(Version 1.1)*
  86. * `sentinel <https://docs.python.org/3/library/unittest.mock.html#sentinel>`_ *(Version 1.2)*
  87. * `mock_open <https://docs.python.org/3/library/unittest.mock.html#mock-open>`_
  88. Spy
  89. ---
  90. The spy acts exactly like the original method in all cases, except it allows use of `mock`
  91. features with it, like retrieving call count. It also works for class and static methods.
  92. .. code-block:: python
  93. def test_spy(mocker):
  94. class Foo(object):
  95. def bar(self):
  96. return 42
  97. foo = Foo()
  98. mocker.spy(foo, 'bar')
  99. assert foo.bar() == 42
  100. assert foo.bar.call_count == 1
  101. Stub
  102. ----
  103. The stub is a mock object that accepts any arguments and is useful to test callbacks, for instance.
  104. May be passed a name to be used by the constructed stub object in its repr (useful for debugging).
  105. .. code-block:: python
  106. def test_stub(mocker):
  107. def foo(on_something):
  108. on_something('foo', 'bar')
  109. stub = mocker.stub(name='on_something_stub')
  110. foo(stub)
  111. stub.assert_called_once_with('foo', 'bar')
  112. Improved reporting of mock call assertion errors
  113. ------------------------------------------------
  114. This plugin monkeypatches the mock library to improve pytest output for failures
  115. of mock call assertions like ``Mock.assert_called_with()`` by hiding internal traceback
  116. entries from the ``mock`` module.
  117. It also adds introspection information on differing call arguments when
  118. calling the helper methods. This features catches `AssertionError` raised in
  119. the method, and uses py.test's own `advanced assertions`_ to return a better
  120. diff::
  121. mocker = <pytest_mock.MockFixture object at 0x0381E2D0>
  122. def test(mocker):
  123. m = mocker.Mock()
  124. m('fo')
  125. > m.assert_called_once_with('', bar=4)
  126. E AssertionError: Expected call: mock('', bar=4)
  127. E Actual call: mock('fo')
  128. E
  129. E pytest introspection follows:
  130. E
  131. E Args:
  132. E assert ('fo',) == ('',)
  133. E At index 0 diff: 'fo' != ''
  134. E Use -v to get the full diff
  135. E Kwargs:
  136. E assert {} == {'bar': 4}
  137. E Right contains more items:
  138. E {'bar': 4}
  139. E Use -v to get the full diff
  140. test_foo.py:6: AssertionError
  141. ========================== 1 failed in 0.03 seconds ===========================
  142. This is useful when asserting mock calls with many/nested arguments and trying
  143. to quickly see the difference.
  144. This feature is probably safe, but if you encounter any problems it can be disabled in
  145. your ``pytest.ini`` file:
  146. .. code-block:: ini
  147. [pytest]
  148. mock_traceback_monkeypatch = false
  149. Note that this feature is automatically disabled with the ``--tb=native`` option. The underlying
  150. mechanism used to suppress traceback entries from ``mock`` module does not work with that option
  151. anyway plus it generates confusing messages on Python 3.5 due to exception chaining
  152. .. _advanced assertions: http://pytest.org/latest/assert.html
  153. Use standalone "mock" package
  154. -----------------------------
  155. *New in version 1.4.0.*
  156. Python 3 users might want to use a newest version of the ``mock`` package as published on PyPI
  157. than the one that comes with the Python distribution.
  158. .. code-block:: ini
  159. [pytest]
  160. mock_use_standalone_module = true
  161. This will force the plugin to import ``mock`` instead of the ``unittest.mock`` module bundled with
  162. Python 3.4+. Note that this option is only used in Python 3+, as Python 2 users only have the option
  163. to use the ``mock`` package from PyPI anyway.
  164. Requirements
  165. ============
  166. * Python 2.7, Python 3.4+
  167. * pytest
  168. * mock (for Python 2)
  169. Install
  170. =======
  171. Install using `pip <http://pip-installer.org/>`_:
  172. .. code-block:: console
  173. $ pip install pytest-mock
  174. Changelog
  175. =========
  176. Please consult the `changelog page`_.
  177. .. _changelog page: https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst
  178. Why bother with a plugin?
  179. =========================
  180. There are a number of different ``patch`` usages in the standard ``mock`` API,
  181. but IMHO they don't scale very well when you have more than one or two
  182. patches to apply.
  183. It may lead to an excessive nesting of ``with`` statements, breaking the flow
  184. of the test:
  185. .. code-block:: python
  186. import mock
  187. def test_unix_fs():
  188. with mock.patch('os.remove'):
  189. UnixFS.rm('file')
  190. os.remove.assert_called_once_with('file')
  191. with mock.patch('os.listdir'):
  192. assert UnixFS.ls('dir') == expected
  193. # ...
  194. with mock.patch('shutil.copy'):
  195. UnixFS.cp('src', 'dst')
  196. # ...
  197. One can use ``patch`` as a decorator to improve the flow of the test:
  198. .. code-block:: python
  199. @mock.patch('os.remove')
  200. @mock.patch('os.listdir')
  201. @mock.patch('shutil.copy')
  202. def test_unix_fs(mocked_copy, mocked_listdir, mocked_remove):
  203. UnixFS.rm('file')
  204. os.remove.assert_called_once_with('file')
  205. assert UnixFS.ls('dir') == expected
  206. # ...
  207. UnixFS.cp('src', 'dst')
  208. # ...
  209. But this poses a few disadvantages:
  210. - test functions must receive the mock objects as parameter, even if you don't plan to
  211. access them directly; also, order depends on the order of the decorated ``patch``
  212. functions;
  213. - receiving the mocks as parameters doesn't mix nicely with pytest's approach of
  214. naming fixtures as parameters, or ``pytest.mark.parametrize``;
  215. - you can't easily undo the mocking during the test execution;
  216. **Note about usage as context manager**
  217. Although mocker's API is intentionally the same as ``mock.patch``'s, its use
  218. as context manager and function decorator is **not** supported through the
  219. fixture. The purpose of this plugin is to make the use of context managers and
  220. function decorators for mocking unnecessary. Indeed, trying to use the
  221. functionality in ``mocker`` in this manner can lead to non-intuitive errors:
  222. .. code-block:: python
  223. def test_context_manager(mocker):
  224. a = A()
  225. with mocker.patch.object(a, 'doIt', return_value=True, autospec=True):
  226. assert a.doIt() == True
  227. .. code-block:: console
  228. ================================== FAILURES ===================================
  229. ____________________________ test_context_manager _____________________________
  230. in test_context_manager
  231. with mocker.patch.object(a, 'doIt', return_value=True, autospec=True):
  232. E AttributeError: __exit__
  233. You can however use ``mocker.mock_module`` to access the underlying ``mock``
  234. module, e.g. to return a context manager in a fixture that mocks something
  235. temporarily:
  236. .. code-block:: python
  237. @pytest.fixture
  238. def fixture_cm(mocker):
  239. @contextlib.contextmanager
  240. def my_cm():
  241. def mocked():
  242. pass
  243. with mocker.mock_module.patch.object(SomeClass, 'method', mocked):
  244. yield
  245. return my_cm
  246. License
  247. =======
  248. Distributed under the terms of the `MIT`_ license.
  249. .. _MIT: https://github.com/pytest-dev/pytest-mock/blob/master/LICENSE