我正在尝试模拟我使用mocker.patch.object创建的另一个方法。然而,我得到了AttributeError。刚开始使用mocker,但还没有看到可以帮助解决这种情况的示例。
尝试了从mocker调用该方法的不同方式。
在test/test_unit.py中
from pytest_mock import mocker
class TestApp:
def setup_method(self):
self.obj = ClassApi()
def test_class_api_method(self, client):
return_value = {'name': 'test'}
mocker.patch.object(self.obj, 'method_to_mock')
mocker.result(return_value)在项目/服务中
class ClassApi:
def method_to_mock(self, input1):
...
return resultAttributeError:“function”对象没有属性“”patch“”
发布于 2019-06-08 15:06:46
我对Pytest-Mock不是很熟悉,但是根据文档,你应该使用mocker作为一种固定物。所以你的函数应该是这样的:
def test_class_api_method(self, client, mocker):
return_value = {'name': 'test'}
mocker.patch.object(self.obj, 'method_to_mock')
mocker.result(return_value)pytest在运行时会自动为测试函数提供参数模拟器,因此不需要导入它。
https://stackoverflow.com/questions/56504222
复制相似问题