views:

75

answers:

1

I have a small project that basically a Python wrapper to a websites API.

It's fairly well tested, but there are some conditions I cannot work out how to easily test: when the remote API is unreachable, or otherwise broken.

More specifically, I would quite like a test for each of the following:

  • When the site is unreachable (connection timeout)
  • When the site is reachable, but returns an HTTP error code (For example, an error 404 or 500)
  • The content is malformed. The site has an XML interface. A while ago the site was having problems, and the page that should have been an XML file was an HTML page, which broke the XML parsing

How would I go about testing these cases? The only thing I can think of is to change the API's URL to a non-existent server (for the unreachable case), and a local web-server for the error 500/404/malformed data

+2  A: 

The other answer is to mock out the code that actually makes the calls to the web site, and have it return the error conditions that you require. I guess that means mock urllib or httplib in python

Andrew Cox