模拟 urllib2.urlopen().read() 以获得不同的响应

Mocking urllib2.urlopen().read() for different responses(模拟 urllib2.urlopen().read() 以获得不同的响应)
本文介绍了模拟 urllib2.urlopen().read() 以获得不同的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试以某种方式模拟 urllib2.urlopen 库,以便对传递给函数的不同 url 获得不同的响应.

I am trying to mock the urllib2.urlopen library in a way that I should get different responses for different urls I pass into the function.

我现在在测试文件中的做法是这样的

The way I am doing it in my test file now is like this

@patch(othermodule.urllib2.urlopen)
def mytest(self, mock_of_urllib2_urllopen):
    a = Mock()
    a.read.side_effect = ["response1", "response2"]
    mock_of_urllib2_urlopen.return_value = a
    othermodule.function_to_be_tested()   #this is the function which uses urllib2.urlopen.read

我希望 othermodule.function_to_be_tested 在第一次调用时获得值response1",在第二次调用时获得值response2",这就是 side_effect 的作用

I expect the the othermodule.function_to_be_tested to get the value "response1" on first call and "response2" on second call which is what side_effect will do

但是 othermodule.function_to_be_tested() 接收

but the othermodule.function_to_be_tested() receives

<MagicMock name='urlopen().read()' id='216621051472'>

而不是实际的响应.请建议我哪里出错了或更简单的方法.

and not the actual response. Please suggest where I am going wrong or an easier way to do this.

推荐答案

patch的参数需要是对象的location的描述,而不是对象本身.因此,您的问题看起来可能只是您需要将参数字符串化为 patch.

The argument to patch needs to be a description of the location of the object, not the object itself. So your problem looks like it may just be that you need to stringify your argument to patch.

不过,为了完整起见,这里有一个完整的示例.首先,我们的待测模块:

Just for completeness, though, here's a fully working example. First, our module under test:

# mod_a.py
import urllib2

def myfunc():
    opened_url = urllib2.urlopen()
    return opened_url.read()

现在,设置我们的测试:

Now, set up our test:

# test.py
from mock import patch, Mock
import mod_a

@patch('mod_a.urllib2.urlopen')
def mytest(mock_urlopen):
    a = Mock()
    a.read.side_effect = ['resp1', 'resp2']
    mock_urlopen.return_value = a
    res = mod_a.myfunc()
    print res
    assert res == 'resp1'

    res = mod_a.myfunc()
    print res
    assert res == 'resp2'

mytest()

从 shell 运行测试:

Running the test from the shell:

$ python test.py
resp1
resp2

编辑:糟糕,最初包含原始错误.(正在测试以验证它是如何被破坏的.)现在应该修复代码.

Edit: Whoops, initially included the original mistake. (Was testing to verify how it was broken.) Code should be fixed now.

这篇关于模拟 urllib2.urlopen().read() 以获得不同的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)