[Baypiggies] TDD and mocking

Shannon -jj Behrens jjinux at gmail.com
Fri Jan 27 23:03:01 CET 2012


Here's my updated, slightly more pragmatic take on the issue.  I think you
should segregate code the interacts with Boto from code that deals with the
data that you get back from Boto.  For instance, if most of your code can
be segregated into a function that takes some JSON and does something with
it, then it'll be really easy to test.  If you have an extremely short
function that uses Boto that you don't have test coverage for, so be it.
 Eventually, you'll learn how to do dependency injection to test things
like that, but in the short term, it's all about bang for buck.

Happy Hacking!
-jj

On Fri, Jan 27, 2012 at 11:11 AM, Glen Jarvis <glen at glenjarvis.com> wrote:

> Hy gave a good newbie nugget last night. I've been doing more TDD and am
> really excited about it. But, I struggle with mocking.
>
> My question last night was how to mock properly. The example that I gave
> was for my current project (A mechanical turk and Django smash up). Most of
> my project is really basic -- just interfacing with other libraries, etc.
> There are pieces that I need to test -- like some admin actions, Django
> management commands, etc.
>
> I've tried to use Mock libraries, but haven't gotten over the hump. Let me
> re-ask the question now.
>
> Would it be "cool" or "uncool" if I made my equivalent Python class for a
> HIT, Assignment and Answer (these are equivalent classes from Boto)? The
> class would be a very basic/thin slice of what Amazon/Boto would provide.
> And, then when I try to mock, I inject my models into my project
> (hand-wavy somehow via monkey patching)?
>
> Now that I write this, would it be better to instantiate the results that
> Boto would present and inject those into a mocked response from Amazon? I'm
> bumping around in the dark on this and just haven't gotten past the hump to
> really mock well.
>
> I think I'm going to just give my best college try and then send a link to
> the project, asking for feedback/review on how I could have done it better.
> Any offline advice would be appreciated. The project is currently "written"
> but I consider it my first draft -- and already have found mistakes that
> would have been found by doing TDD first. I'd like to remove code, build
> failing tests, put in each part of code, and let the design drive.
>
> Wiki: https://github.com/glenjarvis/djurk/wiki/Quick-Start
>
> Code: https://github.com/glenjarvis/djurk
>
> Warmest Regards
>
>
> Glen
> P.S. I got some good advice last night from JJ and others. But, as I
> thought of my question further, I don't think I need to go as far as the
> WSGI layer as I just need to simulate the objects that would be received
> from the function call.... thoughts?
> --
> 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
>



-- 
In this life we cannot do great things. We can only do small things with
great love. -- Mother Teresa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120127/18399e74/attachment.html>


More information about the Baypiggies mailing list