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

模擬 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() 以獲得不同的響應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 亚洲成人精品在线 | 最新日韩精品 | 久久这里只有精品首页 | 伦理二区 | 91 久久 | 操人视频在线观看 | 中文字幕在线视频观看 | 欧美精品一区二区三区视频 | 日韩视频一区在线观看 | 日韩成人在线视频 | 日韩精品中文字幕一区二区三区 | 欧美日韩中文字幕在线 | 国产一区二区在线91 | 亚洲精品久久久蜜桃网站 | 欧美aaaa视频 | 国产精品日日摸夜夜添夜夜av | 亚洲精品视频免费观看 | 精品一区av | 亚洲啊v | 久热精品在线播放 | 国产精品日韩一区二区 | 欧美成人影院在线 | 九九免费视频 | 久久国产精品一区二区三区 | 欧美最猛黑人xxxⅹ 粉嫩一区二区三区四区公司1 | 成在线人视频免费视频 | av天天看| 亚洲 欧美 另类 综合 偷拍 | 在线精品国产 | 亚洲va国产日韩欧美精品色婷婷 | 欧美久久一级特黄毛片 | 日本高清视频在线播放 | 91婷婷韩国欧美一区二区 | 日本精品一区二区在线观看 | www.久久 | 九九热免费观看 | 亚洲性人人天天夜夜摸 | 久久国产精品久久久久久久久久 | 国产午夜精品一区二区三区嫩草 | 午夜视频一区 | 日日摸日日添日日躁av |