如何测试通过 urllib2 检索数据的函数?

发布于 2024-11-24 04:46:44 字数 483 浏览 2 评论 0原文

我正在使用 python 进行测试,我问自己如何测试这个方法。

def get_response(self, url, params):
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = urllib2.urlopen(request, encoded_params)
    return response

使用 doctest 或 unittest,如何最好地实现这一点?我想向 get_response() 传递一个测试 url 和一些测试参数(这些参数存在于现实世界中),并检查 response.read() 是否返回预期的数据。但不知怎的,我觉得这不是应该做的事情。有什么建议吗?我希望得到有关如何在发短信的情况下处理此类情况的建议。

I am getting into testing in python and I asked myself how to test this method.

def get_response(self, url, params):
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = urllib2.urlopen(request, encoded_params)
    return response

Using doctest or unittest, how is this best achieved? I thought of passing get_response() a test url and some test parameters, which exists in real world and to check if response.read() returns the expected data. But somehow I feel, this is not the way it should be done. Any suggestions? I would appreciate suggestions how to handle such cases in context of texting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2024-12-01 04:46:44

这是使用假对象的一个​​很好的例子:

# my_module
get_url = urllib2.urlopen

def get_response(self, url, params):
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = get_url(request, encoded_params)
    return response

# test_my_module    
def fake_get_url(request, params):
    assert request == "the url I expect"
    assert params == ['the', 'params', 'i', 'expect']
    return SomeFakeResponse("OK")

my_module.get_url = fake_get_url
assert my_module.get_response("blah", "blah").content == "OK"

这只是一个最粗略的草图,说明如何使用您自己的假实现来覆盖真正的 urllib2.urlopen 函数,以在不真正访问网络的情况下测试您的代码。

This is a good case for using fake objects:

# my_module
get_url = urllib2.urlopen

def get_response(self, url, params):
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = get_url(request, encoded_params)
    return response

# test_my_module    
def fake_get_url(request, params):
    assert request == "the url I expect"
    assert params == ['the', 'params', 'i', 'expect']
    return SomeFakeResponse("OK")

my_module.get_url = fake_get_url
assert my_module.get_response("blah", "blah").content == "OK"

This is just the sketchiest sketch of how you could override the real urllib2.urlopen function with your own fake implementation to test your code without really hitting the web.

凉墨 2024-12-01 04:46:44

这是使用模拟测试框架的好机会,例如 minimock

BASE_URL='http://google.com'
HEADERS = {"accept-language":"klingon"}
import urllib, urllib2
def get_response(self, url, params):
    r"""
    >>> from minimock import Mock
    >>> urllib2.Request = Mock('urllib2.Request')
    >>> urllib2.urlopen = Mock('urllib2.urlopen')
    >>> get_response(None, 'http://google.com', {'foo':'bar'})
    Called urllib2.Request(
        'http://google.com',
        headers={'accept-language': 'klingon'})
    Called urllib2.urlopen(None, 'foo=bar')
    >>> 
    """
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = urllib2.urlopen(request, encoded_params)
    return response

请注意,该单元以 doctest 格式嵌入到被测函数的文档字符串中。

This is a good opportunity to use a mock testing framework, such as minimock.

BASE_URL='http://google.com'
HEADERS = {"accept-language":"klingon"}
import urllib, urllib2
def get_response(self, url, params):
    r"""
    >>> from minimock import Mock
    >>> urllib2.Request = Mock('urllib2.Request')
    >>> urllib2.urlopen = Mock('urllib2.urlopen')
    >>> get_response(None, 'http://google.com', {'foo':'bar'})
    Called urllib2.Request(
        'http://google.com',
        headers={'accept-language': 'klingon'})
    Called urllib2.urlopen(None, 'foo=bar')
    >>> 
    """
    encoded_params = urllib.urlencode(params)
    request = urllib2.Request(BASE_URL, headers=HEADERS)
    response = urllib2.urlopen(request, encoded_params)
    return response

Do note that the unit is embedded in the docstring for the function under test, in doctest format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文