MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / patch

Function patch

tools/python-3.11.9-amd64/Lib/unittest/mock.py:1698–1777  ·  view source on GitHub ↗

`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

(
        target, new=DEFAULT, spec=None, create=False,
        spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs
    )

Source from the content-addressed store, hash-verified

1696
1697
1698def patch(
1699 target, new=DEFAULT, spec=None, create=False,
1700 spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs
1701 ):
1702 """
1703 `patch` acts as a function decorator, class decorator or a context
1704 manager. Inside the body of the function or with statement, the `target`
1705 is patched with a `new` object. When the function/with statement exits
1706 the patch is undone.
1707
1708 If `new` is omitted, then the target is replaced with an
1709 `AsyncMock if the patched object is an async function or a
1710 `MagicMock` otherwise. If `patch` is used as a decorator and `new` is
1711 omitted, the created mock is passed in as an extra argument to the
1712 decorated function. If `patch` is used as a context manager the created
1713 mock is returned by the context manager.
1714
1715 `target` should be a string in the form `'package.module.ClassName'`. The
1716 `target` is imported and the specified object replaced with the `new`
1717 object, so the `target` must be importable from the environment you are
1718 calling `patch` from. The target is imported when the decorated function
1719 is executed, not at decoration time.
1720
1721 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
1722 if patch is creating one for you.
1723
1724 In addition you can pass `spec=True` or `spec_set=True`, which causes
1725 patch to pass in the object being mocked as the spec/spec_set object.
1726
1727 `new_callable` allows you to specify a different class, or callable object,
1728 that will be called to create the `new` object. By default `AsyncMock` is
1729 used for async functions and `MagicMock` for the rest.
1730
1731 A more powerful form of `spec` is `autospec`. If you set `autospec=True`
1732 then the mock will be created with a spec from the object being replaced.
1733 All attributes of the mock will also have the spec of the corresponding
1734 attribute of the object being replaced. Methods and functions being
1735 mocked will have their arguments checked and will raise a `TypeError` if
1736 they are called with the wrong signature. For mocks replacing a class,
1737 their return value (the 'instance') will have the same spec as the class.
1738
1739 Instead of `autospec=True` you can pass `autospec=some_object` to use an
1740 arbitrary object as the spec instead of the one being replaced.
1741
1742 By default `patch` will fail to replace attributes that don't exist. If
1743 you pass in `create=True`, and the attribute doesn't exist, patch will
1744 create the attribute for you when the patched function is called, and
1745 delete it again afterwards. This is useful for writing tests against
1746 attributes that your production code creates at runtime. It is off by
1747 default because it can be dangerous. With it switched on you can write
1748 passing tests against APIs that don't actually exist!
1749
1750 Patch can be used as a `TestCase` class decorator. It works by
1751 decorating each test method in the class. This reduces the boilerplate
1752 code when your test methods share a common patchings set. `patch` finds
1753 tests by looking for method names that start with `patch.TEST_PREFIX`.
1754 By default this is `test`, which matches the way `unittest` finds tests.
1755 You can specify an alternative prefix by setting `patch.TEST_PREFIX`.

Callers 15

test_asyncMethod · 0.90
test_asyncMethod · 0.90
test_patch_start_stopMethod · 0.90
test_stop_idempotentMethod · 0.90
test_autospecMethod · 0.90

Calls 2

_get_targetFunction · 0.85
_patchClass · 0.70

Tested by 15

test_asyncMethod · 0.72
test_asyncMethod · 0.72
test_patch_start_stopMethod · 0.72
test_stop_idempotentMethod · 0.72
test_autospecMethod · 0.72