Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mock import mock_open, patch
- # works: consume entire "file"
- with patch('__main__.open', mock_open(read_data='bibble')) as m:
- with open('foo') as h:
- result = h.read()
- assert result == 'bibble' # ok
- # works: consume one line
- with patch('__main__.open', mock_open(read_data='bibblenbobble')) as m:
- with open('foo') as h:
- result = h.readline()
- assert result == 'bibblen' # ok
- # consume first 3 bytes of the "file"
- with patch('__main__.open', mock_open(read_data='bibble')) as m:
- with open('foo') as h:
- result = h.read(3)
- assert result == 'bib', 'result of read: {}'.format(result) # fails
- Traceback (most recent call last):
- File "/tmp/t.py", line 25, in <module>
- assert result == 'bib', 'result of read: {}'.format(result)
- AssertionError: result of read: bibble
Add Comment
Please, Sign In to add comment