Quantcast

TDD and mocking

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

TDD and mocking

Glen Jarvis
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.



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

Re: TDD and mocking

Shannon -jj Behrens
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 <[hidden email]> 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.



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
[hidden email]
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

_______________________________________________
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
|  
Report Content as Inappropriate

Re: TDD and mocking

Shannon -jj Behrens
In reply to this post by Glen Jarvis
It's so funny that you asked this at the last BayPiggies because I ran into the same problem with my current project.  I'm using Ruby on Rails for this project, but the solution is pretty much the same for Python.  Let me just say, doing TDD with web services is hard!  By the way, I'm giving a talk tonight at the East Bay Ruby Meetup, and I plan on spending 15 minutes talking about this problem.  However, here is a quick outline of how I do it:

 * Strive to understand the API at the REST level.  Client libraries are nice, but they're often misleading, underdocumented, incomplete, etc.

 * Start by finding a good "context" in your app to play with stuff and add "import pdb; pdb.set_trace()".  Now, load that part of your app in a browser so that you can play around in the debugger.

 * In the debugger, play around with the client library.  Make the request and get the response that you're looking for.  Copy and paste everything you need into a scratch file.

 * Now, start writing a test that will exercise the code that needs to use the client library.

 * In the Rails world, I'm using a library called webmock.  Whenever you try to make a web service call, it'll say, "Hey, you can't do that!  I'm not going to let you make web services calls.  However, if you want to stub out the call you just made, here's a bit of code you can add to your tests."  Grab that.

 * Merge the stuff from your debugging session with the stuff webmock gave you in your test.  Tweak, tweak, tweak to make it a good test.

 * For large web service responses, I tend to put them in appropriately named files.  For instance, I have a file named "spec/support/documents/gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=Pythagorean+Theorem+in+60+Seconds" that contains a big JSON dump.

 * Do the same thing to add tests for all your error cases.

 * I tend to write high-level integration tests where possible, so it's helpful to have a library of useful webmock stubs that you can use in multiple tests.

 * Don't be afraid to look at the source code of the client libraries, especially if it has a good suite of tests!  Sometimes that will give you the understanding you need to write what you need.

Hope that helps!

-jj

On Fri, Jan 27, 2012 at 11:11 AM, Glen Jarvis <[hidden email]> 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.



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
[hidden email]
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

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