Python-单元测试补丁

用户名

我在@patch使用单元测试时遇到了一些问题,修补了导入后,似乎没有拾取模拟,而是使用了“常规”类

我尝试测试的类和方法如下所示(为方便起见,将其简化)

from sila2lib.sila_server import SiLA2Server
# ... snip ...

class DeviceService:

def __init__(self, central_message_queue: asyncio.Queue, config: Config):

    self.central_message_queue: asyncio.Queue = central_message_queue
    self.logger = logging.getLogger('GWLogger')

    # ...... snip ......

async def start_device_server(self, tag_config: dict) -> asyncio.Queue:

    # Message queue used for passing messages to running device features (instrument readings)
    name = tag_config["Name"]
    uuid = tag_config["UUID"]
    cfg = tag_config["FeatureConfig"]

    # SiLA Server Setup [This is what I want to mock]
    self.device_server = SiLA2Server(name=name,
                                     description="Provides Device specific functions",
                                     server_uuid=uuid,
                                     port=self.device_server_port,
                                     ip=self.device_server_address,
                                     simulation_mode=False,
                                     key_file=None,
                                     cert_file=None)

    # ........ snip .......

还有一个示例测试-我要做的就是用替换掉SilaServerMagicMock这样我就可以验证是否已使用正确的参数调用了方法

import asynctest, asyncio
from tests.util.MockConfig import build_mock_config
from unittest.mock import patch, MagicMock
from services.device.DeviceService import DeviceService

class DeviceServiceTests(asynctest.TestCase):

@patch("services.device.DeviceService.SiLA2Server")
async def test_device_info_load(self, mock_sila_server_class):

    mock_sila_server_instance = MagicMock()
    mock_sila_server_class.return_value = mock_sila_server_instance
    message_queue = asyncio.Queue()
    device_service = DeviceService(message_queue, build_mock_config())
    device_service.logger = MagicMock()

    await device_service.start_device_server(empty_tag_config)

    # Test feature has been set
    mock_sila_server_instance.add_feature.assert_called_with(data_path='',
                                                             feature_id='DeviceInfo',
                                                             servicer=None)

    # Check that server was started
    mock_sila_server_instance.run.assert_called_with(block=False)

和项目结构

项目结构

由于未使用模拟程序,因此测试运行并失败,并且我得到了您所期望的断言异常,添加了一个断点并device_service使用实际类检查了

被检查

我觉得这确实很明显,但我看不到,我有其他类似的测试可以很好地与@patch装饰器配合使用,但是在这些测试中的应用方式似乎没有任何区别

任何帮助都非常感谢

用户名

弄清楚了,我在混合asynctest和标准的unittest库

这行:

from unittest.mock import patch, MagicMock

应该:

from asynctest.mock import patch, MagicMock

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python 单元测试:模拟补丁

来自分类Dev

Python单元测试补丁函数-避免将模拟函数传递给测试函数

来自分类Dev

python单元测试补丁模拟方法不返回返回值

来自分类Dev

Python-单元测试

来自分类Dev

模块的Python单元测试

来自分类Dev

Python:测试输出(...不是单元测试?)

来自分类Dev

Python单元测试无法识别测试

来自分类Dev

在Python单元测试中测试stdout

来自分类Dev

Python单元测试结果文件

来自分类Dev

Python中的单元测试理论?

来自分类Dev

类内部的python单元测试方法

来自分类Dev

单元测试实现Python属性

来自分类Dev

python中“ FileNotFoundError”的单元测试

来自分类Dev

单元测试python中的软断言

来自分类Dev

如何对python进程进行单元测试

来自分类Dev

不带Django的Python单元测试

来自分类Dev

在哪里放置python单元测试

来自分类Dev

Python RESTful API的单元测试

来自分类Dev

Python单元测试断言错误

来自分类Dev

Python单元测试模拟错误

来自分类Dev

Python属性和单元测试TestCase

来自分类Dev

单元测试C生成python代码

来自分类Dev

GAE Python单元测试亚军

来自分类Dev

模拟python单元测试中的问题

来自分类Dev

使用Python的BigQuery单元测试

来自分类Dev

无法识别Python Pandas单元测试

来自分类Dev

单个python文件的单元测试

来自分类Dev

Python的单元测试框架TestCase好奇

来自分类Dev

Python单元测试结果文件