Lines Matching refs:mock
6 from unittest.mock import (
29 mock = Mock()
30 mock(ANY)
31 mock.assert_called_with(ANY)
33 mock = Mock()
34 mock(foo=ANY)
35 mock.assert_called_with(foo=ANY)
43 mock = Mock()
44 mock(datetime.now(), foo=datetime.now())
46 mock.assert_called_with(ANY, foo=ANY)
50 mock = Mock()
59 mock.reset_mock()
61 mock(d, foo=d, bar=d)
62 mock.method(d, zinga=d, alpha=d)
63 mock().method(a1=d, z99=d)
70 self.assertEqual(expected, mock.mock_calls)
71 self.assertEqual(mock.mock_calls, expected)
236 mock = Mock()
237 mock(1, 2, 3)
238 mock(a=3, b=6)
239 self.assertEqual(mock.call_args_list,
247 mock = Mock()
248 mock.foo(1, 2 ,3)
249 mock.bar.baz(a=3, b=6)
250 self.assertEqual(mock.method_calls,
258 mock = MagicMock()
259 mock(1, 2, a=3, b=4)
260 self.assertEqual(mock.call_args, call(1, 2, a=3, b=4))
261 self.assertNotEqual(mock.call_args, call(1, 2, 3))
263 self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)])
264 self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)])
266 mock = MagicMock()
267 mock.foo(1).bar()().baz.beep(a=6)
270 self.assertEqual(mock.mock_calls[-1], last_call)
271 self.assertEqual(mock.mock_calls, last_call.call_list())
291 mock = MagicMock()
292 mock(1)
293 self.assertEqual(call(1).call_list(), mock.mock_calls)
295 mock = MagicMock()
296 mock(1).method(2)
298 mock.mock_calls)
300 mock = MagicMock()
301 mock(1).method(2)(3)
303 mock.mock_calls)
305 mock = MagicMock()
306 int(mock(1).method(2)(3).foo.bar.baz(4)(5))
308 self.assertEqual(kall.call_list(), mock.mock_calls)
337 def _check_someclass_mock(self, mock): argument
338 self.assertRaises(AttributeError, getattr, mock, 'foo')
339 mock.one(1, 2)
340 mock.one.assert_called_with(1, 2)
342 mock.one.assert_called_with, 3, 4)
343 self.assertRaises(TypeError, mock.one, 1)
345 mock.two()
346 mock.two.assert_called_with()
348 mock.two.assert_called_with, 3)
349 self.assertRaises(TypeError, mock.two, 1)
351 mock.three()
352 mock.three.assert_called_with()
354 mock.three.assert_called_with, 3)
355 self.assertRaises(TypeError, mock.three, 3, 2)
357 mock.three(1)
358 mock.three.assert_called_with(1)
360 mock.three(a=1)
361 mock.three.assert_called_with(a=1)
365 mock = create_autospec(SomeClass)
366 self._check_someclass_mock(mock)
367 mock = create_autospec(SomeClass())
368 self._check_someclass_mock(mock)
374 mock = create_autospec(f, return_value='foo')
375 self.assertEqual(mock(), 'foo')
380 mock = create_autospec(Foo, return_value='foo')
381 self.assertEqual(mock(), 'foo')
446 mock = create_autospec(obj)
447 mock.f('bing')
448 mock.f.assert_called_with('bing')
454 mock = create_autospec([])
455 mock.append('foo')
456 mock.append.assert_called_with('foo')
458 self.assertRaises(AttributeError, getattr, mock, 'foo')
463 mock = create_autospec(Foo)
464 mock.foo.append(3)
465 mock.foo.append.assert_called_with(3)
466 self.assertRaises(AttributeError, getattr, mock.foo, 'foo')
475 for mock in (sub_mock, sub_mock.attr):
476 self._check_someclass_mock(mock)
489 mock = create_autospec(BuiltinSubclass)
490 mock.append(3)
491 mock.append.assert_called_with(3)
492 self.assertRaises(AttributeError, getattr, mock.append, 'foo')
494 mock.bar('foo')
495 mock.bar.assert_called_with('foo')
496 self.assertRaises(TypeError, mock.bar, 'foo', 'bar')
497 self.assertRaises(AttributeError, getattr, mock.bar, 'foo')
499 mock.sorted([1, 2])
500 mock.sorted.assert_called_with([1, 2])
501 self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')
503 mock.attr.pop(3)
504 mock.attr.pop.assert_called_with(3)
505 self.assertRaises(AttributeError, getattr, mock.attr, 'foo')
512 mock = create_autospec(Sub)
513 mock.one(1, 2)
514 mock.two()
515 mock.three(3)
518 self.assertEqual(mock.method_calls, expected)
520 mock.attr.one(1, 2)
521 mock.attr.two()
522 mock.attr.three(3)
527 self.assertEqual(mock.method_calls, expected)
534 mock = create_autospec(BuiltinSubclass)
535 self.assertEqual(list(mock), [])
536 self.assertRaises(TypeError, int, mock)
537 self.assertRaises(TypeError, int, mock.attr)
538 self.assertEqual(list(mock), [])
540 self.assertIsInstance(mock['foo'], MagicMock)
541 self.assertIsInstance(mock.attr['foo'], MagicMock)
549 mock = create_autospec(spec, spec_set=True)
550 self._check_someclass_mock(mock)
552 self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')
553 self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')
572 mock = create_autospec(spec)
573 mock.f(1, 2)
574 mock.f.assert_called_once_with(1, 2)
576 mock.g(3, 4)
577 mock.g.assert_called_once_with(3, 4)
588 mock = create_autospec(A)
590 mock()
591 self.assertFalse(mock.B.called)
593 mock.a()
594 mock.B.a()
595 self.assertEqual(mock.method_calls, [call.a(), call.B.a()])
598 self.assertIsNot(mock.foo, mock.bar)
599 mock.foo.lower()
600 self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)
650 mock = create_autospec(Foo)
651 instance = mock()
654 attr_instance = mock.Foo()
658 mock = create_autospec(Foo())
659 self.assertRaises(AttributeError, getattr, mock, 'b')
660 self.assertRaises(TypeError, mock)
663 call_result = mock.Foo()
692 mock = create_autospec(f)
693 self.assertRaises(TypeError, mock)
694 mock(1, 2)
695 mock.assert_called_with(1, 2)
696 mock.assert_called_with(1, b=2)
697 mock.assert_called_with(a=1, b=2)
700 mock = create_autospec(f)
701 self.assertRaises(TypeError, mock.f)
702 mock.f(3, 4)
703 mock.f.assert_called_with(3, 4)
704 mock.f.assert_called_with(a=3, b=4)
735 mock = create_autospec(Foo)
737 self.assertRaises(TypeError, mock)
738 mock(1)
739 mock.assert_called_once_with(1)
740 mock.assert_called_once_with(a=1)
741 self.assertRaises(AssertionError, mock.assert_called_once_with, 2)
743 mock(4, 5)
744 mock.assert_called_with(4, 5)
745 mock.assert_called_with(a=4, b=5)
746 self.assertRaises(AssertionError, mock.assert_called_with, a=5, b=4)
764 mock = create_autospec(Callable)
765 mock(1, 2)
766 mock.assert_called_once_with(1, 2)
767 mock.assert_called_once_with(x=1, y=2)
768 self.assertRaises(TypeError, mock, 'a')
770 instance = mock(1, 2)
779 mock = create_autospec(Callable(1, 2))
780 mock(a='a')
781 mock.assert_called_once_with(a='a')
782 self.assertRaises(TypeError, mock)
783 mock('a')
784 mock.assert_called_with('a')
792 mock = create_autospec(NonCallable)
793 instance = mock()
794 mock.assert_called_once_with()
795 self.assertRaises(TypeError, mock, 'a')
799 mock = create_autospec(NonCallable())
800 self.assertRaises(TypeError, mock)
801 self.assertRaises(TypeError, mock, 'a')
808 mock = create_autospec(Foo)
809 none = mock.bar
910 mock = create_autospec(myfunc)
911 mock(1, 2)
912 mock(x=1, y=2)
914 self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(myfunc))
915 self.assertEqual(mock.mock_calls, [call(1, 2), call(x=1, y=2)])
916 self.assertRaises(TypeError, mock, 1)
924 mock = create_autospec(foo)
925 mock(1, 2, c=3)
926 mock(1, c=3)
928 self.assertEqual(inspect.getfullargspec(mock), inspect.getfullargspec(foo))
929 self.assertEqual(mock.mock_calls, [call(1, 2, c=3), call(1, c=3)])
930 self.assertRaises(TypeError, mock, 1)
931 self.assertRaises(TypeError, mock, 1, 2, 3, c=4)
937 mock = Mock()
938 self.assertIsInstance(mock.call_args_list, _CallList)
940 mock(1, 2)
941 mock(a=3)
942 mock(3, 4)
943 mock(b=6)
946 self.assertIn(kall, mock.call_args_list)
949 self.assertIn(calls, mock.call_args_list)
951 self.assertIn(calls, mock.call_args_list)
953 self.assertIn(calls, mock.call_args_list)
955 self.assertIn(calls, mock.call_args_list)
957 self.assertNotIn(call('fish'), mock.call_args_list)
958 self.assertNotIn([call('fish')], mock.call_args_list)
962 mock = Mock()
963 mock(1, 2)
964 mock.foo(a=3)
965 mock.foo.bar().baz('fish', cat='dog')
973 self.assertEqual(str(mock.mock_calls), expected)
978 mock = p.start()
981 mock.assert_called_once_with()
985 mock.assert_called_with()
986 self.assertEqual(mock.mock_calls, [call(), call()])
989 self.assertEqual(mock.mock_calls, [call(), call(), call(3)])