pytest - Python asynctest 模拟补丁装饰器溢出到后续测试中
问题描述
我正在尝试测试异步功能。为此,我正在使用pytest-asyncio
and asynctest
。我需要检查在我正在测试的函数中使用的函数被调用了多少次。为此,我正在使用aynsctest.mock.patch()
. 我的测试文件如下所示:
@pytest.mark.asyncio
@asynctest.mock.patch('my_module.function_name')
async def test_function_called_twice(function_mock):
await function_to_be_tested()
assert function_mock.call_count == 2
@pytest.mark.asyncio
@asynctest.mock.patch('my_module.function_name')
async def test_function_called_once(function_mock):
await function_to_be_tested()
assert function_mock.call_count == 1
如果我单独运行测试用例,它们就会通过。但是,当我运行整个模块时,要运行的第二个测试用例会失败 - 无论它们运行的顺序如何。我怀疑这是因为第一个测试用例的模拟仍然影响要运行的第二个测试用例。
我在这里做错了什么?
解决方案
推荐阅读
- swift - 屏幕旋转SwiftUI iOS 14后导航栏项目不再出现
- python - SQL 和 Python 出错 - 表或视图不存在
- selenium - 等到页面加载 Selenium Python
- sysinternals - Sysinternals Process Monitor (ProcMon):在过滤器上使用通配符
- plot - GNUplot 3D 绘图
- r - 我怎样才能找到合作?
- reactjs - Display data on detail page using useContext
- postgresql - 复制是否有助于稳定数据库读取服务?
- ios - 如何衡量用户在 webview 中阅读的字数?
- c++ - 配置 Qt 项目(.pro 文件)以使用 PcapPlusPlus