From dc683d594ec41c06e0a606df6761456e81530fcf Mon Sep 17 00:00:00 2001 From: Martin Richard Date: Thu, 16 Aug 2018 18:18:42 +0200 Subject: [PATCH] Rename CoroutineMock to CoroutineFunctionMock The old name is kept for backward-compatibility but will be removed for 1.0. --- README.rst | 17 ++--- asynctest/mock.py | 66 +++++++++-------- doc/asynctest.mock.rst | 7 +- setup.py | 8 +++ test/test_mock.py | 155 ++++++++++++++++++++-------------------- test/test_mock_await.py | 4 +- 6 files changed, 141 insertions(+), 116 deletions(-) diff --git a/README.rst b/README.rst index 23a8a9a..7960b7e 100644 --- a/README.rst +++ b/README.rst @@ -58,21 +58,22 @@ TestCases - ClockedTestCase allows to control the loop clock and run timed events without waiting the wall clock. -Mock and CoroutineMock -~~~~~~~~~~~~~~~~~~~~~~ +Mock and CoroutineFunctionMock +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - CoroutineMock is a new Mock class which mocks a coroutine function, and - returns a coroutine when called, + - CoroutineFunctionMock is a new Mock class which mocks a coroutine function, + and returns a coroutine when called, - MagicMock supports asynchronous context managers and asynchronous iterators, - - NonCallableMock, Mock and CoroutineMock can return CoroutineMock objects - when its attributes are get if there is a matching attribute in the spec - (or spec_set) object which is a coroutine function, + - NonCallableMock, Mock and CoroutineFunctionMock can return + CoroutineFunctionMock objects when its attributes are get if there is a + matching attribute in the spec (or spec_set) object which is a coroutine + function, - patch(), patch.object(), patch.multiple() return a MagickMock or - CoroutineMock object by default, according to the patched target, + CoroutineFunctionMock object by default, according to the patched target, - patch(), patch.object(), patch.multiple() handle generators and coroutines and their behavior can be controled when the generator or coroutine pauses, diff --git a/asynctest/mock.py b/asynctest/mock.py index 5ae2458..46f8b21 100644 --- a/asynctest/mock.py +++ b/asynctest/mock.py @@ -112,7 +112,8 @@ def __instancecheck__(cls, obj): if issubclass(_type, (NonCallableMagicMock, Mock, )): return True - if issubclass(cls, Mock) and not issubclass(cls, CoroutineMock): + if issubclass(cls, Mock) and \ + not issubclass(cls, CoroutineFunctionMock): if issubclass(_type, (MagicMock, )): return True @@ -144,13 +145,13 @@ def _mock_add_spec(self, spec, *args, **kwargs): def _get_child_mock(self, *args, **kwargs): _new_name = kwargs.get("_new_name") if _new_name in self.__dict__['_spec_coroutines']: - return CoroutineMock(*args, **kwargs) + return CoroutineFunctionMock(*args, **kwargs) _type = type(self) if issubclass(_type, MagicMock) and _new_name in async_magic_coroutines: - klass = CoroutineMock - elif issubclass(_type, CoroutineMock): + klass = CoroutineFunctionMock + elif issubclass(_type, CoroutineFunctionMock): klass = MagicMock elif not issubclass(_type, unittest.mock.CallableMixin): if issubclass(_type, unittest.mock.NonCallableMagicMock): @@ -282,7 +283,7 @@ class NonCallableMock(unittest.mock.NonCallableMock, ``True`` with ``mock`` as parameter. If ``spec`` or ``spec_set`` is defined and an attribute is get, - :class:`~asynctest.CoroutineMock` is returned instead of + :class:`~asynctest.CoroutineFunctionMock` is returned instead of :class:`~asynctest.Mock` when the matching spec attribute is a coroutine function. @@ -315,10 +316,9 @@ def __init__(self, spec=None, wraps=None, name=None, spec_set=None, self._asynctest_set_is_coroutine(is_coroutine) -class Mock(unittest.mock.Mock, metaclass=MockMetaMixin): - """ +class Mock(unittest.mock.Mock, metaclass=MockMetaMixin): """ Enhance :class:`unittest.mock.Mock` so it returns - a :class:`~asynctest.CoroutineMock` object instead of + a :class:`~asynctest.CoroutineFunctionMock` object instead of a :class:`~asynctest.Mock` object where a method on a ``spec`` or ``spec_set`` object is a coroutine. @@ -333,7 +333,7 @@ class Mock(unittest.mock.Mock, metaclass=MockMetaMixin): ... pass >>> type(asynctest.mock.Mock(Foo()).foo) - + >>> type(asynctest.mock.Mock(Foo()).bar) @@ -343,8 +343,8 @@ class Mock(unittest.mock.Mock, metaclass=MockMetaMixin): :class:`unittest.mock.Mock` object: the wrapped object may have methods defined as coroutine functions. - If you want to mock a coroutine function, use :class:`CoroutineMock` - instead. + If you want to mock a coroutine function, use + :class:`CoroutineFunctionMock` instead. See :class:`~asynctest.NonCallableMock` for details about :mod:`asynctest` features, and :mod:`unittest.mock` for the comprehensive documentation @@ -356,12 +356,12 @@ class MagicMock(AsyncMagicMixin, unittest.mock.MagicMock, metaclass=MockMetaMixin): """ Enhance :class:`unittest.mock.MagicMock` so it returns - a :class:`~asynctest.CoroutineMock` object instead of + a :class:`~asynctest.CoroutineFunctionMock` object instead of a :class:`~asynctest.Mock` object where a method on a ``spec`` or ``spec_set`` object is a coroutine. - If you want to mock a coroutine function, use :class:`CoroutineMock` - instead. + If you want to mock a coroutine function, use + :class:`CoroutineFunctionMock` instead. :class:`MagicMock` allows to mock ``__aenter__``, ``__aexit__``, ``__aiter__`` and ``__anext__``. @@ -371,7 +371,7 @@ class MagicMock(AsyncMagicMixin, unittest.mock.MagicMock, values to be returned during iteration. You can not mock ``__await__``. If you want to mock an object implementing - __await__, :class:`CoroutineMock` will likely be sufficient. + __await__, :class:`CoroutineFunctionMock` will likely be sufficient. see :class:`~asynctest.Mock`. @@ -407,7 +407,7 @@ def wait_next(self, skip=0): Unlike :meth:`wait` that counts any await, mock has to be awaited once more, disregarding to the current - :attr:`asynctest.CoroutineMock.await_count`. + :attr:`asynctest.CoroutineFunctionMock.await_count`. :param skip: How many awaits will be skipped. As a result, the mock should be awaited at least @@ -474,15 +474,16 @@ def __bool__(self): return self._mock.await_count != 0 -class CoroutineMock(Mock): +class CoroutineFunctionMock(Mock): """ Enhance :class:`~asynctest.mock.Mock` with features allowing to mock a coroutine function. - The :class:`~asynctest.CoroutineMock` object will behave so the object is - recognized as coroutine function, and the result of a call as a coroutine: + The :class:`~asynctest.CoroutineFunctionMock` object will behave so the + object is recognized as coroutine function, and the result of a call as a + coroutine: - >>> mock = CoroutineMock() + >>> mock = CoroutineFunctionMock() >>> asyncio.iscoroutinefunction(mock) True >>> asyncio.iscoroutine(mock()) @@ -501,7 +502,7 @@ class CoroutineMock(Mock): ``StopIteration`` is raised immediately, - if ``side_effect`` is not defined, the coroutine will return the value defined by ``return_value``, hence, by default, the coroutine returns - a new :class:`~asynctest.CoroutineMock` object. + a new :class:`~asynctest.CoroutineFunctionMock` object. If the outcome of ``side_effect`` or ``return_value`` is a coroutine, the mock coroutine obtained when the mock object is called will be this @@ -511,6 +512,10 @@ class CoroutineMock(Mock): case, the :class:`~asynctest.Mock` object behavior is the same as with an :class:`unittest.mock.Mock` object: the wrapped object may have methods defined as coroutine functions. + + .. versionadded:: 0.13 + :class:`~asynctest.mock.CoroutineFunctionMock` used to be named + `CoroutineMock`, this name has been deprecated. """ #: Property which is set when the mock is awaited. Its ``wait`` and #: ``wait_next`` coroutine methods can be used to synchronize execution. @@ -613,7 +618,8 @@ def _error_message(): def assert_awaited_once_with(_mock_self, *args, **kwargs): """ - Assert that the mock was awaited exactly once and with the specified arguments. + Assert that the mock was awaited exactly once and with the specified + arguments. .. versionadded:: 0.12 """ @@ -702,6 +708,9 @@ def reset_mock(self, *args, **kwargs): self.await_args_list = unittest.mock._CallList() +CoroutineMock = CoroutineFunctionMock + + def create_autospec(spec, spec_set=False, instance=False, _parent=None, _name=None, **kwargs): """ @@ -740,7 +749,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, if instance: raise RuntimeError("Instance can not be True when create_autospec " "is mocking a coroutine function") - Klass = CoroutineMock + Klass = CoroutineFunctionMock elif not unittest.mock._callable(spec): Klass = NonCallableMagicMock elif is_type and instance and not unittest.mock._instance_callable(spec): @@ -764,8 +773,9 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, # Can't wrap the mock with asyncio.coroutine because it doesn't # detect a CoroWrapper as an awaitable in debug mode. # It is safe to do so because the mock object wrapped by - # _set_signature returns the result of the CoroutineMock itself, - # which is a Coroutine (as defined in CoroutineMock._mock_call) + # _set_signature returns the result of the CoroutineFunctionMock + # itself, which is a Coroutine (as defined in + # CoroutineFunctionMock._mock_call) mock._is_coroutine = _is_coroutine mock.awaited = _AwaitEvent(mock) mock.await_count = 0 @@ -816,7 +826,7 @@ def f(*args, **kwargs): skipfirst = unittest.mock._must_skip(spec, entry, is_type) kwargs['_eat_self'] = skipfirst if asyncio.iscoroutinefunction(original): - child_klass = CoroutineMock + child_klass = CoroutineFunctionMock else: child_klass = MagicMock new = child_klass(parent=parent, name=entry, _new_name=entry, @@ -863,7 +873,7 @@ def _update_new_callable(patcher, new, new_callable): original = original.__get__(None, object) if asyncio.iscoroutinefunction(original): - patcher.new_callable = CoroutineMock + patcher.new_callable = CoroutineFunctionMock else: patcher.new_callable = MagicMock @@ -1104,7 +1114,7 @@ def patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, ``new`` specifies which object will replace the ``target`` when the patch is applied. By default, the target will be patched with an instance of - :class:`~asynctest.CoroutineMock` if it is a coroutine, or + :class:`~asynctest.CoroutineFunctionMock` if it is a coroutine, or a :class:`~asynctest.MagicMock` object. It is a replacement to :func:`unittest.mock.patch`, but using diff --git a/doc/asynctest.mock.rst b/doc/asynctest.mock.rst index 1a9ea53..170eeb3 100644 --- a/doc/asynctest.mock.rst +++ b/doc/asynctest.mock.rst @@ -18,10 +18,15 @@ :members: :undoc-members: - .. autoclass:: asynctest.CoroutineMock + .. autoclass:: asynctest.CoroutineFunctionMock :members: :undoc-members: + .. class:: asynctest.CoroutineMock + + .. deprecated:: 0.13 + Use :class:`~asynctest.CoroutineFunctionMock` instead. + Patch ~~~~~ diff --git a/setup.py b/setup.py index af113f3..7940f96 100644 --- a/setup.py +++ b/setup.py @@ -14,3 +14,11 @@ if __name__ == "__main__": from setuptools import setup setup(**args) + + try: + from warnings import warn + warn("CoroutineMock has been renamed to CoroutineFunctionMock, the alias " + "will be removed in the future", DeprecationWarning) + except DeprecationWarning as e: + import sys + print("DeprecationWarning:", str(e), file=sys.stderr) diff --git a/test/test_mock.py b/test/test_mock.py index abeecaf..fce9a9d 100644 --- a/test/test_mock.py +++ b/test/test_mock.py @@ -205,17 +205,17 @@ def test_mock_returns_coroutine_according_to_spec(self, klass): mock = klass(**{attr: spec}) self.assertIsInstance(mock.a_function, (asynctest.Mock, asynctest.MagicMock)) - self.assertNotIsInstance(mock.a_function, asynctest.CoroutineMock) - self.assertIsInstance(mock.a_coroutine, asynctest.CoroutineMock) - self.assertIsInstance(mock.a_classmethod_coroutine, asynctest.CoroutineMock) - self.assertIsInstance(mock.a_staticmethod_coroutine, asynctest.CoroutineMock) + self.assertNotIsInstance(mock.a_function, asynctest.CoroutineFunctionMock) + self.assertIsInstance(mock.a_coroutine, asynctest.CoroutineFunctionMock) + self.assertIsInstance(mock.a_classmethod_coroutine, asynctest.CoroutineFunctionMock) + self.assertIsInstance(mock.a_staticmethod_coroutine, asynctest.CoroutineFunctionMock) mock.a_coroutine.return_value = "PROBE" self.assertEqual("PROBE", run_coroutine(mock.a_coroutine())) if _using_await: - self.assertIsInstance(mock.an_async_coroutine, asynctest.CoroutineMock) - self.assertIsInstance(mock.an_async_classmethod_coroutine, asynctest.CoroutineMock) - self.assertIsInstance(mock.an_async_staticmethod_coroutine, asynctest.CoroutineMock) + self.assertIsInstance(mock.an_async_coroutine, asynctest.CoroutineFunctionMock) + self.assertIsInstance(mock.an_async_classmethod_coroutine, asynctest.CoroutineFunctionMock) + self.assertIsInstance(mock.an_async_staticmethod_coroutine, asynctest.CoroutineFunctionMock) mock.an_async_coroutine.return_value = "PROBE" self.assertEqual("PROBE", run_coroutine(mock.an_async_coroutine())) @@ -311,23 +311,23 @@ class Test_MagicMock(unittest.TestCase, _Test_subclass, class_to_test = 'MagicMock' -class Test_CoroutineMock(unittest.TestCase, _Test_called_coroutine, +class Test_CoroutineFunctionMock(unittest.TestCase, _Test_called_coroutine, _Test_Spec_Spec_Set_Returns_Coroutine_Mock): - class_to_test = 'CoroutineMock' + class_to_test = 'CoroutineFunctionMock' def test_asyncio_iscoroutinefunction(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() self.assertTrue(asyncio.iscoroutinefunction(mock)) - def test_called_CoroutineMock_returns_MagicMock(self): - mock = asynctest.mock.CoroutineMock() + def test_called_CoroutineFunctionMock_returns_MagicMock(self): + mock = asynctest.mock.CoroutineFunctionMock() self.assertIsInstance(run_coroutine(mock()), asynctest.mock.MagicMock) -class Test_CoroutineMock_awaited(asynctest.TestCase): +class Test_CoroutineFunctionMock_awaited(asynctest.TestCase): @asynctest.fail_on(unused_loop=False) def test_awaited_delays_creation_of_condition(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() self.assertIsNone(mock.awaited._condition) mock() self.assertIsNone(mock.awaited._condition) @@ -335,8 +335,8 @@ def test_awaited_delays_creation_of_condition(self): self.assertIsNotNone(mock.awaited._condition) @asyncio.coroutine - def test_awaited_CoroutineMock_sets_awaited(self): - mock = asynctest.mock.CoroutineMock() + def test_awaited_CoroutineFunctionMock_sets_awaited(self): + mock = asynctest.mock.CoroutineFunctionMock() yield from mock() self.assertTrue(mock.awaited) @@ -347,14 +347,14 @@ def test_awaited_CoroutineMock_sets_awaited(self): def side_effect(): raise RuntimeError() - mock = asynctest.mock.CoroutineMock(side_effect=side_effect) + mock = asynctest.mock.CoroutineFunctionMock(side_effect=side_effect) with self.assertRaises(RuntimeError): yield from mock() @asyncio.coroutine - def test_awaited_CoroutineMock_counts(self): - mock = asynctest.mock.CoroutineMock() + def test_awaited_CoroutineFunctionMock_counts(self): + mock = asynctest.mock.CoroutineFunctionMock() yield from mock() yield from mock() self.assertEqual(mock.await_count, 2) @@ -366,7 +366,7 @@ def test_awaited_CoroutineMock_counts(self): def side_effect(): raise RuntimeError() - mock = asynctest.mock.CoroutineMock(side_effect=side_effect) + mock = asynctest.mock.CoroutineFunctionMock(side_effect=side_effect) with self.assertRaises(RuntimeError): yield from mock() @@ -394,7 +394,7 @@ def test_awaited_from_autospec_mock(self): @asyncio.coroutine def test_awaited_wait(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() t = asyncio.ensure_future(mock.awaited.wait()) yield from mock() yield from mock() @@ -409,7 +409,7 @@ def test_awaited_wait(self): @asyncio.coroutine def test_awaited_wait_next(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() yield from mock() t = asyncio.ensure_future(mock.awaited.wait_next()) yield from asyncio.sleep(0.01) @@ -429,7 +429,7 @@ def test_awaited_wait_next(self): @asyncio.coroutine def test_await_args(self): with self.subTest('in order'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() t1 = mock('foo') t2 = mock('bar') yield from t1 @@ -437,7 +437,7 @@ def test_await_args(self): self.assertEqual(mock.await_args, asynctest.call('bar')) with self.subTest('out of order'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() t1 = mock('foo') t2 = mock('bar') yield from t2 @@ -447,7 +447,7 @@ def test_await_args(self): @asyncio.coroutine def test_await_args_list(self): with self.subTest('in order'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() t1 = mock('foo') t2 = mock('bar') yield from t1 @@ -455,7 +455,7 @@ def test_await_args_list(self): self.assertEqual(mock.await_args_list, [asynctest.call('foo'), asynctest.call('bar')]) with self.subTest('out of order'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() t1 = mock('foo') t2 = mock('bar') yield from t2 @@ -464,7 +464,7 @@ def test_await_args_list(self): @asyncio.coroutine def test_assert_awaited(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_awaited() @@ -474,7 +474,7 @@ def test_assert_awaited(self): @asyncio.coroutine def test_assert_awaited_once(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_awaited_once() @@ -488,7 +488,7 @@ def test_assert_awaited_once(self): @asyncio.coroutine def test_assert_awaited_with(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_awaited_with('foo') @@ -502,7 +502,7 @@ def test_assert_awaited_with(self): @asyncio.coroutine def test_assert_awaited_once_with(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_awaited_once_with('foo') @@ -516,7 +516,7 @@ def test_assert_awaited_once_with(self): @asyncio.coroutine def test_assert_any_wait(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_any_await('bar') @@ -536,7 +536,7 @@ def test_assert_has_awaits(self): calls = [asynctest.call('bar'), asynctest.call('baz')] with self.subTest('any_order=False'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_has_awaits(calls) @@ -556,7 +556,7 @@ def test_assert_has_awaits(self): mock.assert_has_awaits(calls) with self.subTest('any_order=True'): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() with self.assertRaises(AssertionError): mock.assert_has_awaits(calls, any_order=True) @@ -577,7 +577,7 @@ def test_assert_has_awaits(self): @asyncio.coroutine def test_assert_not_awaited(self): - mock = asynctest.mock.CoroutineMock() + mock = asynctest.mock.CoroutineFunctionMock() mock.assert_not_awaited() @@ -591,14 +591,14 @@ class TestMockInheritanceModel(unittest.TestCase): 'NonCallableMagicMock': 'NonCallableMock', 'Mock': 'NonCallableMock', 'MagicMock': 'Mock', - 'CoroutineMock': 'Mock', + 'CoroutineFunctionMock': 'Mock', } - def test_Mock_is_not_CoroutineMock(self): - self.assertNotIsInstance(asynctest.mock.Mock(), asynctest.mock.CoroutineMock) + def test_Mock_is_not_CoroutineFunctionMock(self): + self.assertNotIsInstance(asynctest.mock.Mock(), asynctest.mock.CoroutineFunctionMock) - def test_MagicMock_is_not_CoroutineMock(self): - self.assertNotIsInstance(asynctest.mock.MagicMock(), asynctest.mock.CoroutineMock) + def test_MagicMock_is_not_CoroutineFunctionMock(self): + self.assertNotIsInstance(asynctest.mock.MagicMock(), asynctest.mock.CoroutineFunctionMock) @staticmethod def make_inheritance_test(child, parent): @@ -660,110 +660,110 @@ def test_mock_function(mock): self.assertIn("test_mock_class", called) self.assertIn("test_mock_function", called) - def test_patch_as_decorator_uses_CoroutineMock_on_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_coroutine_function(self): called = False @asynctest.mock.patch('test.test_mock.Test.a_coroutine') def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() self.assertTrue(called) - def test_patch_as_decorator_uses_CoroutineMock_on_classmethod_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_classmethod_coroutine_function(self): called = False @asynctest.mock.patch("test.test_mock.Test.a_classmethod_coroutine") def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() self.assertTrue(called) - def test_patch_as_decorator_uses_CoroutineMock_on_staticmethod_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_staticmethod_coroutine_function(self): called = False @asynctest.mock.patch("test.test_mock.Test.a_staticmethod_coroutine") def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() self.assertTrue(called) - def test_patch_as_context_manager_uses_CoroutineMock_on_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.a_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.a_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) - def test_patch_as_context_manager_uses_CoroutineMock_on_classmethod_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_classmethod_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.a_classmethod_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.a_classmethod_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) - def test_patch_as_context_manager_uses_CoroutineMock_on_staticmethod_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_staticmethod_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.a_staticmethod_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.a_staticmethod_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) if _using_await: - def test_patch_as_context_manager_uses_CoroutineMock_on_async_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_async_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.an_async_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.an_async_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) - def test_patch_as_context_manager_uses_CoroutineMock_on_async_classmethod_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_async_classmethod_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.an_async_classmethod_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.an_async_classmethod_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) - def test_patch_as_context_manager_uses_CoroutineMock_on_async_staticmethod_coroutine_function(self): + def test_patch_as_context_manager_uses_CoroutineFunctionMock_on_async_staticmethod_coroutine_function(self): with asynctest.mock.patch('test.test_mock.Test.an_async_staticmethod_coroutine') as mock: import test.test_mock self.assertIs(test.test_mock.Test.an_async_staticmethod_coroutine, mock) - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) - def test_patch_as_decorator_uses_CoroutineMock_on_async_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_async_coroutine_function(self): called = False @asynctest.mock.patch('test.test_mock.Test.an_async_coroutine') def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() self.assertTrue(called) - def test_patch_as_decorator_uses_CoroutineMock_on_async_classmethod_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_async_classmethod_coroutine_function(self): called = False @asynctest.mock.patch('test.test_mock.Test.an_async_classmethod_coroutine') def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() self.assertTrue(called) - def test_patch_as_decorator_uses_CoroutineMock_on_async_staticmethod_coroutine_function(self): + def test_patch_as_decorator_uses_CoroutineFunctionMock_on_async_staticmethod_coroutine_function(self): called = False @asynctest.mock.patch('test.test_mock.Test.an_async_staticmethod_coroutine') def test_mock_coroutine(mock): nonlocal called - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) called = True test_mock_coroutine() @@ -888,13 +888,13 @@ def test_patch_with_MagicMock(self): with asynctest.mock.patch.object(obj, 'test') as mock: self.assertIsInstance(mock, asynctest.mock.MagicMock) - def test_patch_coroutine_function_with_CoroutineMock(self): + def test_patch_coroutine_function_with_CoroutineFunctionMock(self): with asynctest.mock.patch.object(Test(), 'a_coroutine') as mock: - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) if _using_await: with asynctest.mock.patch.object(Test(), 'an_async_coroutine') as mock: - self.assertIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertIsInstance(mock, asynctest.mock.CoroutineFunctionMock) def test_patch_decorates_coroutine(self): obj = Test() @@ -922,7 +922,7 @@ def test_patch_with_MagicMock(self): import test.test_mock self.assertIsInstance(test.test_mock.Test, asynctest.mock.MagicMock) - def test_patch_coroutine_function_with_CoroutineMock(self): + def test_patch_coroutine_function_with_CoroutineFunctionMock(self): default = asynctest.mock.DEFAULT also_patch = {} @@ -936,11 +936,11 @@ def test_patch_coroutine_function_with_CoroutineMock(self): import test.test_mock obj = test.test_mock.Test() self.assertIsInstance(obj.a_function, asynctest.mock.MagicMock) - self.assertIsInstance(obj.a_coroutine, asynctest.mock.CoroutineMock) + self.assertIsInstance(obj.a_coroutine, asynctest.mock.CoroutineFunctionMock) if _using_await: self.assertIsInstance(obj.an_async_coroutine, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) def test_patch_decorates_coroutine(self): patch = functools.partial(asynctest.mock.patch.multiple, @@ -1016,19 +1016,19 @@ def patched(mock): nonlocal called called = True self.assertIsInstance(mock.a_coroutine, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) self.assertIsInstance(mock().a_coroutine, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) self.assertIsInstance(mock.a_function, asynctest.mock.Mock) self.assertIsInstance(mock().a_function, asynctest.mock.Mock) if _using_await: self.assertIsInstance(mock.an_async_coroutine, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) self.assertIsInstance(mock().an_async_coroutine, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) patched() self.assertTrue(called) @@ -1627,7 +1627,8 @@ def a_coroutine(fut): # It's really hard to test this behavior for a coroutine, but I assume it's # fine as long as the implementation is shared with a generator. Also, it's # really hard to fall in a case like this one with a coroutine. - @unittest.skipIf(platform.python_implementation() != "CPython", "Test relying on how __del__ is called by implementation") + @unittest.skipIf(platform.python_implementation() != "CPython", + "Test relying on how __del__ is called by implementation") def test_patch_stopped_when_generator_is_collected(self): @patch_is_patched(scope=asynctest.GLOBAL) def a_generator(): @@ -1890,10 +1891,10 @@ def coroutine(): # * Ensure we check the signature of the coroutine function (and/or # generator) # * Ensure a coroutine function attribute of a mock create with - # create_autospec is a CoroutineMock + # create_autospec is a CoroutineFunctionMock # * Ensure an instance of a class mocked from create_autospec will be an # asynctest mock, and its coroutine attribute will be mocked by - # a CoroutineMock + # a CoroutineFunctionMock # * Ensure all expected create_autospec tests still run fine # # Also test cases where create_autospec is used (_patch, etc) @@ -1921,7 +1922,7 @@ def a_generator(): # This happens with mocks updated by _set_signature expected_type = type(unittest_mock) self.assertIsInstance(mock, expected_type) - self.assertNotIsInstance(mock, asynctest.mock.CoroutineMock) + self.assertNotIsInstance(mock, asynctest.mock.CoroutineFunctionMock) def test_autospec_of_coroutine_function_is_coroutinefunction(self): mock = asynctest.mock.create_autospec(Test.a_function) diff --git a/test/test_mock_await.py b/test/test_mock_await.py index 37fb301..3cb21c8 100644 --- a/test/test_mock_await.py +++ b/test/test_mock_await.py @@ -63,9 +63,9 @@ def test_mock_magic_methods_are_coroutine_mocks(self, klass): with self.subTest(spec=spec): mock_instance = asynctest.mock.MagicMock(spec) self.assertIsInstance(mock_instance.__aenter__, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) self.assertIsInstance(mock_instance.__aexit__, - asynctest.mock.CoroutineMock) + asynctest.mock.CoroutineFunctionMock) def test_mock_supports_async_context_manager(self, klass): called = False