Hi Mike,

I can see two options.

Option 1: factor the external service API calls behind some sort of
interface that can be swapped out at runtime.

However, this means a lot of extra work if the interface will only be used
for testing.

Option 2: look into using Mock [1]

[1] http://www.voidspace.org.uk/python/mock/

As of Python 3.3, it's part of the Python standard library, but until then,
it's on PyPI as a library that can be used in Python 2.

Mock allows you to replace specific methods and objects in your system with
dummy objects that behave the way you want them to, on a per-test basis.
So, for example, you could mock out the parts of your system that make
calls on external services. You program in the return values and side
effects that you want the methods to have, and they will behave like that
under test conditions without calling the actual service.

The added bonus is that you can program them to behave the way you want to
-- so, for example, if you want to test what happens when your credit card
processor returns a "temporarily unavailable' error, you can specifically
program your mock to behave that way.

Mock works really well for testing against web APIs, because you have a
very clear and simple API interface to test against -- and if you're using
requests to make those API calls, you will often have to mock a single call
(the get/post call you want to test).

Yours,
Russ Magee %-)

On Fri, May 3, 2013 at 1:39 PM, Mike <mike.t...@gmail.com> wrote:

> I'm starting to write tests for my Django project but I'm not sure how to
> test a function that calls out to a web API.  I read somewhere that I
> should not test against the live web api and that I should develop some
> sort of replacement for the actual web api server which sounds like a lot
> of work.  I was thinking of factoring out the actual api request into its
> own function with as few lines of code as possible and just not test it.
>  Or, perhaps develop some tests that do actually require requests out the
> web api but only run those tests on rare occasions, like just before a
> release for example.  What do other people do?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to