Python unittest.mock.call_args and call_args_list














































Python unittest.mock.call_args and call_args_list



call_args and call_args_list



  • call_args

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, 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).


from

unittest.mock import Mock


mock = Mock(return_value=None)

print(mock.call_args)

None

mock()

print(mock.call_args)

call()

print(mock.call_args == ())

True

mock(3, 4)

print(mock.call_args)

call(3, 4)

print(mock.call_args == ((3, 4),))

True

print(mock.call_args.args)

(3, 4)

print(mock.call_args.kwargs)

{}

mock(3, 4, 5, key='cppsecrets', next='w00t!')

print(mock.call_args)

call(3, 4, 5, key='fish', next='w00t!')

print(mock.call_args.args)

(3, 4, 5)

print(mock.call_args.kwargs)

{'key': 'fish', 'next': 'w00t!'}

call_args, along with members of the lists call_args_list, method_calls and mock_calls are call objects. These are tuples, so they can be unpacked to get at the individual arguments and make more complex assertions. See calls as tuples.

Changes in version 3.8:

Added args and kwargs properties.


  • call_args_list

This is a list of all the calls made to the mock object in sequence (so the length of the list is the number of times it has been called). Before any calls have been made it is an empty list. The call object can be used for conveniently constructing lists of calls to compare with call_args_list.

from unittest.mock import Mock

mock = Mock(return_value=None)
mock()
mock(3, 4)
mock(key='cppsecrets', next='w00t!')
print(mock.call_args_list)
[call(), call(3, 4), call(key='cppsecrets', next='w00t!')]
expected = [(), ((3, 4),), ({'key': 'cppsecrets', 'next': 'w00t!'},)]
print(mock.call_args_list == expected)
True

Members of call_args_list are call objects. These can be unpacked as tuples to get at the individual arguments. See calls as tuples.

<<PREV                                                                                                                                         NEXT>>


More Articles of Shubhanjay Pandey:

Name Views Likes
Python ssl Sockets 998 3
Python unittest.mock side_effect 503 2
Python ssl Example Server Side Operation 771 3
Python unittest.mock method_calls and mock_calls 841 2
Python ssl TLS 1.3 and LibreSSL Support 1450 2
Python unittest.mock The patchers 310 2
Python unittest.mock Attaching Mocks as Attributes 433 2
Python ssl Example Client Side Operations 463 3
Python unittest.mock patch.object and patch.dict 2748 2
Python unittest.mock.call_args and call_args_list 3286 2
Python ssl Certificates 321 3
Python unittest.mock Deleting, Mock Names and the name attribute 517 2
Python unittest.mock FILTER_DIR 254 2
Python ssl Introduction 286 3
Python unitest.mock Where to patch, patching descriptors and proxy objects 268 2
Python ssl Memory BIO Support 539 3
Python mimetypes - Introduction 333 3
Python unittest.mock Helpers call 282 2
Python unittest.mock function assert_*() 283 2
Python unittest.mock Introduction 340 2
Python sunau library 305 2
Python unittest.mock The Mock Class 275 2
Python ssl Constants Part 1 357 3
Python mimetypes.add_type 257 4
Python ssl Socket and Context Creation 1300 3
Python unittest.mock.mock_open 297 2
Python ssl non-blocking sockets 646 3
Python unittest.mock MagicMock and magic method support 847 1
Python unittest.mock Magic Mock 370 2
Python ssl Exceptions 307 3
Python unittest.mock TEST_PREFIX and Nesting Patch Decorators 395 2
Python unittest.mock patch.multiple and builtins 2017 2
Python mimetypes additional data 232 3
Python unittest.mock Other HELPERS 252 2
Python MimeTypes Object 267 3
Python mimetypes.guess_type 332 3
Python unittest.mock Autospeccing 304 2
Python unittest.mock.AsyncMock 2440 2
Python ssl Certificates Handling 375 3
Python ssl Random Generation 290 3
Python unittest.mock Function configure_mock 385 2
Python ssl SSLContexts Part 1 618 3
Python unittest.mock patch methods: start and stop 1693 2
Python unittest.mock Calling 271 2
Python unittest.mock other functions 268 2
Python ssl Constants Part 2 430 3
Python ssl SSLContexts Part 2 764 3
Python ssl Security Considerations and SSL Session 416 3
Python mimetypes.guess_extension 313 3
Python unittest.mock PropertyMock and NonCallableMock 787 2
Python mimetypes additional functions 253 3

Comments