Lines Matching refs:self

22     def __init__(self): pass
23 async def async_method(self): pass
24 def normal_method(self): pass
34 def __await__(self): yield
43 def a(self): pass
60 def test_is_coroutine_function_patch(self):
63 self.assertTrue(iscoroutinefunction(mock_method))
66 def test_is_async_patch(self):
70 self.assertTrue(inspect.isawaitable(m))
76 self.assertTrue(inspect.isawaitable(m))
82 def test_is_AsyncMock_patch(self):
85 self.assertIsInstance(mock_method, AsyncMock)
89 def test_is_AsyncMock_patch_staticmethod(self):
92 self.assertIsInstance(mock_method, AsyncMock)
96 def test_is_AsyncMock_patch_classmethod(self):
99 self.assertIsInstance(mock_method, AsyncMock)
103 def test_async_def_patch(self):
107 self.assertEqual(func_args_mock._mock_name, "async_func_args")
108 self.assertEqual(func_mock._mock_name, "async_func")
110 self.assertIsInstance(async_func, AsyncMock)
111 self.assertIsInstance(async_func_args, AsyncMock)
113 self.assertEqual(await async_func(), 1)
114 self.assertEqual(await async_func_args(1, 2, c=3), 2)
117 self.assertTrue(inspect.iscoroutinefunction(async_func))
121 def test_is_async_function_cm(self):
124 self.assertTrue(iscoroutinefunction(mock_method))
128 def test_is_async_cm(self):
132 self.assertTrue(inspect.isawaitable(m))
137 def test_is_AsyncMock_cm(self):
140 self.assertIsInstance(mock_method, AsyncMock)
144 def test_async_def_cm(self):
147 self.assertIsInstance(async_func, AsyncMock)
148 self.assertTrue(inspect.iscoroutinefunction(async_func))
152 def test_patch_dict_async_def(self):
156 self.assertEqual(foo['a'], 'b')
158 self.assertTrue(iscoroutinefunction(test_async))
161 def test_patch_dict_async_def_context(self):
165 self.assertEqual(foo['a'], 'b')
171 def test_iscoroutinefunction_default(self):
173 self.assertTrue(iscoroutinefunction(mock))
175 def test_iscoroutinefunction_function(self):
178 self.assertTrue(iscoroutinefunction(mock))
179 self.assertTrue(inspect.iscoroutinefunction(mock))
181 def test_isawaitable(self):
184 self.assertTrue(inspect.isawaitable(m))
186 self.assertIn('assert_awaited', dir(mock))
188 def test_iscoroutinefunction_normal_function(self):
191 self.assertTrue(iscoroutinefunction(mock))
192 self.assertTrue(inspect.iscoroutinefunction(mock))
194 def test_future_isfuture(self):
200 self.assertIsInstance(mock, asyncio.Future)
204 def test_is_AsyncMock_patch(self):
207 self.assertIsInstance(mock_method.async_method, AsyncMock)
208 self.assertIsInstance(mock_method, MagicMock)
212 self.assertIsInstance(mock_method.normal_method, MagicMock)
217 def test_create_autospec_instance(self):
218 with self.assertRaises(RuntimeError):
222 def test_create_autospec_awaitable_class(self):
223 self.assertIsInstance(create_autospec(AwaitableClass), AsyncMock)
225 def test_create_autospec(self):
231 self.assertEqual(spec.await_count, 0)
232 self.assertIsNone(spec.await_args)
233 self.assertEqual(spec.await_args_list, [])
238 self.assertTrue(iscoroutinefunction(spec))
239 self.assertTrue(asyncio.iscoroutine(awaitable))
240 self.assertEqual(spec.await_count, 1)
241 self.assertEqual(spec.await_args, call(1, 2, c=3))
242 self.assertEqual(spec.await_args_list, [call(1, 2, c=3)])
248 with self.assertRaises(AssertionError):
252 def test_patch_with_autospec(self):
257 self.assertIsInstance(mock_method.mock, AsyncMock)
259 self.assertTrue(iscoroutinefunction(mock_method))
260 self.assertTrue(asyncio.iscoroutine(awaitable))
261 self.assertTrue(inspect.isawaitable(awaitable))
264 self.assertEqual(mock_method.await_count, 0)
265 self.assertEqual(mock_method.await_args_list, [])
266 self.assertIsNone(mock_method.await_args)
271 self.assertEqual(mock_method.await_count, 1)
272 self.assertEqual(mock_method.await_args, call(1, 2, c=3))
273 self.assertEqual(mock_method.await_args_list, [call(1, 2, c=3)])
280 self.assertEqual(mock_method.await_count, 0)
281 self.assertIsNone(mock_method.await_args)
282 self.assertEqual(mock_method.await_args_list, [])
288 def test_spec_normal_methods_on_class(self):
291 self.assertIsInstance(mock.async_method, AsyncMock)
292 self.assertIsInstance(mock.normal_method, MagicMock)
295 with self.subTest(f"test method types with {mock_type}"):
298 def test_spec_normal_methods_on_class_with_mock(self):
300 self.assertIsInstance(mock.async_method, AsyncMock)
301 self.assertIsInstance(mock.normal_method, Mock)
303 def test_spec_normal_methods_on_class_with_mock_seal(self):
306 with self.assertRaises(AttributeError):
308 with self.assertRaises(AttributeError):
311 def test_spec_mock_type_kw(self):
314 self.assertIsInstance(async_mock, mock_type)
315 with assertNeverAwaited(self):
316 self.assertTrue(inspect.isawaitable(async_mock()))
319 self.assertIsInstance(sync_mock, mock_type)
322 with self.subTest(f"test spec kwarg with {mock_type}"):
325 def test_spec_mock_type_positional(self):
328 self.assertIsInstance(async_mock, mock_type)
329 with assertNeverAwaited(self):
330 self.assertTrue(inspect.isawaitable(async_mock()))
333 self.assertIsInstance(sync_mock, mock_type)
336 with self.subTest(f"test spec positional with {mock_type}"):
339 def test_spec_as_normal_kw_AsyncMock(self):
341 self.assertIsInstance(mock, AsyncMock)
343 self.assertTrue(inspect.isawaitable(m))
346 def test_spec_as_normal_positional_AsyncMock(self):
348 self.assertIsInstance(mock, AsyncMock)
350 self.assertTrue(inspect.isawaitable(m))
353 def test_spec_async_mock(self):
356 self.assertIsInstance(mock_method, AsyncMock)
360 def test_spec_parent_not_async_attribute_is(self):
363 self.assertIsInstance(mock_method, MagicMock)
364 self.assertIsInstance(mock_method.async_method, AsyncMock)
368 def test_target_async_spec_not(self):
371 self.assertIsInstance(mock_method, MagicMock)
372 self.assertFalse(inspect.iscoroutine(mock_method))
373 self.assertFalse(inspect.isawaitable(mock_method))
377 def test_target_not_async_spec_is(self):
380 self.assertIsInstance(mock_async_func, AsyncMock)
383 def test_spec_async_attributes(self):
386 self.assertIsInstance(MockNormalClass.async_method, AsyncMock)
387 self.assertIsInstance(MockNormalClass, MagicMock)
393 def test_is_AsyncMock_patch(self):
396 self.assertIsInstance(async_method, AsyncMock)
399 def test_is_async_AsyncMock(self):
401 self.assertTrue(iscoroutinefunction(mock))
402 self.assertIsInstance(mock, AsyncMock)
404 def test_is_child_AsyncMock(self):
406 self.assertTrue(iscoroutinefunction(mock.async_method))
407 self.assertFalse(iscoroutinefunction(mock.normal_method))
408 self.assertIsInstance(mock.async_method, AsyncMock)
409 self.assertIsInstance(mock.normal_method, MagicMock)
410 self.assertIsInstance(mock, MagicMock)
412 def test_magicmock_lambda_spec(self):
417 self.assertIsInstance(cm, MagicMock)
421 async def test_add_return_value(self):
422 async def addition(self, var): pass
427 self.assertEqual(output, 10)
429 async def test_add_side_effect_exception(self):
432 with self.assertRaises(Exception):
435 async def test_add_side_effect_coroutine(self):
440 self.assertEqual(result, 6)
442 async def test_add_side_effect_normal_function(self):
447 self.assertEqual(result, 6)
449 async def test_add_side_effect_iterable(self):
453 self.assertEqual(await mock(), item)
455 with self.assertRaises(StopAsyncIteration) as e:
458 async def test_add_side_effect_exception_iterable(self):
464 self.assertEqual(await mock(), 1)
466 with self.assertRaises(SampleException) as e:
469 async def test_return_value_AsyncMock(self):
473 self.assertIs(result, value)
475 async def test_return_value_awaitable(self):
480 self.assertIsInstance(result, asyncio.Future)
482 async def test_side_effect_awaitable_values(self):
488 self.assertIsInstance(result, asyncio.Future)
490 with self.assertRaises(StopAsyncIteration):
493 async def test_side_effect_is_AsyncMock(self):
498 self.assertEqual(result, 10)
500 async def test_wraps_coroutine(self):
511 self.assertEqual(result, value)
513 self.assertTrue(ran)
515 async def test_wraps_normal_function(self):
526 self.assertEqual(result, value)
528 self.assertTrue(ran)
530 async def test_await_args_list_order(self):
537 self.assertEqual(async_mock.await_args_list, [call(1), call(2)])
538 self.assertEqual(async_mock.call_args_list, [call(2), call(1)])
542 def test_async_magic_methods_return_async_mocks(self):
544 self.assertIsInstance(m_mock.__aenter__, AsyncMock)
545 self.assertIsInstance(m_mock.__aexit__, AsyncMock)
546 self.assertIsInstance(m_mock.__anext__, AsyncMock)
549 self.assertIsInstance(m_mock.__aiter__, MagicMock)
551 def test_sync_magic_methods_return_magic_mocks(self):
553 self.assertIsInstance(a_mock.__enter__, MagicMock)
554 self.assertIsInstance(a_mock.__exit__, MagicMock)
555 self.assertIsInstance(a_mock.__next__, MagicMock)
556 self.assertIsInstance(a_mock.__len__, MagicMock)
558 def test_magicmock_has_async_magic_methods(self):
560 self.assertTrue(hasattr(m_mock, "__aenter__"))
561 self.assertTrue(hasattr(m_mock, "__aexit__"))
562 self.assertTrue(hasattr(m_mock, "__anext__"))
564 def test_asyncmock_has_sync_magic_methods(self):
566 self.assertTrue(hasattr(a_mock, "__enter__"))
567 self.assertTrue(hasattr(a_mock, "__exit__"))
568 self.assertTrue(hasattr(a_mock, "__next__"))
569 self.assertTrue(hasattr(a_mock, "__len__"))
571 def test_magic_methods_are_async_functions(self):
573 self.assertIsInstance(m_mock.__aenter__, AsyncMock)
574 self.assertIsInstance(m_mock.__aexit__, AsyncMock)
576 self.assertTrue(iscoroutinefunction(m_mock.__aenter__))
577 self.assertTrue(iscoroutinefunction(m_mock.__aexit__))
582 async def __aenter__(self, *args, **kwargs): pass
584 async def __aexit__(self, *args, **kwargs): pass
587 def __enter__(self, *args, **kwargs): pass
589 def __exit__(self, *args, **kwargs): pass
593 def __init__(self):
594 self.session = None
596 async def main(self):
597 async with self.session.post('https://python.org') as response:
601 def test_set_return_value_of_aenter(self):
603 pc = self.ProductionCode()
611 self.assertEqual(result, {'json': 123})
614 with self.subTest(f"test set return value of aenter with {mock_type}"):
617 def test_mock_supports_async_context_manager(self):
620 cm = self.WithAsyncContextManager()
630 self.assertTrue(called)
631 self.assertTrue(cm_mock.__aenter__.called)
632 self.assertTrue(cm_mock.__aexit__.called)
635 # We mock __aenter__ so it does not return self
636 self.assertIsNot(cm_mock, cm_result)
639 with self.subTest(f"test context manager magics with {mock_type}"):
643 def test_mock_customize_async_context_manager(self):
644 instance = self.WithAsyncContextManager()
654 self.assertIs(run(use_context_manager()), expected_result)
656 def test_mock_customize_async_context_manager_with_coroutine(self):
668 instance = self.WithAsyncContextManager()
679 self.assertTrue(enter_called)
680 self.assertTrue(exit_called)
682 def test_context_manager_raise_exception_by_default(self):
687 instance = self.WithAsyncContextManager()
689 with self.assertRaises(TypeError):
695 def __init__(self):
696 self.items = ["foo", "NormalFoo", "baz"]
698 def __aiter__(self): pass
700 async def __anext__(self): pass
702 def test_aiter_set_return_value(self):
708 self.assertEqual(result, [1, 2, 3])
710 def test_mock_aiter_and_anext_asyncmock(self):
712 instance = self.WithAsyncIterator()
716 self.assertFalse(iscoroutinefunction(instance.__aiter__))
717 self.assertFalse(iscoroutinefunction(mock_instance.__aiter__))
719 self.assertTrue(iscoroutinefunction(instance.__anext__))
720 self.assertTrue(iscoroutinefunction(mock_instance.__anext__))
723 with self.subTest(f"test aiter and anext corourtine with {mock_type}"):
727 def test_mock_async_for(self):
737 mock_instance = mock_type(self.WithAsyncIterator())
738 self.assertEqual(run(iterate(mock_instance)), [])
742 mock_instance = mock_type(self.WithAsyncIterator())
744 self.assertEqual(run(iterate(mock_instance)), expected)
747 mock_instance = mock_type(self.WithAsyncIterator())
749 self.assertEqual(run(iterate(mock_instance)), expected)
752 with self.subTest(f"default value with {mock_type}"):
755 with self.subTest(f"set return_value with {mock_type}"):
758 with self.subTest(f"set return_value iterator with {mock_type}"):
763 def setUp(self):
764 self.mock = AsyncMock()
766 async def _runnable_test(self, *args, **kwargs):
767 await self.mock(*args, **kwargs)
769 async def _await_coroutine(self, coroutine):
772 def test_assert_called_but_not_awaited(self):
774 with assertNeverAwaited(self):
776 self.assertTrue(iscoroutinefunction(mock.async_method))
780 with self.assertRaises(AssertionError):
782 with self.assertRaises(AssertionError):
785 def test_assert_called_then_awaited(self):
791 with self.assertRaises(AssertionError):
794 run(self._await_coroutine(mock_coroutine))
801 def test_assert_called_and_awaited_at_same_time(self):
802 with self.assertRaises(AssertionError):
803 self.mock.assert_awaited()
805 with self.assertRaises(AssertionError):
806 self.mock.assert_called()
808 run(self._runnable_test())
809 self.mock.assert_called_once()
810 self.mock.assert_awaited_once()
812 def test_assert_called_twice_and_awaited_once(self):
817 with assertNeverAwaited(self):
819 with self.assertRaises(AssertionError):
822 run(self._await_coroutine(coroutine))
826 def test_assert_called_once_and_awaited_twice(self):
830 run(self._await_coroutine(coroutine))
831 with self.assertRaises(RuntimeError):
833 run(self._await_coroutine(coroutine))
836 def test_assert_awaited_but_not_called(self):
837 with self.assertRaises(AssertionError):
838 self.mock.assert_awaited()
839 with self.assertRaises(AssertionError):
840 self.mock.assert_called()
841 with self.assertRaises(TypeError):
843 run(self._await_coroutine(self.mock))
845 with self.assertRaises(AssertionError):
846 self.mock.assert_awaited()
847 with self.assertRaises(AssertionError):
848 self.mock.assert_called()
850 def test_assert_has_calls_not_awaits(self):
852 with assertNeverAwaited(self):
853 self.mock('foo')
854 self.mock.assert_has_calls(kalls)
855 with self.assertRaises(AssertionError):
856 self.mock.assert_has_awaits(kalls)
858 def test_assert_has_mock_calls_on_async_mock_no_spec(self):
859 with assertNeverAwaited(self):
860 self.mock()
862 self.assertEqual(self.mock.mock_calls, kalls_empty)
864 with assertNeverAwaited(self):
865 self.mock('foo')
866 with assertNeverAwaited(self):
867 self.mock('baz')
869 self.assertEqual(self.mock.mock_calls, mock_kalls)
871 def test_assert_has_mock_calls_on_async_mock_with_spec(self):
873 with assertNeverAwaited(self):
876 self.assertEqual(a_class_mock.async_method.mock_calls, kalls_empty)
877 self.assertEqual(a_class_mock.mock_calls, [call.async_method()])
879 with assertNeverAwaited(self):
883 self.assertEqual(a_class_mock.async_method.mock_calls, method_kalls)
884 self.assertEqual(a_class_mock.mock_calls, mock_kalls)
886 def test_async_method_calls_recorded(self):
887 with assertNeverAwaited(self):
888 self.mock.something(3, fish=None)
889 with assertNeverAwaited(self):
890 self.mock.something_else.something(6, cake=sentinel.Cake)
892 self.assertEqual(self.mock.method_calls, [
897 self.assertEqual(self.mock.something_else.method_calls,
901 def test_async_arg_lists(self):
906 self.assertIsInstance(attr, _CallList)
907 self.assertIsInstance(attr, list)
908 self.assertEqual(attr, [])
910 assert_attrs(self.mock)
911 with assertNeverAwaited(self):
912 self.mock()
913 with assertNeverAwaited(self):
914 self.mock(1, 2)
915 with assertNeverAwaited(self):
916 self.mock(a=3)
918 self.mock.reset_mock()
919 assert_attrs(self.mock)
922 with assertNeverAwaited(self):
924 with assertNeverAwaited(self):
930 def test_assert_awaited(self):
931 with self.assertRaises(AssertionError):
932 self.mock.assert_awaited()
934 run(self._runnable_test())
935 self.mock.assert_awaited()
937 def test_assert_awaited_once(self):
938 with self.assertRaises(AssertionError):
939 self.mock.assert_awaited_once()
941 run(self._runnable_test())
942 self.mock.assert_awaited_once()
944 run(self._runnable_test())
945 with self.assertRaises(AssertionError):
946 self.mock.assert_awaited_once()
948 def test_assert_awaited_with(self):
950 with self.assertRaisesRegex(AssertionError, msg):
951 self.mock.assert_awaited_with('foo')
953 run(self._runnable_test())
955 with self.assertRaisesRegex(AssertionError, msg):
956 self.mock.assert_awaited_with('foo')
958 run(self._runnable_test('foo'))
959 self.mock.assert_awaited_with('foo')
961 run(self._runnable_test('SomethingElse'))
962 with self.assertRaises(AssertionError):
963 self.mock.assert_awaited_with('foo')
965 def test_assert_awaited_once_with(self):
966 with self.assertRaises(AssertionError):
967 self.mock.assert_awaited_once_with('foo')
969 run(self._runnable_test('foo'))
970 self.mock.assert_awaited_once_with('foo')
972 run(self._runnable_test('foo'))
973 with self.assertRaises(AssertionError):
974 self.mock.assert_awaited_once_with('foo')
976 def test_assert_any_wait(self):
977 with self.assertRaises(AssertionError):
978 self.mock.assert_any_await('foo')
980 run(self._runnable_test('baz'))
981 with self.assertRaises(AssertionError):
982 self.mock.assert_any_await('foo')
984 run(self._runnable_test('foo'))
985 self.mock.assert_any_await('foo')
987 run(self._runnable_test('SomethingElse'))
988 self.mock.assert_any_await('foo')
990 def test_assert_has_awaits_no_order(self):
993 with self.assertRaises(AssertionError) as cm:
994 self.mock.assert_has_awaits(calls)
995 self.assertEqual(len(cm.exception.args), 1)
997 run(self._runnable_test('foo'))
998 with self.assertRaises(AssertionError):
999 self.mock.assert_has_awaits(calls)
1001 run(self._runnable_test('foo'))
1002 with self.assertRaises(AssertionError):
1003 self.mock.assert_has_awaits(calls)
1005 run(self._runnable_test('baz'))
1006 self.mock.assert_has_awaits(calls)
1008 run(self._runnable_test('SomethingElse'))
1009 self.mock.assert_has_awaits(calls)
1011 def test_awaits_asserts_with_any(self):
1013 def __eq__(self, other): pass
1015 run(self._runnable_test(Foo(), 1))
1017 self.mock.assert_has_awaits([call(ANY, 1)])
1018 self.mock.assert_awaited_with(ANY, 1)
1019 self.mock.assert_any_await(ANY, 1)
1021 def test_awaits_asserts_with_spec_and_any(self):
1023 def __eq__(self, other): pass
1035 def test_assert_has_awaits_ordered(self):
1037 with self.assertRaises(AssertionError):
1038 self.mock.assert_has_awaits(calls, any_order=True)
1040 run(self._runnable_test('baz'))
1041 with self.assertRaises(AssertionError):
1042 self.mock.assert_has_awaits(calls, any_order=True)
1044 run(self._runnable_test('bamf'))
1045 with self.assertRaises(AssertionError):
1046 self.mock.assert_has_awaits(calls, any_order=True)
1048 run(self._runnable_test('foo'))
1049 self.mock.assert_has_awaits(calls, any_order=True)
1051 run(self._runnable_test('qux'))
1052 self.mock.assert_has_awaits(calls, any_order=True)
1054 def test_assert_not_awaited(self):
1055 self.mock.assert_not_awaited()
1057 run(self._runnable_test())
1058 with self.assertRaises(AssertionError):
1059 self.mock.assert_not_awaited()
1061 def test_assert_has_awaits_not_matching_spec_error(self):
1064 self.mock = AsyncMock(spec=f)
1065 run(self._runnable_test(1))
1067 with self.assertRaisesRegex(
1073 self.mock.assert_has_awaits([call()])
1074 self.assertIsNone(cm.exception.__cause__)
1076 with self.assertRaisesRegex(
1085 self.mock.assert_has_awaits([call(), call(1, 2)])
1086 self.assertIsInstance(cm.exception.__cause__, TypeError)