The magic_patch is a simple library that provides the ability to patch an object in different scopes at once. Now you can patch objects easily like magic.
Project description
The magic_patch is a simple library that provides the ability to patch an object in different scopes at once. Now you can patch objects easily like magic.
- Example 1 * A project contains a few modules that imports datetime library
# module 1
from datetime import datetime
...
# module 2
from datetime import datetime
...
Using magic_patch you don't need to patch each module manually
# test
from unittest.mock import Mock
from magic_patch import magic_patch
from datetime import datetime # import target object
from ... import start_module # necessary to run imports
datetime_mock = Mock()
with magic_patch(start_module, datetime, datetime_mock):
# all done! - all modules patched!!!
...
- Example 2 * A project contains a few modules that import the datetime class from datatime library There are different names for the target object. The magic_patch provides the ability to examine all available variables in each module
# module 1
from datetime import datetime
...
# module 2
from datetime import datetime as new_name_datetime
...
Just set up the option search_by_all_names=True and magic_patch finds your target with other names
# test
from unittest.mock import Mock
from magic_patch import magic_patch
from datetime import datetime # import target object
from ... import start_module # necessary to run imports
datetime_mock = Mock()
with magic_patch(start_module, datetime, datetime_mock, search_by_all_names=True):
# all done! - all modules patched!!!
...
Happy magic patching!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
magic_patch-0.0.1.tar.gz
(3.1 kB
view hashes)
Built Distribution
Close
Hashes for magic_patch-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d120c683f44bc78212d2fa886f11fe3f814eca9cc516296f2fd12742880ee1d |
|
MD5 | f79071fd0da6adb3739b7fcc555c5bd2 |
|
BLAKE2b-256 | d4e2128bcbb7e954bfb76d21ca1587ece246553e7cd1c5b7073d5426899f1189 |