@@ -1031,6 +1031,12 @@ patch
10311031 default because it can be dangerous. With it switched on you can write
10321032 passing tests against APIs that don't actually exist!
10331033
1034+ .. note ::
1035+
1036+ .. versionchanged :: 3.5
1037+ If you are patching builtins in a module then you don't
1038+ need to pass `create=True `, it will be added by default.
1039+
10341040 Patch can be used as a `TestCase ` class decorator. It works by
10351041 decorating each test method in the class. This reduces the boilerplate
10361042 code when your test methods share a common patchings set. `patch ` finds
@@ -1401,6 +1407,21 @@ It is also possible to stop all patches which have been started by using
14011407
14021408 Stop all active patches. Only stops patches started with `start `.
14031409
1410+ .. patch-builtins:
1411+
1412+ patch builtins
1413+ ~~~~~~~~~~~~~~~
1414+ You can patch any builtins within a module. The following example patches
1415+ builtin `ord `:
1416+
1417+ >>> @ patch(' __main__.ord' )
1418+ ... def test (mock_ord ):
1419+ ... mock_ord.return_value = 101
1420+ ... print (ord (' c' ))
1421+ ...
1422+ >>> test()
1423+ 101
1424+
14041425
14051426TEST_PREFIX
14061427~~~~~~~~~~~
@@ -2011,7 +2032,7 @@ Mocking context managers with a :class:`MagicMock` is common enough and fiddly
20112032enough that a helper function is useful.
20122033
20132034 >>> m = mock_open()
2014- >>> with patch(' __main__.open' , m, create = True ):
2035+ >>> with patch(' __main__.open' , m):
20152036 ... with open (' foo' , ' w' ) as h:
20162037 ... h.write(' some stuff' )
20172038 ...
@@ -2026,7 +2047,7 @@ enough that a helper function is useful.
20262047
20272048And for reading files:
20282049
2029- >>> with patch(' __main__.open' , mock_open(read_data = ' bibble' ), create = True ) as m:
2050+ >>> with patch(' __main__.open' , mock_open(read_data = ' bibble' )) as m:
20302051 ... with open (' foo' ) as h:
20312052 ... result = h.read()
20322053 ...
0 commit comments