There's a couple of stack overflow posts out there talking about mocking the open call in Python. That's great but it doesn't really help me if a function takes in a file handle or stream object instead of a file path.
One solution I've been using up until now has been cStringIO
objects. I've run into a problem, however.
If I want to test if I'm logging the file name correctly on some sort of failure (say if the file / stream is empty and you expect some kind of data)
cStringIO
fd = cStringIO("")
fd.name = "testing/path" # Throws an AttributeError
I can't set the name attribute since cStringIO
and StringIO
are slotted classes.
If switch over to using open_mock
with mock.patch('__main__.open', mock.mock_open(read_data=''), create=True) as m:
I run into
AttributeError: Mock object has no attribute 'tell'
At this point it feels like I have to use temp files but I'd like to avoid actually calling out to the file system if possible.
How do you test functions that take in file handles without having to create actual files on a file system?