我觉得这可能是相对简单的,但我正在努力让它工作。我想模拟整个类,然后为该类的一个方法指定返回值。
我已经研究了here,还有其他几个问题,当然也是在docs中。我还是不能让它工作。请看下面我的简单示例。
目录tmp
的内容
tmp
├── __init__.py
├── my_module.py
└── test_my_module.py
my_module.py
的内容
class MyClass:
def __init__(self):
# Do expensive operations that will be mocked in testing.
self.a = 7
def my_method(self):
# For sake of simple example, always return 1.
return 1
def create_class_call_method():
"""Create MyClass instance and call its my_method method, returning
the result."""
instance = MyClass()
value = instance.my_method()
return value
test_my_module.py
的内容
import unittest
from unittest.mock import patch, Mock
from tmp import my_module
class MyClassTestCase(unittest.TestCase):
def test_create_class_call_method(self):
# Attempt to patch MyClass as well as specify a return_value for
# the my_method method (spoiler: this doesn't work)
with patch('tmp.my_module.MyClass',
my_method=Mock(return_value=2)):
value = my_module.create_class_call_method()
self.assertEqual(value, 2)
if __name__ == '__main__':
unittest.main()
运行test_my_module.py
的结果
2 != <MagicMock name='MyClass().my_method()' id='140234477124048'>
Expected :<MagicMock name='MyClass().my_method()' id='140234477124048'>
Actual :2
我还尝试过其他一些方法:
使用**{'my_method.return_value': 2}
patch
语句中的..., my_method=Mock(return_value=2))
来解压缩字典。外部语句像with patch('tmp.my_module.MyClass'):
一样简单,内部语句尝试像这样修补my_method
:p.evaluate = Mock(return_value=2)
将修饰器而不是上下文with patch('tmp.my_module.MyClass'):
语句修补为my_method
,然后在with
语句内部,尝试像这样设置p
:
感谢您的帮助,谢谢。
https://stackoverflow.com/questions/57044593
复制相似问题