[TIP] testing against HTTP services

Barry Warsaw barry at python.org
Mon Dec 10 07:48:54 PST 2012

On Dec 09, 2012, at 02:49 PM, Alfredo Deza wrote:

>The current approach for running a test in application Foo that talks to
>HTTP service Bar, is to run the Bar HTTP service as part of some setup.

With modern test runners, this can be less expensive if you can handle some
shared state (with care taken not to allow side-effects to pollute tests).
Use the class/layer/resource-based setup and teardowns to amortize the cost of
setting up the service.

You're testing Foo here right, not Bar?  And hopefully Bar isn't a live
services. ;)

>This seems straightforward, it is only an HTTP service after all, but what
>happens when that service become expensive to run? What if it needs a
>database and other things in order to run properly? What if it gets to the
>point where you need to wait for several seconds just to bring it up for
>your test that takes 0.01 to complete?
>If Bar keeps getting updated, having Foo test Bar against a number of
>versions is a pain multiplier.

In addition to the good recommendations others have given, I think the key is
really to limit which tests need to actually talk to Bar.  The more that you
can make the edges of your system really lightweight, the more you can beef up
your tests behind the edges, and then reduce the amount of testing at the
interface layer.

What I mean is, let's say Foo has to talk to Bar to get some JSON
representation of an object out of the database.  If your method that talks to
Bar is the thinnest of layers around HTTP GET and json.loads() then you can do
a ton of testing of the lower levels assuming you have a valid JSON object.
Then you might be able to get away with fewer tests of the good and bad paths
of the GET/loads layer.

YMMV, and it's hard to say what will work in the abstract, but on both sides
of such communications, I try to keep things as thin and light as possible.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121210/88f39eb3/attachment.pgp>

More information about the testing-in-python mailing list