python - How do I unit test a module that relies on urllib2? -


i've got piece of code can't figure out how unit test! module pulls content external xml feeds (twitter, flickr, youtube, etc.) urllib2. here's pseudo-code it:

params = (url, urlencode(data),) if data else (url,) req = request(*params) response = urlopen(req) #check headers, content-length, etc... #parse response xml lxml... 

my first thought pickle response , load testing, apparently urllib's response object unserializable (it raises exception).

just saving xml response body isn't ideal, because code uses header information too. it's designed act on response object.

and of course, relying on external source data in unit test horrible idea.

so how write unit test this?

urllib2 has functions called build_opener() , install_opener() should use mock behaviour of urlopen()

import urllib2 stringio import stringio  def mock_response(req):     if req.get_full_url() == "http://example.com":         resp = urllib2.addinfourl(stringio("mock file"), "mock message", req.get_full_url())         resp.code = 200         resp.msg = "ok"         return resp  class myhttphandler(urllib2.httphandler):     def http_open(self, req):         print "mock opener"         return mock_response(req)  my_opener = urllib2.build_opener(myhttphandler) urllib2.install_opener(my_opener)  response=urllib2.urlopen("http://example.com") print response.read() print response.code print response.msg 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -