Skip to main content

A subclass of MagicMock that copies the arguments

Project description

travis coveralls pypi pyversions womm

MagicMock has problems when used on a function with mutable arguments. This is called out in the documentation:

Another situation is rare, but can bite you, is when your mock is called with mutable arguments. call_args and call_args_list store references to the arguments. If the arguments are mutated by the code under test then you can no longer make assertions about what the values were when the mock was called.

They then go on to propose a workaround, using side_effect, but it’s not very likable. There is also an elegant recipe offered which copies arguments at call-time. It’s simply a subclass of MagicMock which copies the arguments, instead of storing references. I’m not sure why the recipe wasn’t included directly in mock, so here it is as a third-party package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for copyingmock, version 0.2
Filename, size File type Python version Upload date Hashes
Filename, size copyingmock-0.2-py2.py3-none-any.whl (3.8 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size copyingmock-0.2.tar.gz (2.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page