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

时间:2022-10-18
本文介绍了模拟 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() 以获得不同的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:对于公共方法,Python 模拟补丁无法按预期工作 下一条:如何在 Python 中模拟 os.listdir 来假装文件和目录?

相关文章

最新文章