[TIP] Test doubles for Python standard library HTTP classes

Robert Collins robertc at robertcollins.net
Thu Aug 6 01:56:45 PDT 2015


On 6 August 2015 at 20:41, Ben Finney <ben+python at benfinney.id.au> wrote:
> Howdy all,
>
> 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?

> 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?

You can use mock (the rolling backport of unittest.mock) with autospec
to create high fidelity doubles.
There may be HTTP specific doubles floating around already, perhaps
look on PyPI for that.

-Rob


-- 
Robert Collins <rbtcollins at hp.com>
Distinguished Technologist
HP Converged Cloud



More information about the testing-in-python mailing list