Python如何在單元測試中給對象打補丁
問題
你寫的單元測試中需要給指定的對象打補丁, 用來斷言它們在測試中的期望行為(比如,斷言被調(diào)用時的參數(shù)個數(shù),訪問指定的屬性等)。
解決方案
unittest.mock.patch() 函數(shù)可被用來解決這個問題。 patch() 還可被用作一個裝飾器、上下文管理器或單獨使用,盡管并不常見。 例如,下面是一個將它當做裝飾器使用的例子:
from unittest.mock import patch
import example
@patch('example.func')
def test1(x, mock_func):
example.func(x) # Uses patched example.func
mock_func.assert_called_with(x)
它還可以被當做一個上下文管理器:
with patch('example.func') as mock_func:
example.func(x) # Uses patched example.func
mock_func.assert_called_with(x)
最后,你還可以手動的使用它打補?。?/p>
p = patch('example.func')
mock_func = p.start()
example.func(x)
mock_func.assert_called_with(x)
p.stop()
如果可能的話,你能夠疊加裝飾器和上下文管理器來給多個對象打補丁。例如:
@patch('example.func1')
@patch('example.func2')
@patch('example.func3')
def test1(mock1, mock2, mock3):
...
def test2():
with patch('example.patch1') as mock1, \
patch('example.patch2') as mock2, \
patch('example.patch3') as mock3:
...
討論
patch() 接受一個已存在對象的全路徑名,將其替換為一個新的值。 原來的值會在裝飾器函數(shù)或上下文管理器完成后自動恢復(fù)回來。 默認情況下,所有值會被 MagicMock 實例替代。例如:
>>> x = 42
>>> with patch('__main__.x'):
... print(x)
...
<MagicMock name='x' id='4314230032'>
>>> x
42
>>>
不過,你可以通過給 patch() 提供第二個參數(shù)來將值替換成任何你想要的:
>>> x
42
>>> with patch('__main__.x', 'patched_value'):
... print(x)
...
patched_value
>>> x
42
>>>
被用來作為替換值的 MagicMock 實例能夠模擬可調(diào)用對象和實例。 他們記錄對象的使用信息并允許你執(zhí)行斷言檢查,例如:
>>> from unittest.mock import MagicMock
>>> m = MagicMock(return_value = 10)
>>> m(1, 2, debug=True)
10
>>> m.assert_called_with(1, 2, debug=True)
>>> m.assert_called_with(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../unittest/mock.py", line 726, in assert_called_with
raise AssertionError(msg)
AssertionError: Expected call: mock(1, 2)
Actual call: mock(1, 2, debug=True)
>>>
>>> m.upper.return_value = 'HELLO'
>>> m.upper('hello')
'HELLO'
>>> assert m.upper.called
>>> m.split.return_value = ['hello', 'world']
>>> m.split('hello world')
['hello', 'world']
>>> m.split.assert_called_with('hello world')
>>>
>>> m['blah']
<MagicMock name='mock.__getitem__()' id='4314412048'>
>>> m.__getitem__.called
True
>>> m.__getitem__.assert_called_with('blah')
>>>
一般來講,這些操作會在一個單元測試中完成。例如,假設(shè)你已經(jīng)有了像下面這樣的函數(shù):
# example.py
from urllib.request import urlopen
import csv
def dowprices():
u = urlopen('http://finance.yahoo.com/d/quotes.csv?s=@^DJI&f=sl1')
lines = (line.decode('utf-8') for line in u)
rows = (row for row in csv.reader(lines) if len(row) == 2)
prices = { name:float(price) for name, price in rows }
return prices
正常來講,這個函數(shù)會使用 urlopen() 從Web上面獲取數(shù)據(jù)并解析它。 在單元測試中,你可以給它一個預(yù)先定義好的數(shù)據(jù)集。下面是使用補丁操作的例子:
import unittest
from unittest.mock import patch
import io
import example
sample_data = io.BytesIO(b'''\
"IBM",91.1\r
"AA",13.25\r
"MSFT",27.72\r
\r
''')
class Tests(unittest.TestCase):
@patch('example.urlopen', return_value=sample_data)
def test_dowprices(self, mock_urlopen):
p = example.dowprices()
self.assertTrue(mock_urlopen.called)
self.assertEqual(p,
{'IBM': 91.1,
'AA': 13.25,
'MSFT' : 27.72})
if __name__ == '__main__':
unittest.main()
本例中,位于 example 模塊中的 urlopen() 函數(shù)被一個模擬對象替代, 該對象會返回一個包含測試數(shù)據(jù)的 ByteIO()
還有一點,在打補丁時我們使用了 example.urlopen 來代替 urllib.request.urlopen 。 當你創(chuàng)建補丁的時候,你必須使用它們在測試代碼中的名稱。 由于測試代碼使用了 from urllib.request import urlopen ,那么 dowprices() 函數(shù) 中使用的 urlopen() 函數(shù)實際上就位于 example 模塊了。
本節(jié)實際上只是對 unittest.mock 模塊的一次淺嘗輒止。 更多更高級的特性,請參考 官方文檔
以上就是Python如何在單元測試中給對象打補丁的詳細內(nèi)容,更多關(guān)于Python 單元測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python基礎(chǔ)之reverse和reversed函數(shù)的介紹及使用
我們在整理資料的時候,有的時候過于混亂難以整理,這時我們不妨可以對它先進行一個排序,這樣可以方便我們?yōu)g覽,查詢,有利于我們整理資料,下面這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)之reverse和reversed函數(shù)的介紹及使用,需要的朋友可以參考下2022-12-12
查看keras各種網(wǎng)絡(luò)結(jié)構(gòu)各層的名字方式
這篇文章主要介紹了查看keras各種網(wǎng)絡(luò)結(jié)構(gòu)各層的名字方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
APIStar:一個專為Python3設(shè)計的API框架
今天小編就為大家分享一篇關(guān)于一個專為Python3設(shè)計的API框架:APIStar,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-09-09
Django開發(fā)中使用Ueditor上傳圖片遇到的坑及解決
在Django開發(fā)中使用Ueditor上傳圖片時,可能會遇到后端配置不正確的問題,建議在實例化Ueditor后加上serverUrl,這可以在Chrome的F12工具中查看請求的后端配置項,此外,如果需要修改上傳路徑,可以在配置文件中更改路徑,并調(diào)整view.py中的代碼來管理上傳文件2024-09-09
Python配置文件管理之ini和yaml文件讀取的實現(xiàn)
本文主要介紹了Python配置文件管理之ini和yaml文件讀取,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

