小编bra*_*999的帖子

Pytest mocker 补丁属性:错误“函数”对象没有属性“补丁”

我正在尝试模拟我使用 mocker.patch.object 创建的另一种方法。但是我得到了 AttributeError。使用 mocker 的新手,但还没有看到可以帮助解决这种情况的示例。

尝试了从 mocker 调用该方法的不同方式。

在测试/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)
Run Code Online (Sandbox Code Playgroud)

在项目/服务中

class ClassApi:

       def method_to_mock(self, input1):
         ...
        return result
Run Code Online (Sandbox Code Playgroud)

AttributeError: 'function' 对象没有属性 'patch'

python api-design pytest

3
推荐指数
1
解决办法
6056
查看次数

标签 统计

api-design ×1

pytest ×1

python ×1