[Baypiggies] Mock Suggestions/Feedback?

Glen Jarvis glen at glenjarvis.com
Thu Dec 29 00:27:29 CET 2011


I need help with a real Test Driven concept. I've been building up my TDD
(not just testing, but real TDD) skills over this past year. I *really*
like the book "Growing Object-Oriented Software, Guided by Tests." It seems
to reflect how this makes sense -- growing the architecture, almost
organically, using tests to weed, guide, and prune as we go.

I'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't keep writing
fragile tests.

I am currently refactoring a project and I'd like to build it back up with
tests again. Here is a sample skeleton:

class HttpMixIn[snip]
    ""[snip]"""

    def fetch(self, url):
        """Fetch page, return result as large string."""
        [snip]
        try:
            response = urllib2.urlopen(url, timeout=settings.[snip])
            response = unicode(response.read(), errors='ignore')
            [snip]
        except urllib2.HTTPError, e:
            [snip]
        except ValueError, e:
            [snip]
        except urllib2.URLError, e:
            [snip]
        except Exception, e:
            [snip]

        return response

Obviously, to test this, I would like to try it against a "website" that
has a 404 and see how well it responds. Similarly, a "website" that has a
"500." Also, what about time out issues? And, what about retries that fail
-- could we retry a few times and, thus, come up with a "website" that
"fails" two times and then is successful? Also, look at the
"response.read()" 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).

Obviously, this is a case that I just need to learn mocking. But, as with
many projects, I'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't really in our sprint this week, but unfortunately has to be
done :( :( :(

So, could anyone point me to some quick examples of using Mock?  I'm
getting confused by this "reply" 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.)

What libraries would you use in Python?  I am looking at this one - is it
sufficient (I like it's .proxy()):

http://pypi.python.org/pypi/mocker/0.10.1

I just need to get kicked over the hump with an example, and think I'd be
fine after that.


Any help that you could give would be appreciated.


Cheers,


Glen
-- 
Things which matter most must never be at the mercy of things which matter
least.

-- Goethe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20111228/33f8060c/attachment.html>


More information about the Baypiggies mailing list