久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

模擬 urllib2.urlopen().read() 以獲得不同的響應(yīng)

Mocking urllib2.urlopen().read() for different responses(模擬 urllib2.urlopen().read() 以獲得不同的響應(yīng))
本文介紹了模擬 urllib2.urlopen().read() 以獲得不同的響應(yīng)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試以某種方式模擬 urllib2.urlopen 庫(kù),以便對(duì)傳遞給函數(shù)的不同 url 獲得不同的響應(yīng).

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.

我現(xiàn)在在測(cè)試文件中的做法是這樣的

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 在第一次調(diào)用時(shí)獲得值response1",在第二次調(diào)用時(shí)獲得值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'>

而不是實(shí)際的響應(yīng).請(qǐng)建議我哪里出錯(cuò)了或更簡(jiǎn)單的方法.

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

推薦答案

patch的參數(shù)需要是對(duì)象的location的描述,而不是對(duì)象本身.因此,您的問(wèn)題看起來(lái)可能只是您需要將參數(shù)字符串化為 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.

不過(guò),為了完整起見(jiàn),這里有一個(gè)完整的示例.首先,我們的待測(cè)模塊:

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()

現(xiàn)在,設(shè)置我們的測(cè)試:

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 運(yùn)行測(cè)試:

Running the test from the shell:

$ python test.py
resp1
resp2

編輯:糟糕,最初包含原始錯(cuò)誤.(正在測(cè)試以驗(yàn)證它是如何被破壞的.)現(xiàn)在應(yīng)該修復(fù)代碼.

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

這篇關(guān)于模擬 urllib2.urlopen().read() 以獲得不同的響應(yīng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 91在线网站 | 国产精品亚洲第一区在线暖暖韩国 | 精品一区二区三区在线观看国产 | 日本大香伊一区二区三区 | 成人精品一区二区三区中文字幕 | 日韩精品成人 | 一区二区三区成人 | 国产精品99久久久精品免费观看 | 精品国产1区2区3区 在线国产视频 | 日韩电影一区二区三区 | 91精品久久久久久久久久小网站 | 日韩在线成人 | 日韩av在线一区 | 亚洲欧洲视频 | 在线观看成人免费视频 | 欧美男人天堂 | 91日韩在线 | 亚洲综合伊人 | 色婷婷av久久久久久久 | 亚洲综合二区 | 午夜精品一区二区三区在线视频 | 国产福利视频在线观看 | 国产在线区 | 亚洲嫩草| 国产精品一区二区久久 | 爱高潮www亚洲精品 中文字幕免费视频 | 一级在线免费观看 | 中文字幕在线免费视频 | 国产成人久久久 | 成人亚洲精品久久久久软件 | 国产成人亚洲精品自产在线 | 在线看av网址| 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | 亚洲一区二区三区在线播放 | 欧美中文一区 | 男女网站免费观看 | www.亚洲成人网 | 亚洲欧美激情国产综合久久久 | 在线国产一区二区 | 日本高清aⅴ毛片免费 | 成人深夜福利网站 |