diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index ed00ee6d0c2d83..a3eb3b3db5bf4f 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2213,6 +2213,16 @@ And for reading files:: >>> m.assert_called_once_with('foo') >>> assert result == 'bibble' +If you want to mock open for use in another module you can mock +builtins.open: + + >>> with patch('builtins.open', mock_open(read_data='bibble')) as m: + ... with open('foo') as h: + ... result = h.read() + ... + >>> m.assert_called_once_with('foo') + >>> assert result == 'bibble' + .. _auto-speccing: diff --git a/Misc/NEWS.d/next/Documentation/2019-04-10-14-47-17.bpo-33722.UwwygU.rst b/Misc/NEWS.d/next/Documentation/2019-04-10-14-47-17.bpo-33722.UwwygU.rst new file mode 100644 index 00000000000000..4cbd50fbd8adee --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-04-10-14-47-17.bpo-33722.UwwygU.rst @@ -0,0 +1,2 @@ +Add in example to unittest.mock.rst showing how to mock builtins.open in +cases where builtins.open is mocked in another module.