Skip to content

Instantly share code, notes, and snippets.

@curzona
Created January 22, 2015 06:04
Show Gist options
  • Select an option

  • Save curzona/96bac22da2de3191c2d8 to your computer and use it in GitHub Desktop.

Select an option

Save curzona/96bac22da2de3191c2d8 to your computer and use it in GitHub Desktop.
unittest with mock __builtin__.open
import mock
import unittest
class TestOpen(unittest.TestCase):
def test_mock_open(self):
m = mock.patch('__builtin__.open',
mock.mock_open(read_data="some text"))
m.start()
with open('somefile.txt', 'rb') as fin:
text = fin.read()
assert text == "some text"
m.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment