[TIP] Mock and csv reader

Sean Fisk sean at seanfisk.com
Tue Jul 9 16:05:17 PDT 2013

Hi Flavio,

Exactly what function are you trying to test? I assume it is the with
open(...) block, but that's not currently in a different function. I think
I could better understand your problem if you posted your test code and
your function under test.


On Mon, Jul 8, 2013 at 3:36 AM, Flavio Oliveira <flavyobr at gmail.com> wrote:

> 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
> Flavio
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
> --
Sean Fisk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130709/54623cc2/attachment.htm>

More information about the testing-in-python mailing list