[英]Pytest mocker patch Attribute:Error 'function' object has no attribute 'patch'
我正在嘗試使用mocker.patch.object模擬我創建的另一種方法。 但是我得到了AttributeError。 使用mocker的新手,但沒有看到一個可以幫助解決這個問題的例子。
嘗試了從mocker調用方法的不同方法。
在tests / 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 result
AttributeError:'function'對象沒有屬性'patch'
我對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在運行時自動為測試函數提供參數mocker,因此無需導入它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.