【发布时间】:2019-05-01 03:46:29
【问题描述】:
对于令人困惑的标题,我深表歉意,我不确定是否有更好的方式来描述问题。我有一个具有以下结构的 python 库:
library/
src/
code/
__init__.py
baseclass.py
helpers.py
class1.py
tests/
__init__.py
test_library.py
我正在尝试在 baseclass.py 的一个类中测试一个函数。 baseclass.py中类中的函数从class1.py中返回类对象,如下图: 基类.py:
from class1 import DeviceClass
class LibraryBuilder:
def __init__(self, user, password):
......
def get_devices(self):
devlist = helpers.call_api('api_url', 'post', header, json)
#Return a list of dictionaries
for dev in devlist:
if dev = "DeviceType":
return DeviceClass(dev, self)
test_library.py
import pytest
from unittest.mock import patch, Mock
from library.baseclass import LibraryBuilder
import library
from library.class1 import DeviceClass
class TestDeviceList(object):
@pytest.fixture()
def api_mock(self, caplog):
self.mock_api_call = patch('library.helpers.call_api', autospec=True)
self.mock_api = self.mock_api_call.start()
self.mock_api.return_value.ok = True
self.library_obj = library.baseclass.LibraryBuilder('sam@mail.com', 'pass')
yield
self.mock_api_call.stop()
@patch.object('baseclass.class1', 'DeviceClass', autospec=True)
def test_get_devices_all(self, caplog, dev_mock, api_mock):
self.mock_api.return_value = return_dict
devs = self.library_object.get_devices()
dev_mock.assert_called_with(return_dict, self.library_object)
测试失败,因为从未调用过“device_object”。调试时,我看到创建的 device_patch 对象不是模拟对象,而是实际的 DeviceClass 对象。
我尝试将 device_object 路径引用到patch.object('library.baseclass', 'DeviceClass', autospec=True)。我尝试了导入类的变体,因为我相信这与下面的线程有关,但我无法弄清楚我哪里出错了:
Why python mock patch doesn't work?
call_api 模拟可以正常工作。 library_object 返回基于 call_api 模拟的 return_value 的实际实例化类
我只是将代码从单个文件重构为此配置,并且在此之前测试通过了。关于我缺少什么的任何想法?
编辑
我进一步调试,我相信它与 DeviceClass 继承自 DeviceBaseClass 有关,因此 device_class.py 看起来像:
class DeviceBaseClass(object):
def __init__(self, details, manager):
self.details = {}
..............
class DeviceClass(DeviceBaseClass):
def __init__(self, details, manager):
super(DeviceClass, self).__init__(details, manager)
所以现在我收到了TypeError: super() argument 1 must be type not MagicMock 的消息。我猜,因为我在模拟 DeviceClass,所以在 super() 方法中调用了模拟的类。我已经看到了其他一些关于此的帖子,但还没有找到解决方案。我是否遗漏了一些明显的东西,还是我走错了路?
【问题讨论】:
标签: python unit-testing mocking python-unittest