@@ -1119,13 +1119,13 @@ patch
11191119 Instead of ``autospec=True `` you can pass ``autospec=some_object `` to use an
11201120 arbitrary object as the spec instead of the one being replaced.
11211121
1122- By default :func: `patch ` will fail to replace attributes that don't exist. If
1123- you pass in ``create=True ``, and the attribute doesn't exist, patch will
1124- create the attribute for you when the patched function is called, and
1125- delete it again afterwards . This is useful for writing tests against
1126- attributes that your production code creates at runtime. It is off by
1127- default because it can be dangerous. With it switched on you can write
1128- passing tests against APIs that don't actually exist!
1122+ By default :func: `patch ` will fail to replace attributes that don't exist.
1123+ If you pass in ``create=True ``, and the attribute doesn't exist, patch will
1124+ create the attribute for you when the patched function is called, and delete
1125+ it again after the patched function has exited . This is useful for writing
1126+ tests against attributes that your production code creates at runtime. It is
1127+ off by default because it can be dangerous. With it switched on you can
1128+ write passing tests against APIs that don't actually exist!
11291129
11301130 .. note ::
11311131
@@ -1247,6 +1247,27 @@ into a :func:`patch` call using ``**``::
12471247 ...
12481248 KeyError
12491249
1250+ By default, attempting to patch a function in a module (or a method or an
1251+ attribute in a class) that does not exist will fail with :exc: `AttributeError `::
1252+
1253+ >>> @patch('sys.non_existing_attribute', 42)
1254+ ... def test():
1255+ ... assert sys.non_existing_attribute == 42
1256+ ...
1257+ >>> test()
1258+ Traceback (most recent call last):
1259+ ...
1260+ AttributeError: <module 'sys' (built-in)> does not have the attribute 'non_existing'
1261+
1262+ but adding ``create=True `` in the call to :func: `patch ` will make the previous example
1263+ work as expected::
1264+
1265+ >>> @patch('sys.non_existing_attribute', 42, create=True)
1266+ ... def test(mock_stdout):
1267+ ... assert sys.non_existing_attribute == 42
1268+ ...
1269+ >>> test()
1270+
12501271
12511272patch.object
12521273~~~~~~~~~~~~
0 commit comments