[issue28733] Show how to use mock_open in modules other that __main__
New submission from Michał Bultrowicz: Documentation of mock_open doesn't say how to use it in real-life test situations (when you're probably not mocking in __main__). I've spent some time scratching my head and googling for the way to mock-out the "open" function, want to spare other people the hassle. The thing is that "open" needs to be mocked out from the magical "builtins" module, and not from the place of usage (like when mocking everything that's not built-in). So it's not obvious how to do that, especially that the example with __main__ makes it look like the normal mocking approach should work. I still don't fully understand why mocking "__main__.open" can work from interpreter, but that's a different thing... ---------- assignee: docs@python components: Documentation files: mock_open_doc.patch keywords: patch messages: 281114 nosy: butla, docs@python priority: normal severity: normal status: open title: Show how to use mock_open in modules other that __main__ type: enhancement versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7 Added file: http://bugs.python.org/file45533/mock_open_doc.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Berker Peksag added the comment: Thanks for the report and for the patch. I think expanding the "Where to patch" [1] section would be better. We can then refer to it from the mock_open() documentation. [1] https://docs.python.org/3.6/library/unittest.mock.html#where-to-patch ---------- nosy: +berker.peksag, michael.foord stage: -> patch review versions: -Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Michael Foord added the comment: open shouldn't always be patched in builtins, it's much better to patch it in the specific namespace it's being called from. So the doc patch here shouldn't be applied as is. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Michał Bultrowicz added the comment: Then where it should be patched in? Can you give an example? From what I've checked patching only works in __main__ and builtins. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Michał Bultrowicz added the comment: Ok, I've checked again and now patching "file_writer.open" works. I have no idea what I was doing wrong the last time I checked... So I guess I'll close the issue. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Changes by Michał Bultrowicz <michal.bultrowicz@gmail.com>: ---------- status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
Michał Bultrowicz added the comment: One more update - I had the problem, because I was using monkeypatch.setattr() from Pytest, and assumed that it will work the same as patch(). This assumption turned out to be wrong. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue28733> _______________________________________
participants (3)
-
Berker Peksag
-
Michael Foord
-
Michał Bultrowicz