[TIP] Test doubles for Python standard library HTTP classes

Ben Finney ben+python at benfinney.id.au
Thu Aug 6 01:41:24 PDT 2015

Howdy all,

What standard Python library is there to make test doubles of
‘httplib.HTTPConnection’ and ‘urllib2.HTTPBasicAuthHandler’ and so on?

I have a code base (Python 2 code) which performs HTTP sessions using
the various HTTP-level classes in the standard library.

Testing this code will be made much easier if I can create numerous test
doubles with pre-determined behaviour and instrumentation for later
inspection, to allow the functions I'm testing to interact with those
doubles instead of the real thing.

There are some libraries that provide a much higher-level API, but I
need to provide double behaviour at the level this code expects, i.e. at
the level of the API provided by ‘httplib’ and ‘urllib2’ classes.

What standard code libraries provide classes which make it easy to
double the behaviour of ‘httplib’ classes and ‘urllib2’ classes, etc.
for test cases?

 \       “My mind is incapable of conceiving such a thing as a soul. I |
  `\     may be in error, and man may have a soul; but I simply do not |
_o__)                                      believe it.” —Thomas Edison |
Ben Finney

More information about the testing-in-python mailing list