code_junkie

Unit Testing File Modifications

Nov 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. def fake_open(module):
  2. """Patch module's `open` builtin so that it returns StringIOs instead of
  3. creating real files, which is useful for testing. Returns a dict that maps
  4. opened file names to StringIO objects."""
  5. from contextlib import closing
  6. from StringIO import StringIO
  7. streams = {}
  8. def fakeopen(filename,mode):
  9. stream = StringIO()
  10. stream.close = lambda: None
  11. streams[filename] = stream
  12. return closing(stream)
  13. module.open = fakeopen
  14. return streams
Add Comment
Please, Sign In to add comment