From 9c3c006e01bcbb6abec34eb793153e6be67b64bd Mon Sep 17 00:00:00 2001 From: jacrotts Date: Wed, 10 Apr 2019 14:49:26 -0700 Subject: [PATCH] bpo-33722: Add documentation on mocking builtins.open to unittest.mock.mock_open --- Doc/library/unittest.mock.rst | 10 ++++++++++ .../2019-04-10-14-47-17.bpo-33722.UwwygU.rst | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2019-04-10-14-47-17.bpo-33722.UwwygU.rst 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.