Mocking Method Calls In Python(在 Python 中模拟方法调用)
问题描述
我一直在搜索堆栈交换并在网络上搜索如何做到这一点,但我不明白如何模拟方法的行为.我正在尝试为我的自定义类模拟 openpyxl 行为和行为.这是我的尝试:
导入单元测试从 unittest.mock 导入 MagicMock导入openpyxl从 MyPythonFile 导入 MyClass类TestMyClass(unittest.TestCase):def test_myclass(self):我的班级 = 我的班级()wb = openpyxl.workbook()ws = openpyxl.worksheet()wbPath = 'wbPath'openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)
当我尝试最后一行时,我收到错误无法分配给函数调用".我需要使用 patch.object('openpyxl','load_workbook')
吗?我习惯用 Groovy 在 Java 中进行模拟,它非常简单.
*想根据@alxwrd 的响应添加测试的最终版本
导入单元测试从 unittest.mock 导入 MagicMock导入openpyxl导入配置解析器从 MyPythonFile 导入 MyClass类TestMyClass(unittest.TestCase):def test_myclass(self):我的班级 = 我的班级()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'])
请注意 config.get 使用 side_effect 参数提供多个返回,因此如果在代码中调用 config.get()
一次,它会返回 'Value1'
以及何时config.get()
在返回 'Value2'
时被第二次调用.
你不能重写函数调用,但你可以重写函数本身.
来自 docs:
<块引用>>>>从 unittest.mock 导入 MagicMock>>>东西=生产类()>>>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)
这篇关于在 Python 中模拟方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中模拟方法调用
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01