`patch` acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the `target` is patched with a `new` object. When the function/with statement exits the patch is undone. If `new` is omitted, then the target is replac
(
target, new=DEFAULT, spec=None, create=False,
spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs
)
| 1769 | |
| 1770 | |
| 1771 | def patch( |
| 1772 | target, new=DEFAULT, spec=None, create=False, |
| 1773 | spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs |
| 1774 | ): |
| 1775 | """ |
| 1776 | `patch` acts as a function decorator, class decorator or a context |
| 1777 | manager. Inside the body of the function or with statement, the `target` |
| 1778 | is patched with a `new` object. When the function/with statement exits |
| 1779 | the patch is undone. |
| 1780 | |
| 1781 | If `new` is omitted, then the target is replaced with an |
| 1782 | `AsyncMock` if the patched object is an async function or a |
| 1783 | `MagicMock` otherwise. If `patch` is used as a decorator and `new` is |
| 1784 | omitted, the created mock is passed in as an extra argument to the |
| 1785 | decorated function. If `patch` is used as a context manager the created |
| 1786 | mock is returned by the context manager. |
| 1787 | |
| 1788 | `target` should be a string in the form `'package.module.ClassName'`. The |
| 1789 | `target` is imported and the specified object replaced with the `new` |
| 1790 | object, so the `target` must be importable from the environment you are |
| 1791 | calling `patch` from. The target is imported when the decorated function |
| 1792 | is executed, not at decoration time. |
| 1793 | |
| 1794 | The `spec` and `spec_set` keyword arguments are passed to the `MagicMock` |
| 1795 | if patch is creating one for you. |
| 1796 | |
| 1797 | In addition you can pass `spec=True` or `spec_set=True`, which causes |
| 1798 | patch to pass in the object being mocked as the spec/spec_set object. |
| 1799 | |
| 1800 | `new_callable` allows you to specify a different class, or callable object, |
| 1801 | that will be called to create the `new` object. By default `AsyncMock` is |
| 1802 | used for async functions and `MagicMock` for the rest. |
| 1803 | |
| 1804 | A more powerful form of `spec` is `autospec`. If you set `autospec=True` |
| 1805 | then the mock will be created with a spec from the object being replaced. |
| 1806 | All attributes of the mock will also have the spec of the corresponding |
| 1807 | attribute of the object being replaced. Methods and functions being |
| 1808 | mocked will have their arguments checked and will raise a `TypeError` if |
| 1809 | they are called with the wrong signature. For mocks replacing a class, |
| 1810 | their return value (the 'instance') will have the same spec as the class. |
| 1811 | |
| 1812 | Instead of `autospec=True` you can pass `autospec=some_object` to use an |
| 1813 | arbitrary object as the spec instead of the one being replaced. |
| 1814 | |
| 1815 | By default `patch` will fail to replace attributes that don't exist. If |
| 1816 | you pass in `create=True`, and the attribute doesn't exist, patch will |
| 1817 | create the attribute for you when the patched function is called, and |
| 1818 | delete it again afterwards. This is useful for writing tests against |
| 1819 | attributes that your production code creates at runtime. It is off by |
| 1820 | default because it can be dangerous. With it switched on you can write |
| 1821 | passing tests against APIs that don't actually exist! |
| 1822 | |
| 1823 | Patch can be used as a `TestCase` class decorator. It works by |
| 1824 | decorating each test method in the class. This reduces the boilerplate |
| 1825 | code when your test methods share a common patchings set. `patch` finds |
| 1826 | tests by looking for method names that start with `patch.TEST_PREFIX`. |
| 1827 | By default this is `test`, which matches the way `unittest` finds tests. |
| 1828 | You can specify an alternative prefix by setting `patch.TEST_PREFIX`. |
searching dependent graphs…