[TIP] Test doubles for Python standard library HTTP classes

Ben Finney ben+python at benfinney.id.au
Sun Aug 9 23:20:19 PDT 2015

Florian Bruhin <me at the-compiler.org> writes:

> Are you thinking of things like VCR.py[1] or HTTPretty[2] here?
> Because that's what came to my mind first.

There's also the ‘responses’ library [0], which would be great if the
system under test were using the ‘requests’ library.

[0]: <URL:https://pypi.python.org/pypi/responses>

But it's no good for the code I'm looking at, since ‘requests’ is not
the API being used. Rather it uses, the Python 2 standard library
‘httplib’ and ‘urllib2’ and other APIs.

> [1] https://pypi.python.org/pypi/vcrpy

This one doesn't really match what I need; I don't have existing
transactions to record and store and replay. Rather I want to specify in
test case code a scripted HTTP session without dealing with all the

Also ‘vcrpy’ is not packaged in Debian Jessie, so that would be a high
cost for my project.

> [2] https://github.com/gabrielfalcao/HTTPretty

This one is quite promising. It allows me to specify some aspects of an
interaction, even to the point of specifying an HTTP response generator

I'm having trouble getting HTTPretty to respond to the authentication
steps in a session; but that probably speaks more to the fact I'm
unfamiliar with the detailed steps the system under test is expecting.

HTTPretty's documentation and API do not inspire confidence; its
published classes and names don't cohere at all well, and its
documentation is a garbled collection of examples only.

But so far it seems closest to what I need.

 \      “You've got to think about big things while you're doing small |
  `\              things, so that all the small things go in the right |
_o__)                                       direction.” —Alvin Toffler |
Ben Finney

More information about the testing-in-python mailing list