Tobias,<br><br>pymock definitely took me a bit of getting used to. Here's example code to test your Example class; I've tried commenting it to make evident some of the things I had a hard time understanding when I first started using pymock.
<br><br>Note that while I believe this works, it may not be the best way to go about doing things--suggestions from more experienced pymock users are welcome.<br><br>"""<br>import pymock<br><br>c = pymock.Controller
()<br><br># Override the built-in open() function.<br>c.override( __builtins__, "open" )<br><br># Create a mock file object, which our mock open() will return.<br>mockFile = c.mock()<br><br># The first parameter here looks a little weird; we've already
<br># overridden open() with a mock object, so when we call open() here,<br># we're "recording" the call, not actually calling open().<br>c.expectAndReturn( open("myfile.conf"), mockFile )<br><br># This looks a little weird: we're actually "recording" the calling of
<br>
# mockFile.close() here, we're not actually calling it!<br>
mockFile.close()<br>
<br># We're done "recording" the calls that our mocked objects/functions<br># will receive; now prepare for them to be "replayed" by our<br># production code.<br>c.replay()<br><br># Call our real code, which will ultimately call into our mock
<br># objects/functions.<br>Example( "myfile.conf" )<br><br># Verify that all the calls we told our mock objects/functions to<br># expect were actually "played back" properly.<br>c.verify()<br>"""
<br><br>Note also that in particular regard to the topic of mocking or stubbing out built-ins, you may also find this Google Testing in the Toilet article useful:<br><br> <a href="http://googletesting.blogspot.com/2007/01/better-stubbing-in-python.html">
http://googletesting.blogspot.com/2007/01/better-stubbing-in-python.html</a><br><br>I hope this helps.<br><br>- Atul<br><br>On 9/7/07, <b class="gmail_sendername">Tobias Roth</b> <<a href="mailto:misc.lists@fsck.ch">misc.lists@fsck.ch
</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi<br><br>I am struggling with the basics of pymock. Here is a simplified example
<br>class I'd like to test:<br><br>class Example(object):<br> def __init__(self, cf):<br> self.conffile = cf<br> f = open(self.conffile)<br> # read and assign<br>
f.close()<br><br>What I'd like to test is whether the constructor correctly opens a file<br>and reads and assigns its contents to some member variables.<br><br>What I'd do with conventional unit testing would be to create a fake
<br>conffile with some data, have it read by __init__ and then compare what<br>has been read with what I set up.<br><br>Now what I imagined to do with pymock was to stub out<br>__builtins__.open(). But I could't get pymock to handle the
<br>self.conffile argument to open() correctly.<br><br><br>I feel like I am looking at the whole pymock testing from the wrong way,<br>since I can't get such a simple example to work. Can someone enlighten me?<br><br>
Thanks,<br>Tobias<br><br>_______________________________________________<br>testing-in-python mailing list<br><a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br><a href="http://lists.idyll.org/listinfo/testing-in-python">
http://lists.idyll.org/listinfo/testing-in-python</a><br></blockquote></div><br>