From 0f28ba0e397962c5e704f3700fc5af6571802384 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 9 Oct 2015 23:24:37 +0200 Subject: [PATCH] import pytest-mock_0.8.1.orig.tar.gz --- LICENSE | 165 ++++++++++++++++++++++++++++++ MANIFEST.in | 2 + PKG-INFO | 239 ++++++++++++++++++++++++++++++++++++++++++++ README.rst | 218 ++++++++++++++++++++++++++++++++++++++++ pytest_mock.py | 128 ++++++++++++++++++++++++ setup.cfg | 8 ++ setup.py | 37 +++++++ test_pytest_mock.py | 233 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 1030 insertions(+) create mode 100644 LICENSE create mode 100644 MANIFEST.in create mode 100644 PKG-INFO create mode 100644 README.rst create mode 100644 pytest_mock.py create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 test_pytest_mock.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1dd90f2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ +GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..acfb84e --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +include LICENSE \ No newline at end of file diff --git a/PKG-INFO b/PKG-INFO new file mode 100644 index 0000000..47b3137 --- /dev/null +++ b/PKG-INFO @@ -0,0 +1,239 @@ +Metadata-Version: 1.1 +Name: pytest-mock +Version: 0.8.1 +Summary: Thin-wrapper around the mock package for easier use with py.test +Home-page: https://github.com/pytest-dev/pytest-mock/ +Author: Bruno Oliveira +Author-email: nicoddemus@gmail.com +License: LGPL +Description: =========== + pytest-mock + =========== + + This plugin installs a ``mocker`` fixture which is a thin-wrapper around the patching API + provided by the excellent `mock `_ package, + but with the benefit of not having to worry about undoing patches at the end + of a test: + + .. code-block:: python + + + def test_unix_fs(mocker): + mocker.patch('os.remove') + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + + .. Using PNG badges because PyPI doesn't support SVG + + |python| |version| |downloads| |ci| |coverage| + + .. |version| image:: http://img.shields.io/pypi/v/pytest-mock.png + :target: https://pypi.python.org/pypi/pytest-mock + + .. |downloads| image:: http://img.shields.io/pypi/dm/pytest-mock.png + :target: https://pypi.python.org/pypi/pytest-mock + + .. |ci| image:: http://img.shields.io/travis/pytest-dev/pytest-mock.png + :target: https://travis-ci.org/pytest-dev/pytest-mock + + .. |coverage| image:: http://img.shields.io/coveralls/pytest-dev/pytest-mock.png + :target: https://coveralls.io/r/pytest-dev/pytest-mock + + .. |python| image:: https://pypip.in/py_versions/pytest-mock/badge.png + :target: https://pypi.python.org/pypi/pytest-mock/ + :alt: Supported Python versions + + + Usage + ===== + + The ``mocker`` fixture has the same API as + `mock.patch `_, + supporting the same arguments: + + .. code-block:: python + + def test_foo(mocker): + # all valid calls + mocker.patch('os.remove') + mocker.patch.object(os, 'listdir', autospec=True) + mocked_isfile = mocker.patch('os.path.isfile') + + The supported methods are: + + * ``mocker.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch. + * ``mocker.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object. + * ``mocker.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple. + * ``mocker.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict. + * ``mocker.stopall()``: stops all active patches at this point. + + 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: + + .. code-block:: python + + def test_context_manager(mocker): + a = A() + with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): + assert a.doIt() == True + + .. code-block:: console + + ================================== FAILURES =================================== + ____________________________ test_context_manager _____________________________ + in test_context_manager + with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): + E AttributeError: __exit__ + + + You can also access ``Mock`` and ``MagicMock`` directly using from ``mocker`` + fixture: + + .. code-block:: python + + def test_feature(mocker): + ret = [mocker.Mock(return_value=True), mocker.Mock(return_value=True)] + mocker.patch('mylib.func', side_effect=ret) + + *New in version 0.5* + + Spy + --- + + *New in version 0.6* + + The spy acts exactly like the original method in all cases, except it allows use of `mock` + features with it, like retrieving call count. + + From version 0.7 onward it also works for class and static methods. Originally it was only safe to + use with instance methods. + + .. code-block:: python + + def test_spy(mocker): + class Foo(object): + def bar(self): + return 42 + + foo = Foo() + mocker.spy(foo, 'bar') + assert foo.bar() == 42 + assert foo.bar.call_count == 1 + + Stub + ---- + + *New in version 0.6* + + The stub is a mock object that accepts any arguments and is useful to test callbacks, for instance. + + .. code-block:: python + + def test_stub(mocker): + def foo(on_something): + on_something('foo', 'bar') + + stub = mocker.stub() + + foo(stub) + stub.assert_called_once_with('foo', 'bar') + + Note + ---- + + Prior to version ``0.4.0``, the ``mocker`` fixture was named ``mock``. + This was changed because naming the fixture ``mock`` conflicts with the + actual ``mock`` module, which made using it awkward when access to both the + module and the plugin were required within a test. + + The old fixture ``mock`` still works, but its use is discouraged and will be + removed in version ``1.0``. + + Requirements + ============ + + * Python 2.6+, Python 3.2+ + * pytest + * mock (for Python < 3.3) + + + Install + ======= + + Install using `pip `_: + + .. code-block:: console + + $ pip install pytest-mock + + Changelog + ========= + + Please consult `releases `_. + + Why bother with a plugin? + ========================= + + There are a number of different ``patch`` usages in the standard ``mock`` API, + but IMHO they don't scale very well when you have more than one or two + patches to apply. + + It may lead to an excessive nesting of ``with`` statements, breaking the flow + of the test: + + .. code-block:: python + + import mock + + def test_unix_fs(): + with mock.patch('os.remove'): + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + with mock.patch('os.listdir'): + assert UnixFS.ls('dir') == expected + # ... + + with mock.patch('shutil.copy'): + UnixFS.cp('src', 'dst') + # ... + + + One can use ``patch`` as a decorator to improve the flow of the test: + + .. code-block:: python + + @mock.patch('os.remove') + @mock.patch('os.listdir') + @mock.patch('shutil.copy') + def test_unix_fs(mocked_copy, mocked_listdir, mocked_remove): + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + assert UnixFS.ls('dir') == expected + # ... + + UnixFS.cp('src', 'dst') + # ... + + But this poses a few disadvantages: + + - test functions must receive the mock objects as parameter, even if you don't plan to + access them directly; also, order depends on the order of the decorated ``patch`` + functions; + - receiving the mocks as parameters doesn't mix nicely with pytest's approach of + naming fixtures as parameters, or ``pytest.mark.parametrize``; + - you can't easily undo the mocking during the test execution; + +Keywords: pytest mock +Platform: any +Classifier: Development Status :: 3 - Alpha +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3) +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python :: 2.6 +Classifier: Programming Language :: Python :: 2.7 +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.4 +Classifier: Programming Language :: Python :: 3.5 +Classifier: Topic :: Software Development :: Testing diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..28c02d7 --- /dev/null +++ b/README.rst @@ -0,0 +1,218 @@ +=========== +pytest-mock +=========== + +This plugin installs a ``mocker`` fixture which is a thin-wrapper around the patching API +provided by the excellent `mock `_ package, +but with the benefit of not having to worry about undoing patches at the end +of a test: + +.. code-block:: python + + + def test_unix_fs(mocker): + mocker.patch('os.remove') + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + +.. Using PNG badges because PyPI doesn't support SVG + +|python| |version| |downloads| |ci| |coverage| + +.. |version| image:: http://img.shields.io/pypi/v/pytest-mock.png + :target: https://pypi.python.org/pypi/pytest-mock + +.. |downloads| image:: http://img.shields.io/pypi/dm/pytest-mock.png + :target: https://pypi.python.org/pypi/pytest-mock + +.. |ci| image:: http://img.shields.io/travis/pytest-dev/pytest-mock.png + :target: https://travis-ci.org/pytest-dev/pytest-mock + +.. |coverage| image:: http://img.shields.io/coveralls/pytest-dev/pytest-mock.png + :target: https://coveralls.io/r/pytest-dev/pytest-mock + +.. |python| image:: https://pypip.in/py_versions/pytest-mock/badge.png + :target: https://pypi.python.org/pypi/pytest-mock/ + :alt: Supported Python versions + + +Usage +===== + +The ``mocker`` fixture has the same API as +`mock.patch `_, +supporting the same arguments: + +.. code-block:: python + + def test_foo(mocker): + # all valid calls + mocker.patch('os.remove') + mocker.patch.object(os, 'listdir', autospec=True) + mocked_isfile = mocker.patch('os.path.isfile') + +The supported methods are: + +* ``mocker.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch. +* ``mocker.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object. +* ``mocker.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple. +* ``mocker.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict. +* ``mocker.stopall()``: stops all active patches at this point. + +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: + +.. code-block:: python + + def test_context_manager(mocker): + a = A() + with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): + assert a.doIt() == True + +.. code-block:: console + + ================================== FAILURES =================================== + ____________________________ test_context_manager _____________________________ + in test_context_manager + with mocker.patch.object(a, 'doIt', return_value=True, autospec=True): + E AttributeError: __exit__ + + +You can also access ``Mock`` and ``MagicMock`` directly using from ``mocker`` +fixture: + +.. code-block:: python + + def test_feature(mocker): + ret = [mocker.Mock(return_value=True), mocker.Mock(return_value=True)] + mocker.patch('mylib.func', side_effect=ret) + +*New in version 0.5* + +Spy +--- + +*New in version 0.6* + +The spy acts exactly like the original method in all cases, except it allows use of `mock` +features with it, like retrieving call count. + +From version 0.7 onward it also works for class and static methods. Originally it was only safe to +use with instance methods. + +.. code-block:: python + + def test_spy(mocker): + class Foo(object): + def bar(self): + return 42 + + foo = Foo() + mocker.spy(foo, 'bar') + assert foo.bar() == 42 + assert foo.bar.call_count == 1 + +Stub +---- + +*New in version 0.6* + +The stub is a mock object that accepts any arguments and is useful to test callbacks, for instance. + +.. code-block:: python + + def test_stub(mocker): + def foo(on_something): + on_something('foo', 'bar') + + stub = mocker.stub() + + foo(stub) + stub.assert_called_once_with('foo', 'bar') + +Note +---- + +Prior to version ``0.4.0``, the ``mocker`` fixture was named ``mock``. +This was changed because naming the fixture ``mock`` conflicts with the +actual ``mock`` module, which made using it awkward when access to both the +module and the plugin were required within a test. + +The old fixture ``mock`` still works, but its use is discouraged and will be +removed in version ``1.0``. + +Requirements +============ + +* Python 2.6+, Python 3.2+ +* pytest +* mock (for Python < 3.3) + + +Install +======= + +Install using `pip `_: + +.. code-block:: console + + $ pip install pytest-mock + +Changelog +========= + +Please consult `releases `_. + +Why bother with a plugin? +========================= + +There are a number of different ``patch`` usages in the standard ``mock`` API, +but IMHO they don't scale very well when you have more than one or two +patches to apply. + +It may lead to an excessive nesting of ``with`` statements, breaking the flow +of the test: + +.. code-block:: python + + import mock + + def test_unix_fs(): + with mock.patch('os.remove'): + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + with mock.patch('os.listdir'): + assert UnixFS.ls('dir') == expected + # ... + + with mock.patch('shutil.copy'): + UnixFS.cp('src', 'dst') + # ... + + +One can use ``patch`` as a decorator to improve the flow of the test: + +.. code-block:: python + + @mock.patch('os.remove') + @mock.patch('os.listdir') + @mock.patch('shutil.copy') + def test_unix_fs(mocked_copy, mocked_listdir, mocked_remove): + UnixFS.rm('file') + os.remove.assert_called_once_with('file') + + assert UnixFS.ls('dir') == expected + # ... + + UnixFS.cp('src', 'dst') + # ... + +But this poses a few disadvantages: + +- test functions must receive the mock objects as parameter, even if you don't plan to + access them directly; also, order depends on the order of the decorated ``patch`` + functions; +- receiving the mocks as parameters doesn't mix nicely with pytest's approach of + naming fixtures as parameters, or ``pytest.mark.parametrize``; +- you can't easily undo the mocking during the test execution; diff --git a/pytest_mock.py b/pytest_mock.py new file mode 100644 index 0000000..9f06cd6 --- /dev/null +++ b/pytest_mock.py @@ -0,0 +1,128 @@ +import sys +import inspect + +import pytest + + +if sys.version_info >= (3, 3): # pragma: no cover + import unittest.mock as mock_module +else: + import mock as mock_module + + +class MockFixture(object): + """ + Fixture that provides the same interface to functions in the mock module, + ensuring that they are uninstalled at the end of each test. + """ + + Mock = mock_module.Mock + MagicMock = mock_module.MagicMock + ANY = mock_module.ANY + + def __init__(self): + self._patches = [] # list of mock._patch objects + self.patch = self._Patcher(self._patches) + + def stopall(self): + """ + Stop all patchers started by this fixture. Can be safely called multiple + times. + """ + for p in reversed(self._patches): + p.stop() + self._patches[:] = [] + + def spy(self, obj, name): + """ + Creates a spy of method. It will run method normally, but it is now + possible to use `mock` call features with it, like call count. + + :param object obj: An object. + :param unicode name: A method in object. + :rtype: mock.MagicMock + :return: Spy object. + """ + method = getattr(obj, name) + + autospec = inspect.ismethod(method) or inspect.isfunction(method) + # Can't use autospec classmethod or staticmethod objects + # see: https://bugs.python.org/issue23078 + if inspect.isclass(obj): + # bypass class descriptor: + # http://stackoverflow.com/questions/14187973/python3-check-if-method-is-static + value = obj.__getattribute__(obj, name) + if isinstance(value, (classmethod, staticmethod)): + autospec = False + + result = self.patch.object(obj, name, side_effect=method, + autospec=autospec) + return result + + def stub(self): + """ + Creates a stub method. It accepts any arguments. Ideal to register to + callbacks in tests. + + :rtype: mock.MagicMock + :return: Stub object. + """ + return mock_module.MagicMock(spec=lambda *args, **kwargs: None) + + class _Patcher(object): + """ + Object to provide the same interface as mock.patch, mock.patch.object, + etc. We need this indirection to keep the same API of the mock package. + """ + + def __init__(self, patches): + self._patches = patches + + def _start_patch(self, mock_func, *args, **kwargs): + """Patches something by calling the given function from the mock + module, registering the patch to stop it later and returns the + mock object resulting from the mock call. + """ + p = mock_func(*args, **kwargs) + mocked = p.start() + self._patches.append(p) + return mocked + + def object(self, *args, **kwargs): + """API to mock.patch.object""" + return self._start_patch(mock_module.patch.object, *args, **kwargs) + + def multiple(self, *args, **kwargs): + """API to mock.patch.multiple""" + return self._start_patch(mock_module.patch.multiple, *args, + **kwargs) + + def dict(self, *args, **kwargs): + """API to mock.patch.dict""" + return self._start_patch(mock_module.patch.dict, *args, **kwargs) + + def __call__(self, *args, **kwargs): + """API to mock.patch""" + return self._start_patch(mock_module.patch, *args, **kwargs) + + +@pytest.yield_fixture +def mocker(): + """ + return an object that has the same interface to the `mock` module, but + takes care of automatically undoing all patches after each test method. + """ + result = MockFixture() + yield result + result.stopall() + + +@pytest.fixture +def mock(mocker): + """ + Same as "mocker", but kept only for backward compatibility. + """ + import warnings + warnings.warn('"mock" fixture has been deprecated, use "mocker" instead', + DeprecationWarning) + return mocker diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9ea0a41 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,8 @@ +[bdist_wheel] +universal = 1 + +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3ca9b55 --- /dev/null +++ b/setup.py @@ -0,0 +1,37 @@ +from setuptools import setup + + +setup( + name='pytest-mock', + version='0.8.1', + entry_points={ + 'pytest11': ['pytest_mock = pytest_mock'], + }, + py_modules=['pytest_mock'], + platforms='any', + install_requires=[ + 'pytest>=2.4', + ], + extras_require={ + ':python_version=="2.6" or python_version=="2.7"': ['mock'], + }, + url='https://github.com/pytest-dev/pytest-mock/', + license='LGPL', + author='Bruno Oliveira', + author_email='nicoddemus@gmail.com', + description='Thin-wrapper around the mock package for easier use with py.test', + long_description=open('README.rst').read(), + keywords="pytest mock", + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Topic :: Software Development :: Testing', + ] +) diff --git a/test_pytest_mock.py b/test_pytest_mock.py new file mode 100644 index 0000000..7caf17d --- /dev/null +++ b/test_pytest_mock.py @@ -0,0 +1,233 @@ +import os +import platform + +import pytest + + +pytest_plugins = 'pytester' + +# could not make some of the tests work on PyPy, patches are welcome! +skip_pypy = pytest.mark.skipif(platform.python_implementation() == 'PyPy', + reason='could not make work on pypy') + + +class UnixFS(object): + """ + Wrapper to os functions to simulate a Unix file system, used for testing + the mock fixture. + """ + + @classmethod + def rm(cls, filename): + os.remove(filename) + + @classmethod + def ls(cls, path): + return os.listdir(path) + + +@pytest.fixture +def check_unix_fs_mocked(tmpdir, mocker): + """ + performs a standard test in a UnixFS, assuming that both `os.remove` and + `os.listdir` have been mocked previously. + """ + + def check(mocked_rm, mocked_ls): + assert mocked_rm is os.remove + assert mocked_ls is os.listdir + + file_name = tmpdir / 'foo.txt' + file_name.ensure() + + UnixFS.rm(str(file_name)) + mocked_rm.assert_called_once_with(str(file_name)) + assert os.path.isfile(str(file_name)) + + mocked_ls.return_value = ['bar.txt'] + assert UnixFS.ls(str(tmpdir)) == ['bar.txt'] + mocked_ls.assert_called_once_with(str(tmpdir)) + + mocker.stopall() + + assert UnixFS.ls(str(tmpdir)) == ['foo.txt'] + UnixFS.rm(str(file_name)) + assert not os.path.isfile(str(file_name)) + + return check + + +def mock_using_patch_object(mocker): + return mocker.patch.object(os, 'remove'), mocker.patch.object(os, 'listdir') + + +def mock_using_patch(mocker): + return mocker.patch('os.remove'), mocker.patch('os.listdir') + + +def mock_using_patch_multiple(mocker): + from pytest_mock import mock_module + + r = mocker.patch.multiple('os', remove=mock_module.DEFAULT, + listdir=mock_module.DEFAULT) + return r['remove'], r['listdir'] + + +@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch, + mock_using_patch_multiple], +) +def test_mock_patches(mock_fs, mocker, check_unix_fs_mocked): + """ + Installs mocks into `os` functions and performs a standard testing of + mock functionality. We parametrize different mock methods to ensure + all (intended, at least) mock API is covered. + """ + # mock it twice on purpose to ensure we unmock it correctly later + mock_fs(mocker) + mocked_rm, mocked_ls = mock_fs(mocker) + check_unix_fs_mocked(mocked_rm, mocked_ls) + + +def test_mock_patch_dict(mocker): + """ + Testing + :param mock: + """ + x = {'original': 1} + mocker.patch.dict(x, values=[('new', 10)], clear=True) + assert x == {'new': 10} + mocker.stopall() + assert x == {'original': 1} + + +def test_mock_fixture_is_deprecated(testdir): + """ + Test that a warning emitted when using deprecated "mock" fixture. + """ + testdir.makepyfile(''' + import warnings + import os + warnings.simplefilter('always') + + def test_foo(mock, tmpdir): + mock.patch('os.listdir', return_value=['mocked']) + assert os.listdir(str(tmpdir)) == ['mocked'] + mock.stopall() + assert os.listdir(str(tmpdir)) == [] + ''') + result = testdir.runpytest('-s') + result.stderr.fnmatch_lines(['*"mock" fixture has been deprecated*']) + + +def test_deprecated_mock(mock, tmpdir): + """ + Use backward-compatibility-only mock fixture to ensure complete coverage. + """ + mock.patch('os.listdir', return_value=['mocked']) + assert os.listdir(str(tmpdir)) == ['mocked'] + mock.stopall() + assert os.listdir(str(tmpdir)) == [] + + +def test_mocker_has_magic_mock_class_as_attribute_for_instantiation(): + from pytest_mock import mock_module, MockFixture + + mocker = MockFixture() + assert isinstance(mocker.MagicMock(), mock_module.MagicMock) + + +def test_mocker_has_mock_class_as_attribute_for_instantiation(): + from pytest_mock import mock_module, MockFixture + + mocker = MockFixture() + assert isinstance(mocker.Mock(), mock_module.Mock) + + +def test_mocker_stub(mocker): + def foo(on_something): + on_something('foo', 'bar') + + stub = mocker.stub() + + foo(stub) + stub.assert_called_once_with('foo', 'bar') + + +def test_instance_method_spy(mocker): + class Foo(object): + + def bar(self, arg): + return arg * 2 + + foo = Foo() + other = Foo() + spy = mocker.spy(foo, 'bar') + assert foo.bar(arg=10) == 20 + assert other.bar(arg=10) == 20 + foo.bar.assert_called_once_with(arg=10) + spy.assert_called_once_with(arg=10) + + +@skip_pypy +def test_instance_method_by_class_spy(mocker): + from pytest_mock import mock_module + + class Foo(object): + + def bar(self, arg): + return arg * 2 + + spy = mocker.spy(Foo, 'bar') + foo = Foo() + other = Foo() + assert foo.bar(arg=10) == 20 + assert other.bar(arg=10) == 20 + calls = [mock_module.call(foo, arg=10), mock_module.call(other, arg=10)] + assert spy.call_args_list == calls + + +@skip_pypy +def test_class_method_spy(mocker): + class Foo(object): + + @classmethod + def bar(cls, arg): + return arg * 2 + + spy = mocker.spy(Foo, 'bar') + assert Foo.bar(arg=10) == 20 + Foo.bar.assert_called_once_with(arg=10) + spy.assert_called_once_with(arg=10) + + +@skip_pypy +def test_class_method_with_metaclass_spy(mocker): + class MetaFoo(type): + pass + + class Foo(object): + + __metaclass__ = MetaFoo + + @classmethod + def bar(cls, arg): + return arg * 2 + + spy = mocker.spy(Foo, 'bar') + assert Foo.bar(arg=10) == 20 + Foo.bar.assert_called_once_with(arg=10) + spy.assert_called_once_with(arg=10) + + +@skip_pypy +def test_static_method_spy(mocker): + class Foo(object): + + @staticmethod + def bar(arg): + return arg * 2 + + spy = mocker.spy(Foo, 'bar') + assert Foo.bar(arg=10) == 20 + Foo.bar.assert_called_once_with(arg=10) + spy.assert_called_once_with(arg=10)