@@ -1572,15 +1572,36 @@ patch.dict
1572
1572
:func: `patch.dict ` can also be called with arbitrary keyword arguments to set
1573
1573
values in the dictionary.
1574
1574
1575
- :func: `patch.dict ` can be used as a context manager, decorator or class
1576
- decorator. When used as a class decorator :func: `patch.dict ` honours
1577
- ``patch.TEST_PREFIX `` for choosing which methods to wrap.
1578
-
1579
1575
.. versionchanged :: 3.8
1580
1576
1581
1577
:func: `patch.dict ` now returns the patched dictionary when used as a context
1582
1578
manager.
1583
1579
1580
+ :func: `patch.dict ` can be used as a context manager, decorator or class
1581
+ decorator:
1582
+
1583
+ >>> foo = {}
1584
+ >>> @ patch.dict(foo, {' newkey' : ' newvalue' })
1585
+ ... def test ():
1586
+ ... assert foo == {' newkey' : ' newvalue' }
1587
+ >>> test()
1588
+ >>> assert foo == {}
1589
+
1590
+ When used as a class decorator :func: `patch.dict ` honours
1591
+ ``patch.TEST_PREFIX `` (default to ``'test' ``) for choosing which methods to wrap:
1592
+
1593
+ >>> import os
1594
+ >>> import unittest
1595
+ >>> from unittest.mock import patch
1596
+ >>> @ patch.dict(' os.environ' , {' newkey' : ' newvalue' })
1597
+ ... class TestSample (unittest .TestCase ):
1598
+ ... def test_sample (self ):
1599
+ ... self .assertEqual(os.environ[' newkey' ], ' newvalue' )
1600
+
1601
+ If you want to use a different prefix for your test, you can inform the
1602
+ patchers of the different prefix by setting ``patch.TEST_PREFIX ``. For
1603
+ more details about how to change the value of see :ref: `test-prefix `.
1604
+
1584
1605
:func: `patch.dict ` can be used to add members to a dictionary, or simply let a test
1585
1606
change a dictionary, and ensure the dictionary is restored when the test
1586
1607
ends.
@@ -1793,6 +1814,8 @@ builtin :func:`ord`::
1793
1814
101
1794
1815
1795
1816
1817
+ .. _test-prefix :
1818
+
1796
1819
TEST_PREFIX
1797
1820
~~~~~~~~~~~
1798
1821
0 commit comments