[Baypiggies] Mock Suggestions/Feedback?

Alexandre Conrad alexandre.conrad at gmail.com
Thu Dec 29 00:55:53 CET 2011


I previously wrote about my approach before on this list, here is the


My motivation is that I don't want my tests to make any calls to external
services, like testing a client that talks to an external API.

Nowadays, I make heavy use of lambda as such:

from mock import patch

expected_responses = [True, False, "hello", None]
with patch("myproj.lib.client._make_request",
            lambda: *args, **kwargs: responses.pop(0)):
   # test my stuff

If you need to raise an error, substitute the lambda with a callable that
raises an exception:

def do_raise(Exc): raise Exc("raised!")


2011/12/28 Glen Jarvis <glen at glenjarvis.com>:
> 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*
> 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
> 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
> a 404 and see how well it responds. Similarly, a "website" that has a
> Also, what about time out issues? And, what about retries that fail --
> 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
> this code. What about pages that have larger content than is normally
> 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.,
> down and add a day to learn how to mock well). In fact, this refactor
> 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
> confused by this "reply" pattern. I just want to override a library, and
> 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
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies

Alex | twitter.com/alexconrad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20111228/ad7a9253/attachment-0001.html>

More information about the Baypiggies mailing list