bpo-21269: Provide args and kwargs attributes on mock call objects GH11807

This commit is contained in:
Kumar Akshay 2019-03-22 13:40:40 +05:30 committed by Chris Withers
parent 40b6907b37
commit b0df45e55d
5 changed files with 51 additions and 11 deletions

View file

@ -609,9 +609,11 @@ the *new_callable* argument to :func:`patch`.
This is either ``None`` (if the mock hasn't been called), or the
arguments that the mock was last called with. This will be in the
form of a tuple: the first member is any ordered arguments the mock
was called with (or an empty tuple) and the second member is any
keyword arguments (or an empty dictionary).
form of a tuple: the first member, which can also be accessed through
the ``args`` property, is any ordered arguments the mock was
called with (or an empty tuple) and the second member, which can
also be accessed through the ``kwargs`` property, is any keyword
arguments (or an empty dictionary).
>>> mock = Mock(return_value=None)
>>> print(mock.call_args)
@ -626,9 +628,17 @@ the *new_callable* argument to :func:`patch`.
call(3, 4)
>>> mock.call_args == ((3, 4),)
True
>>> mock.call_args.args
(3, 4)
>>> mock.call_args.kwargs
{}
>>> mock(3, 4, 5, key='fish', next='w00t!')
>>> mock.call_args
call(3, 4, 5, key='fish', next='w00t!')
>>> mock.call_args.args
(3, 4, 5)
>>> mock.call_args.kwargs
{'key': 'fish', 'next': 'w00t!'}
:attr:`call_args`, along with members of the lists :attr:`call_args_list`,
:attr:`method_calls` and :attr:`mock_calls` are :data:`call` objects.
@ -1987,14 +1997,13 @@ arguments are a dictionary:
>>> m = MagicMock(return_value=None)
>>> m(1, 2, 3, arg='one', arg2='two')
>>> kall = m.call_args
>>> args, kwargs = kall
>>> args
>>> kall.args
(1, 2, 3)
>>> kwargs
>>> kall.kwargs
{'arg': 'one', 'arg2': 'two'}
>>> args is kall[0]
>>> kall.args is kall[0]
True
>>> kwargs is kall[1]
>>> kall.kwargs is kall[1]
True
>>> m = MagicMock()