python - 如何模拟同一类的嵌套函数调用
问题描述
在帖子中如何模拟嵌套函数?用户模拟一系列用户调用。就我而言,我的情况如下:
class myClass:
def __init__(self, my_type):
self.type = my_type
def output(self):
self.create_figure()
def create_figure():
if self.type == 'A':
do_A()
elif self.type == 'B':
do_B()
def do_A():
pass
def do_B():
pass
我正在尝试编写一个单元测试来测试是否调用了正确的东西。例如:我想确保当用户调用时output
,myClass('A')
调用create_figure
和do_A()
调用。
我目前正在使用 pytest 进行如下测试:
import myModule, pytest
from unittest.mock import patch
@pytest.fixture()
def default_my_class():
return myModule.myClass(my_type='A')
@patch('myModule.myClass.do_A')
@patch('myModule.myClass.create_figure')
def test_chart(mock_create_figure, mock_do_a, default_my_class):
default_my_class.output()
mock_create_figure.assert_called_once()
mock_do_a.assert_called_once()
解决方案
推荐阅读
- ios - 如何在 iOS 13 的 AVPlayer 中播放不安全的视频?
- c# - 如何从签名邮件中获取 .msg 文件?
- powershell - 以有效的方式将哈希表转换回字符串数据
- react-native - 如何在 React-Native 中的抽屉导航中的屏幕之间传输数据
- java - 如何在没有 Eclipse 的情况下运行 Jsp 项目
- node.js - 上传大量文件时节点脚本导致系统冻结
- jquery - 带有 POSTMAN 的 asp.net 核心 API 工作正常,但使用 jquery ajax 不起作用?
- docker - Artifactory oss 与 Docker 一起安装
- ruby - 无法构建 gem 原生扩展(安装 ruby-debug-ide-0.7.1.beta3.gem)
- java - 排除 Maven 中的传递阴影依赖