classification
Title: Replacement for unittest.mock.mock_open
Type: enhancement Stage: patch review
Components: Library (Lib), Tests Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Niv Ben-David, ezio.melotti, michael.foord, r.david.murray, rbcollins
Priority: normal Keywords: patch

Created on 2015-11-21 16:23 by Niv Ben-David, last changed 2016-03-15 19:54 by Niv Ben-David.

Files
File name Uploaded Description Edit
mock_open.patch Niv Ben-David, 2015-11-21 16:22 review
mock_open.patch Niv Ben-David, 2016-01-30 20:39 review
mock_open.patch Niv Ben-David, 2016-03-15 19:54 review
Messages (5)
msg255061 - (view) Author: Niv Ben-David (Niv Ben-David) * Date: 2015-11-21 16:22
The unittest.mock module defines a mock_open utility to mock the builtin open function.

During a recent project I found I needed something more. Specifically, mocking different files at the same time, better mocking for operations (like seek, readlines, etc.) among others.

The project can also be found on http://github.com/nivbend/mock-open

Thanks
msg255078 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-22 00:39
Thanks for the submission. I haven't looked at this in detail, but the fact that you are changing existing unit tests indicates there is probably a backward compatibility problem with your patch.  Also if you are adding features I'd expect there to be doc changes, which would make it easier to understand what you are proposing.
msg259268 - (view) Author: Niv Ben-David (Niv Ben-David) * Date: 2016-01-30 20:39
Regarding the documentation changes, my version simply mocks `open` more "closely", so for the most part I can't think of any changes to the documentation. I've added a bit about the mock object acting as a map of file names to mock objects. Should I add anything else?

Regarding the tests, I've tried changing as little as possible to make sure I don't have any backward compatibility issues. However, some changes had to be made:
* By default, `open`'s mode is 'r', the new mock_open imitates this behavior, and so the test shows. Also, calling `open` as a context manager will call `close` in `__exit__`. These are the majority of changes to the testing code.
* test_mock_open_write and test_mock_open_read_with_argument: These functionalities' testing is taken care of by the new mock_open tests.
* test_readlines_data: I removed the `create` argument to `patch`. Must've been by accident. Fixed this in the new patch.

Sorry about the (very) late response, I've been preoccupied lately.
msg261714 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2016-03-14 02:31
Hmm, I haven't looked closely, but some high level thoughts.

I'm worried about making mock too complex here. We already say folk should use a VFS for complex file based tests, and there's quite a chunk of code you're adding - perhaps better to just use a VFS?

I don't like mock importing mock_open and mock_open importing mock: please keep this in a single mock.py at this point.
msg261825 - (view) Author: Niv Ben-David (Niv Ben-David) * Date: 2016-03-15 19:54
I copied the code in place of the old mock_open in unittest/mock.py.

Regarding the VFS issue, I think that it really depends on what you're trying to test. If you only care about "side effects" on the file system, the VFS way it much better and easier. But if you want to test the way files are handled by your code (proper handling of errors during open/read/write seems the obvious thing here, but I can think of other use cases), then I think MockOpen is the better tool.
History
Date User Action Args
2016-03-15 19:54:14Niv Ben-Davidsetfiles: + mock_open.patch

messages: + msg261825
2016-03-14 02:31:17rbcollinssetmessages: + msg261714
2016-01-30 20:39:38Niv Ben-Davidsetfiles: + mock_open.patch

messages: + msg259268
2015-11-22 00:39:50r.david.murraysetnosy: + r.david.murray
messages: + msg255078
2015-11-21 16:26:52SilentGhostsetnosy: + rbcollins, ezio.melotti, michael.foord

components: + Tests
stage: patch review
2015-11-21 16:23:03Niv Ben-Davidcreate