<div>I need help with a real Test Driven concept. I&#39;ve been building up my TDD (not just testing, but real TDD) skills over this past year. I *really* like the book &quot;Growing Object-Oriented Software, Guided by Tests.&quot; It seems to reflect how this makes sense -- growing the architecture, almost organically, using tests to weed, guide, and prune as we go.</div>
<div><br></div><div>I&#39;ve never really used Mocks successfully. Often, I would write a fragile test that actually retrieves data from the web. I liked the idea that if the actual sample data retrieval failed, we would know it and catch it almost immediately. However, doing that makes sense. I can&#39;t keep writing fragile tests.</div>
<div><br></div><div>I am currently refactoring a project and I&#39;d like to build it back up with tests again. Here is a sample skeleton:</div><div><br></div><div><div>class HttpMixIn[snip]</div><div>    &quot;&quot;[snip]&quot;&quot;&quot;</div>
<div><br></div><div><div>    def fetch(self, url):</div><div>        &quot;&quot;&quot;Fetch page, return result as large string.&quot;&quot;&quot;</div><div>        [snip]</div><div>        try:</div><div>            response = urllib2.urlopen(url, timeout=settings.[snip])</div>
<div>            response = unicode(response.read(), errors=&#39;ignore&#39;)</div><div>            [snip]</div><div>        except urllib2.HTTPError, e:</div><div>            [snip]</div><div>        except ValueError, e:</div>
<div>            [snip]</div><div>        except urllib2.URLError, e:</div><div>            [snip]</div><div>        except Exception, e:</div><div>            [snip]</div><div><br></div><div>        return response</div>
</div></div><div><br></div><div>Obviously, to test this, I would like to try it against a &quot;website&quot; that has a 404 and see how well it responds. Similarly, a &quot;website&quot; that has a &quot;500.&quot; Also, what about time out issues? And, what about retries that fail -- could we retry a few times and, thus, come up with a &quot;website&quot; that &quot;fails&quot; two times and then is successful? Also, look at the &quot;response.read()&quot; that is in this code. What about pages that have larger content than is normally stored in the buffer? (i.e., we read in blocks at a time).</div>
<div><br></div><div>Obviously, this is a case that I just need to learn mocking. But, as with many projects, I&#39;m not really given the time to learn this right (i.e., sit down and add a day to learn how to mock well). In fact, this refactor itself isn&#39;t really in our sprint this week, but unfortunately has to be done :( :( :(</div>
<div><br></div><div>So, could anyone point me to some quick examples of using Mock?  I&#39;m getting confused by this &quot;reply&quot; pattern. I just want to override a library, and do a simple test to simulate the response. I prefer the old pyUnit patterns (setup, assert, etc.)</div>
<div><br></div><div>What libraries would you use in Python?  I am looking at this one - is it sufficient (I like it&#39;s .proxy()):</div><div><br class="Apple-interchange-newline"><a href="http://pypi.python.org/pypi/mocker/0.10.1">http://pypi.python.org/pypi/mocker/0.10.1</a></div>
<div><br></div><div>I just need to get kicked over the hump with an example, and think I&#39;d be fine after that.</div><div><br></div><div><br></div><div>Any help that you could give would be appreciated.</div><div><br></div>
<div><br></div><div>Cheers,</div><div><br></div><div><br></div><div>Glen</div><div>-- <br><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse">Things which matter most must never be at the mercy of things which matter least.</span><br>
<br>-- Goethe <br>
</div>