Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacement for unittest.mock.mock_open #69876

Open
NivBen-David mannequin opened this issue Nov 21, 2015 · 5 comments
Open

Replacement for unittest.mock.mock_open #69876

NivBen-David mannequin opened this issue Nov 21, 2015 · 5 comments
Labels
stdlib Python modules in the Lib dir tests Tests in the Lib/test dir type-feature A feature request or enhancement

Comments

@NivBen-David
Copy link
Mannequin

NivBen-David mannequin commented Nov 21, 2015

BPO 25690
Nosy @rbtcollins, @ezio-melotti, @bitdancer, @voidspace
Files
  • mock_open.patch
  • mock_open.patch
  • mock_open.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2015-11-21.16:23:03.721>
    labels = ['tests', 'type-feature', 'library']
    title = 'Replacement for unittest.mock.mock_open'
    updated_at = <Date 2016-03-15.19:54:14.977>
    user = 'https://bugs.python.org/NivBen-David'

    bugs.python.org fields:

    activity = <Date 2016-03-15.19:54:14.977>
    actor = 'Niv Ben-David'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)', 'Tests']
    creation = <Date 2015-11-21.16:23:03.721>
    creator = 'Niv Ben-David'
    dependencies = []
    files = ['41113', '41762', '42175']
    hgrepos = []
    issue_num = 25690
    keywords = ['patch']
    message_count = 5.0
    messages = ['255061', '255078', '259268', '261714', '261825']
    nosy_count = 5.0
    nosy_names = ['rbcollins', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'Niv Ben-David']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25690'
    versions = ['Python 3.6']

    @NivBen-David
    Copy link
    Mannequin Author

    NivBen-David mannequin commented Nov 21, 2015

    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

    @NivBen-David NivBen-David mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 21, 2015
    @SilentGhost SilentGhost mannequin added the tests Tests in the Lib/test dir label Nov 21, 2015
    @bitdancer
    Copy link
    Member

    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.

    @NivBen-David
    Copy link
    Mannequin Author

    NivBen-David mannequin commented Jan 30, 2016

    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.

    @rbtcollins
    Copy link
    Member

    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.

    @NivBen-David
    Copy link
    Mannequin Author

    NivBen-David mannequin commented Mar 15, 2016

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir tests Tests in the Lib/test dir type-feature A feature request or enhancement
    Projects
    Status: No status
    Development

    No branches or pull requests

    2 participants