問(wèn)題描述
我一直在搜索堆棧交換并在網(wǎng)絡(luò)上搜索如何做到這一點(diǎn),但我不明白如何模擬方法的行為.我正在嘗試為我的自定義類(lèi)模擬 openpyxl 行為和行為.這是我的嘗試:
導(dǎo)入單元測(cè)試從 unittest.mock 導(dǎo)入 MagicMock導(dǎo)入openpyxl從 MyPythonFile 導(dǎo)入 MyClass類(lèi)TestMyClass(unittest.TestCase):def test_myclass(self):我的班級(jí) = 我的班級(jí)()wb = openpyxl.workbook()ws = openpyxl.worksheet()wbPath = 'wbPath'openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)
當(dāng)我嘗試最后一行時(shí),我收到錯(cuò)誤無(wú)法分配給函數(shù)調(diào)用".我需要使用 patch.object('openpyxl','load_workbook')
嗎?我習(xí)慣用 Groovy 在 Java 中進(jìn)行模擬,它非常簡(jiǎn)單.
*想根據(jù)@alxwrd 的響應(yīng)添加測(cè)試的最終版本
導(dǎo)入單元測(cè)試從 unittest.mock 導(dǎo)入 MagicMock導(dǎo)入openpyxl導(dǎo)入配置解析器從 MyPythonFile 導(dǎo)入 MyClass類(lèi)TestMyClass(unittest.TestCase):def test_myclass(self):我的班級(jí) = 我的班級(jí)()wb = openpyxl.workbook()ws = openpyxl.worksheet()config = configparser.ConfigParser()openpyxl.load_workbook = MagicMock(return_value=wb)wb.get_sheet_by_name = MagicMock(return_value=ws)config.sections() = MagicMock(return_value=['Section1'])config.get = MagicMock(side_effect=['Value1','Value2'])
請(qǐng)注意 config.get 使用 side_effect 參數(shù)提供多個(gè)返回,因此如果在代碼中調(diào)用 config.get()
一次,它會(huì)返回 'Value1'
以及何時(shí)config.get()
在返回 'Value2'
時(shí)被第二次調(diào)用.
你不能重寫(xiě)函數(shù)調(diào)用,但你可以重寫(xiě)函數(shù)本身.
來(lái)自 docs:
<塊引用>>>>從 unittest.mock 導(dǎo)入 MagicMock>>>東西=生產(chǎn)類(lèi)()>>>thing.method = MagicMock(return_value=3)>>>thing.method(3, 4, 5, key='value')3>>>thing.method.assert_call_with(3, 4, 5, key='value')
所以在你的情況下:
openpyxl.load_workbook = MagicMock(return_value=wb)
I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt:
import unittest
from unittest.mock import MagicMock
import openpyxl
from MyPythonFile import MyClass
class TestMyClass(unittest.TestCase):
def test_myclass(self):
myclass = MyClass()
wb = openpyxl.workbook()
ws = openpyxl.worksheet()
wbPath = 'wbPath'
openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)
When I try the final line I get the error "can't assign to function call". Do I need to use patch.object('openpyxl','load_workbook')
? I am used to mocking in Java with Groovy and it's pretty straightforward.
*Edit: wanted to add the finalized version of the test based on the response from @alxwrd
import unittest
from unittest.mock import MagicMock
import openpyxl
import configparser
from MyPythonFile import MyClass
class TestMyClass(unittest.TestCase):
def test_myclass(self):
myclass = MyClass()
wb = openpyxl.workbook()
ws = openpyxl.worksheet()
config = configparser.ConfigParser()
openpyxl.load_workbook = MagicMock(return_value=wb)
wb.get_sheet_by_name = MagicMock(return_value=ws)
config.sections() = MagicMock(return_value=['Section1'])
config.get = MagicMock(side_effect=['Value1','Value2'])
Notice that config.get gives multiple returns with the side_effect parameter, so if config.get()
is called once in the code it returns 'Value1'
and when config.get()
is called a second time it returns 'Value2'
.
You can't override a function call, but you can override the function itself.
From the docs:
>>> from unittest.mock import MagicMock >>> thing = ProductionClass() >>> thing.method = MagicMock(return_value=3) >>> thing.method(3, 4, 5, key='value') 3 >>> thing.method.assert_called_with(3, 4, 5, key='value')
So in your case:
openpyxl.load_workbook = MagicMock(return_value=wb)
這篇關(guān)于在 Python 中模擬方法調(diào)用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!