[TIP] Mock and csv reader
flavyobr at gmail.com
Mon Jul 8 02:36:46 PDT 2013
The only way to print the rows in the code below is if I use f.read() as
input to csv.reader().
I really don't know how to test my function as I just pass the file handle
to the csv.reader().
Do you know the reason? How can I sort it out?
my_mock = mock.MagicMock(spec=file)
with mock.patch('__builtin__.open', my_mock):
manager = my_mock.return_value.__enter__.return_value
manager.read.return_value = StringIO("my,example,input")
with open('x') as f:
reader = csv.reader(f)
for row in reader:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python