Mocking open ?

999 views
Skip to first unread message

munda....@gmail.com

unread,
Mar 11, 2012, 10:42:24 AM3/11/12
to mockito...@googlegroups.com
Hello everyone !

Thanks for this project. It seems to be very useful !

A simple question :

With python-mock, it's possible to mock built-in function line `open` using MagickMock :


How can I do the same thing using python-mockito ?

Thanks.

Kaste

unread,
Apr 1, 2012, 10:59:56 AM4/1/12
to mockito-python
should be:


import __builtin__
when(__builtin__).open('filename').thenReturn(...)
(...)
unstub()

sayor...@gmail.com

unread,
Nov 26, 2012, 4:33:10 PM11/26/12
to mockito...@googlegroups.com, munda....@gmail.com
Thanks for suggesting the solution!

Though when I try to use it as:

 when( __builtin__).open('/mnt/test_share/test_ver/latest/test.txt',"r").thenReturn('test_result')
---method call here, which internally calls the open()---
unstub()

where 'open' is used in the following way:

    fh = open(file_name, "r")
    res = fh.read()
    fh.close()

I get an error when running the unit test. The last message in the trace is for the linecache.py in Python lib at lines = fp.readlines()
AttributeError: 'NoneType' object has no attribute 'readlines'

I am new to Python and mockito, so not sure if I am missing something. Any suggestions?

Caspar Duregger

unread,
Nov 27, 2012, 6:25:39 AM11/27/12
to mockito...@googlegroups.com

Instead of returning a string, you need to return a file-like object; usually a StringIO thing. At least something that implements readlines() in your case.

--
You received this message because you are subscribed to the Google Groups "mockito-python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito-python/-/L5V8Q4ol048J.
To post to this group, send email to mockito...@googlegroups.com.
To unsubscribe from this group, send email to mockito-pytho...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito-python?hl=en.

sayor...@gmail.com

unread,
Nov 28, 2012, 3:43:58 PM11/28/12
to mockito...@googlegroups.com, munda....@gmail.com
Thanks Caspar, that makes sense!

But, on trying with StringIO and even trying to create a tempfile with lines as below I still get the same error. I am attaching the complete stacktrace if that helps.

f = tempfile.NamedTemporaryFile()
f.write('test data\n')
f.write('more test data\n')
when(__builtin__).open('/mnt/test_share/test_ver/latest/test.txt',"r").thenReturn(f)


And

out = StringIO.StringIO()
out.write('test_result_line1\n')
out.write('test_result_line2\n')
out.close()
when(__builtin__).open('/mnt/test_share/test_ver/latest/test.txt',"r").thenReturn(out)

On Sunday, 11 March 2012 07:42:24 UTC-7, munda....@gmail.com wrote:
errortrace.txt

Caspar Duregger

unread,
Nov 28, 2012, 4:57:22 PM11/28/12
to mockito-python
The "'NoneType' object has no attribute 'readlines'" usually means the mocking didn't work. You need to check the arguments for open(...). They have to be _exactly_ the same when preparing the mock and when you use it.

After that keep in mind that the default StringIO doesn't have a readlines() method either. You have to subclass it.

Regards


2012/11/28 <sayor...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mockito-python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito-python/-/P08ixJ4g0e0J.

Win Treese

unread,
Nov 29, 2012, 9:53:55 AM11/29/12
to munda....@gmail.com, mockito...@googlegroups.com

On Nov 28, 2012, at 3:43 PM, sayor...@gmail.com wrote:

> Thanks Caspar, that makes sense!
>
> But, on trying with StringIO and even trying to create a tempfile with lines as below I still get the same error. I am attaching the complete stacktrace if that helps.
>
> f = tempfile.NamedTemporaryFile()
> f.write('test data\n')
> f.write('more test data\n')
> when(__builtin__).open('/mnt/test_share/test_ver/latest/test.txt',"r").thenReturn(f)
>
>
> And
>
> out = StringIO.StringIO()
> out.write('test_result_line1\n')
> out.write('test_result_line2\n')
> out.close()
> when(__builtin__).open('/mnt/test_share/test_ver/latest/test.txt',"r").thenReturn(out)

I think you want to create the string to use and then create a StringIO object using for input instead of building it as output. For example:

from mockito import when
import StringIO
import __builtin__

x = 'hi\nthere\njoe\n'
f = StringIO.StringIO(x)
when(__builtin__).open('/tmp/foo').thenReturn(f)
print f.readlines()

=>
['hi\n', 'there\n', 'joe\n']


(This is with Python 2.7.3 on Mac OS Mountain Lion).

Good luck with it!

- Win

Reply all
Reply to author
Forward
0 new messages