问题描述
我有一段代码,我不知道如何进行单元测试!该模块使用 urllib2 从外部 XML 提要(twitter、flickr、youtube 等)中提取内容.这是它的一些伪代码:
I've got a piece of code that I can't figure out how to unit test! The module pulls content from external XML feeds (twitter, flickr, youtube, etc.) with urllib2. Here's some pseudo-code for it:
params = (url, urlencode(data),) if data else (url,)
req = Request(*params)
response = urlopen(req)
#check headers, content-length, etc...
#parse the response XML with lxml...
我的第一个想法是腌制响应并加载它以进行测试,但显然 urllib 的响应对象是不可序列化的(它引发了异常).
My first thought was to pickle the response and load it for testing, but apparently urllib's response object is unserializable (it raises an exception).
仅保存响应正文中的 XML 并不理想,因为我的代码也使用标头信息.它旨在作用于响应对象.
Just saving the XML from the response body isn't ideal, because my code uses the header information too. It's designed to act on a response object.
当然,在单元测试中依赖外部数据源是一个可怕的想法.
And of course, relying on an external source for data in a unit test is a horrible idea.
那么我该如何为此编写单元测试?
So how do I write a unit test for this?
推荐答案
urllib2 有一个名为 build_opener()
和 install_opener()
的函数,你应该用它们来模拟urlopen()
urllib2 has a functions called build_opener()
and install_opener()
which you should use to mock the behaviour of urlopen()
import urllib2
from 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
这篇关于如何对依赖 urllib2 的模块进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!