[New-bugs-announce] [issue44102] mock_open does not support the use of 'seek()'

Niko report at bugs.python.org
Mon May 10 07:32:27 EDT 2021


New submission from Niko <nikle at cendio.com>:

Using 'mock_open' to unit test a code that uses the 'seek()' function for the file object does not work. This would be a great addition for mock_open functionality as this use case happens occasionally.

Test file contains:
def test_one(self):
    with patch("builtins.open", mock_open(read_data="#123")):
        script.main()

Example script:
def main():
    with open('some/file', 'r') as f:
        print(f.read(1))
        f.seek(0)
        print(f.read(1))

if __name__ == "__main__":
    main()

Output will be:
#
1

While the expected output is:
#
#

----------
messages: 393378
nosy: nikle
priority: normal
severity: normal
status: open
title: mock_open does not support the use of 'seek()'
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44102>
_______________________________________


More information about the New-bugs-announce mailing list