修补从模拟类调用的类函数

时间:2019-05-01 03:46:29

标签: python unit-testing mocking python-unittest

我为令人困惑的标题表示歉意,但不确定是否有更好的方法来描述问题。我有一个具有以下结构的python库:

library/
  src/
    code/
      __init__.py
      baseclass.py
      helpers.py
      class1.py
    tests/
      __init__.py
      test_library.py

我正在尝试在baseclass.py中的类中测试功能。 baseclass.py中类中的函数从class1.py返回类对象,如下所示: baseclass.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()方法中调用了模拟的类。我还看到了其他一些帖子,但是还没有找到解决方案。是我遗漏了明显的东西还是走错了路?

1 个答案:

答案 0 :(得分:0)

最后弄清楚了,因为我认为这是导入模块的位置。我尝试了所有可能的变体,解决方案是确保从被调用的位置打补丁的对象。我不知道为什么我昨晚没看到这个!!

原始呼叫补丁为@patch('baseclass.class1', 'DeviceClass', autospec=True),正确的补丁为@patch('baseclass.DeviceClass', autospec=True),如下所示

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('Library.baseclass.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)