[TIP] Test doubles for Python standard library HTTP classes

Ben Finney ben+python at benfinney.id.au
Thu Aug 6 17:10:29 PDT 2015

Robert Collins <robertc at robertcollins.net> writes:

> On 6 August 2015 at 20:41, Ben Finney <ben+python at benfinney.id.au> wrote:
> > What standard Python library is there to make test doubles of
> > ‘httplib.HTTPConnection’ and ‘urllib2.HTTPBasicAuthHandler’ and so
> > on?
> Out of interest - why do you need to limit yourself to the standard
> library?

Sorry for being unclear, “standard” implies Python standard library, and
that's not what I meant. I meant to say “widely-used or

I would *really* prefer to say “already packaged in Debian Jessie”, but
that would have been a distraction I suspect.

> > I have a code base (Python 2 code) which performs HTTP sessions
> > using the various HTTP-level classes in the standard library.
> > What standard code libraries provide classes which make it easy to
> > double the behaviour of ‘httplib’ classes and ‘urllib2’ classes, etc.
> > for test cases?
> You can use mock (the rolling backport of unittest.mock) with autospec
> to create high fidelity doubles.

I'm already using ‘mock’ and am quite happy with it. But that doesn't
help with doubling the *behaviour* of those classes; I want test doubles
which behave as expected by the code, and to inject specific scripted
responses at specific points of the session, while having as much of the
other grunt work of an HTTP session taken care of for me.

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.

That is close. What I want though isn't to record behaviour, but to
script it.

I want to set up a bunch of scenarios (using ‘testscenarios’) and
combinatorially determine behaviour of the test doubles for those

 \          “The fact that I have no remedy for all the sorrows of the |
  `\     world is no reason for my accepting yours. It simply supports |
_o__)  the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney

More information about the testing-in-python mailing list