[TIP] Mock and csv reader

Flavio Oliveira 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?

    def test_parse_csv(self):
        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:
                    print row

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130708/766cd736/attachment.html>

More information about the testing-in-python mailing list