gpt4 book ai didi

python-3.x - python : How do I mock an async method that gets called multiple times?

转载 作者:行者123 更新时间:2023-12-04 13:43:10 24 4
gpt4 key购买 nike

我有一种用于在 Python (3.6) 中测试异步代码的方法:

@asyncio.coroutine
def coroutine_creater(value):
return value

我像这样使用它:
async def test_test():
my_mock = Mock(return_value=coroutine_creater(5))

# I call with "await"
first_call = await my_mock()
second_call = await my_mock()

assert first_call == 5, 'first call failed'
assert second_call == 5, 'second call failed' # this call fails

这样我就可以为异步调用创建模拟。我发现如果我调用 async 方法两次,这将不起作用。在我的代码中, first_call像我期望的那样等于 5,但是 second_call等于无。这里发生了什么?如何测试多次调用 Mock 异步方法的代码?

最佳答案

您应该设置 side_effect Mock 的论据.

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT, the return value of this function is used as the return value.


下面的示例使用 pytest-asynciopytest模块: code_53856568.py :
import asyncio


@asyncio.coroutine
def coroutine_creater(value):
return value
test_code_53856568.py :
from unittest.mock import Mock
from code_53856568 import coroutine_creater
import pytest


@pytest.mark.asyncio
async def test_test():
def coroutine_creater_side_effect():
return coroutine_creater(5)

my_mock = Mock(side_effect=coroutine_creater_side_effect)

first_call = await my_mock()
second_call = await my_mock()

assert first_call == 5, 'first call failed'
assert second_call == 5, 'second call failed'
带有覆盖率报告的单元测试结果:
(venv) ☁  python-codelab [master] ⚡  coverage run -m pytest /Users/ldu020/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/53856568/test_code_53856568.py && coverage report -m --include="src/*"
===================================================================================================================== test session starts =====================================================================================================================
platform darwin -- Python 3.7.5, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /Users/ldu020/workspace/github.com/mrdulin/python-codelab
plugins: asyncio-0.10.0
collected 1 item

src/stackoverflow/53856568/test_code_53856568.py . [100%]

====================================================================================================================== 1 passed in 0.04s ======================================================================================================================
Name Stmts Miss Cover Missing
--------------------------------------------------------------------------------
src/stackoverflow/53856568/code_53856568.py 3 0 100%
src/stackoverflow/53856568/test_code_53856568.py 11 0 100%
--------------------------------------------------------------------------------
TOTAL

关于python-3.x - python : How do I mock an async method that gets called multiple times?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53856568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com