8000 bpo-35685: Add examples of unittest.mock.patch.dict usage by eamanu · Pull Request #11456 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-35685: Add examples of unittest.mock.patch.dict usage #11456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 12, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add samples on patch.dict of the use of decorator and in class
  • Loading branch information
eamanu authored and JulienPalard committed Sep 12, 2019
commit 23cd0194b5a124e761eeb53af853ac4fc22e5597
20 changes: 17 additions & 3 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1572,9 +1572,23 @@ patch.dict
:func:`patch.dict` can also be called with arbitrary keyword arguments to set
values in the dictionary.

:func:`patch.dict` can be used as a context manager, decorator or class
decorator. When used as a class decorator :func:`patch.dict` honours
``patch.TEST_PREFIX`` for choosing which methods to wrap.
:func:`patch.dict` can be used as a context manager, decorator or class
decorator. When used as a class decorator :func:`patch.dict` honours
``patch.TEST_PREFIX`` for choosing which methods to wrap.

>>> foo = {}
>>> @patch.dict(foo, {'newkey': 'newvalue'})
>>> def test():
... assert foo == {'newkey': 'newvalue'}
>>> test()
>>> assert foo == {}

>>> import os
>>> import unittest
>>> @patch.dict('os.environ', {'newkey': 'newvalue'}):
>>> class TestSample(unittest.TestCase):
... def test_sample(self):
... self.assertEqual(os.environ['newkey'], 'newvalue')

.. versionchanged:: 3.8

Expand Down
0