Mock Suggestions/Feedback?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Mock Suggestions/Feedback?

Glen Jarvis
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()):

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
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Mock Suggestions/Feedback?

Alexandre Conrad-2
Glen,

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

http://mail.python.org/pipermail/baypiggies/2011-March/007675.html

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!")

HTH,


2011/12/28 Glen Jarvis <[hidden email]>:
> 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
>
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies



--
Alex | twitter.com/alexconrad


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies