diff options
author | Kumar Akshay <k.akshay9721@gmail.com> | 2019-03-22 08:10:40 (GMT) |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-03-22 08:10:40 (GMT) |
commit | b0df45e55dc8304bac0e3cad0225472b84190964 (patch) | |
tree | 14726255b39569c2d0b748bc7393ce2ce2423128 /Lib/unittest | |
parent | 40b6907b377cfc8c4743007894364ac8c5a1c113 (diff) | |
download | cpython-b0df45e55dc8304bac0e3cad0225472b84190964.zip cpython-b0df45e55dc8304bac0e3cad0225472b84190964.tar.gz cpython-b0df45e55dc8304bac0e3cad0225472b84190964.tar.bz2 |
bpo-21269: Provide args and kwargs attributes on mock call objects GH11807
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 16 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testhelpers.py | 9 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 11 |
3 files changed, 33 insertions, 3 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 2ccf0d8..fdde16b 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2135,6 +2135,22 @@ class _Call(tuple): def index(self, *args, **kwargs): return self.__getattr__('index')(*args, **kwargs) + def _get_call_arguments(self): + if len(self) == 2: + args, kwargs = self + else: + name, args, kwargs = self + + return args, kwargs + + @property + def args(self): + return self._get_call_arguments()[0] + + @property + def kwargs(self): + return self._get_call_arguments()[1] + def __repr__(self): if not self._mock_from_kall: name = self._mock_name or 'call' diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py index 745580e..9f1bf26 100644 --- a/Lib/unittest/test/testmock/testhelpers.py +++ b/Lib/unittest/test/testmock/testhelpers.py @@ -146,6 +146,8 @@ class CallTest(unittest.TestCase): self.assertEqual(args, ('foo', (1, 2, 3))) self.assertEqual(args, ('foo', (1, 2, 3), {})) self.assertEqual(args, ((1, 2, 3), {})) + self.assertEqual(args.args, (1, 2, 3)) + self.assertEqual(args.kwargs, {}) def test_named_call_with_args(self): @@ -153,6 +155,8 @@ class CallTest(unittest.TestCase): self.assertEqual(args, ('foo', (1, 2, 3))) self.assertEqual(args, ('foo', (1, 2, 3), {})) + self.assertEqual(args.args, (1, 2, 3)) + self.assertEqual(args.kwargs, {}) self.assertNotEqual(args, ((1, 2, 3),)) self.assertNotEqual(args, ((1, 2, 3), {})) @@ -165,6 +169,8 @@ class CallTest(unittest.TestCase): self.assertEqual(args, ('foo', dict(a=3, b=4))) self.assertEqual(args, ('foo', (), dict(a=3, b=4))) self.assertEqual(args, ((), dict(a=3, b=4))) + self.assertEqual(args.args, ()) + self.assertEqual(args.kwargs, dict(a=3, b=4)) def test_named_call_with_kwargs(self): @@ -172,6 +178,8 @@ class CallTest(unittest.TestCase): self.assertEqual(args, ('foo', dict(a=3, b=4))) self.assertEqual(args, ('foo', (), dict(a=3, b=4))) + self.assertEqual(args.args, ()) + self.assertEqual(args.kwargs, dict(a=3, b=4)) self.assertNotEqual(args, (dict(a=3, b=4),)) self.assertNotEqual(args, ((), dict(a=3, b=4))) @@ -179,6 +187,7 @@ class CallTest(unittest.TestCase): def test_call_with_args_call_empty_name(self): args = _Call(((1, 2, 3), {})) + self.assertEqual(args, call(1, 2, 3)) self.assertEqual(call(1, 2, 3), args) self.assertIn(call(1, 2, 3), [args]) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 2ad90ea..66a5720 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -267,6 +267,10 @@ class MockTest(unittest.TestCase): self.assertEqual(mock.call_count, 1, "call_count incoreect") self.assertEqual(mock.call_args, ((sentinel.Arg,), {}), "call_args not set") + self.assertEqual(mock.call_args.args, (sentinel.Arg,), + "call_args not set") + self.assertEqual(mock.call_args.kwargs, {}, + "call_args not set") self.assertEqual(mock.call_args_list, [((sentinel.Arg,), {})], "call_args_list not initialised correctly") @@ -300,6 +304,8 @@ class MockTest(unittest.TestCase): ]) self.assertEqual(mock.call_args, ((sentinel.Arg,), {"kw": sentinel.Kwarg})) + self.assertEqual(mock.call_args.args, (sentinel.Arg,)) + self.assertEqual(mock.call_args.kwargs, {"kw": sentinel.Kwarg}) # Comparing call_args to a long sequence should not raise # an exception. See issue 24857. @@ -1157,9 +1163,8 @@ class MockTest(unittest.TestCase): mock(2, b=4) self.assertEqual(len(mock.call_args), 2) - args, kwargs = mock.call_args - self.assertEqual(args, (2,)) - self.assertEqual(kwargs, dict(b=4)) + self.assertEqual(mock.call_args.args, (2,)) + self.assertEqual(mock.call_args.kwargs, dict(b=4)) expected_list = [((1,), dict(a=3)), ((2,), dict(b=4))] for expected, call_args in zip(expected_list, mock.call_args_list): |