Perform multiple patches in a single call. It takes the object to be patched (either as an object or a string to fetch the object by importing) and keyword arguments for the patches:: with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'): ... Use `DEF
(target, spec=None, create=False, spec_set=None,
autospec=None, new_callable=None, **kwargs)
| 1720 | |
| 1721 | |
| 1722 | def _patch_multiple(target, spec=None, create=False, spec_set=None, |
| 1723 | autospec=None, new_callable=None, **kwargs): |
| 1724 | """Perform multiple patches in a single call. It takes the object to be |
| 1725 | patched (either as an object or a string to fetch the object by importing) |
| 1726 | and keyword arguments for the patches:: |
| 1727 | |
| 1728 | with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'): |
| 1729 | ... |
| 1730 | |
| 1731 | Use `DEFAULT` as the value if you want `patch.multiple` to create |
| 1732 | mocks for you. In this case the created mocks are passed into a decorated |
| 1733 | function by keyword, and a dictionary is returned when `patch.multiple` is |
| 1734 | used as a context manager. |
| 1735 | |
| 1736 | `patch.multiple` can be used as a decorator, class decorator or a context |
| 1737 | manager. The arguments `spec`, `spec_set`, `create`, |
| 1738 | `autospec` and `new_callable` have the same meaning as for `patch`. These |
| 1739 | arguments will be applied to *all* patches done by `patch.multiple`. |
| 1740 | |
| 1741 | When used as a class decorator `patch.multiple` honours `patch.TEST_PREFIX` |
| 1742 | for choosing which methods to wrap. |
| 1743 | """ |
| 1744 | if type(target) is str: |
| 1745 | getter = partial(pkgutil.resolve_name, target) |
| 1746 | else: |
| 1747 | getter = lambda: target |
| 1748 | |
| 1749 | if not kwargs: |
| 1750 | raise ValueError( |
| 1751 | 'Must supply at least one keyword argument with patch.multiple' |
| 1752 | ) |
| 1753 | # need to wrap in a list for python 3, where items is a view |
| 1754 | items = list(kwargs.items()) |
| 1755 | attribute, new = items[0] |
| 1756 | patcher = _patch( |
| 1757 | getter, attribute, new, spec, create, spec_set, |
| 1758 | autospec, new_callable, {} |
| 1759 | ) |
| 1760 | patcher.attribute_name = attribute |
| 1761 | for attribute, new in items[1:]: |
| 1762 | this_patcher = _patch( |
| 1763 | getter, attribute, new, spec, create, spec_set, |
| 1764 | autospec, new_callable, {} |
| 1765 | ) |
| 1766 | this_patcher.attribute_name = attribute |
| 1767 | patcher.additional_patchers.append(this_patcher) |
| 1768 | return patcher |
| 1769 | |
| 1770 | |
| 1771 | def patch( |