Tobias,<br><br>pymock definitely took me a bit of getting used to.&nbsp; Here&#39;s example code to test your Example class; I&#39;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>&quot;&quot;&quot;<br>import pymock<br><br>c = pymock.Controller
()<br><br># Override the built-in open() function.<br>c.override( __builtins__, &quot;open&quot; )<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&#39;ve already
<br># overridden open() with a mock object, so when we call open() here,<br># we&#39;re &quot;recording&quot; the call, not actually calling open().<br>c.expectAndReturn( open(&quot;myfile.conf&quot;), mockFile )<br><br># This looks a little weird: we&#39;re actually &quot;recording&quot; the calling of
<br>
# mockFile.close() here, we&#39;re not actually calling it!<br>
mockFile.close()<br>
<br># We&#39;re done &quot;recording&quot; the calls that our mocked objects/functions<br># will receive; now prepare for them to be &quot;replayed&quot; 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( &quot;myfile.conf&quot; )<br><br># Verify that all the calls we told our mock objects/functions to<br># expect were actually &quot;played back&quot; properly.<br>c.verify()<br>&quot;&quot;&quot;
<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>&nbsp; <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> &lt;<a href="mailto:misc.lists@fsck.ch">misc.lists@fsck.ch
</a>&gt; 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&#39;d like to test:<br><br>class Example(object):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, cf):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.conffile = cf<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f = open(self.conffile)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# read and assign<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
f.close()<br><br>What I&#39;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&#39;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&#39;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&#39;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>