-
-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to spy call of async/await function? #60
Comments
solution was much simpler than I thought: @pytest.mark.asyncio
async def test_async_func2(mocker):
mock_async_func2 = mocker.patch(__name__ + '.async_func2')
async def return_async_value(val):
return val
mock_async_func2.return_value = return_async_value('something')
res = await async_func1()
mock_async_func2.assert_called_once_with()
assert res == 'something' thanks! :) |
but I can only run the async_func1 once or I got an error async def async_func2():
pass
async def async_func1():
await async_func2()
@pytest.mark.asyncio
async def test_async_func2(mocker):
mock_async_func2 = mocker.patch(__name__ + '.async_func2')
async def return_async_value(val):
return val
mock_async_func2.return_value = return_async_value('something')
res = await mock_async_func2()
res = await mock_async_func2()
mock_async_func2.assert_called_once_with()
assert res == 'something' run pytest and got
|
finally I found a solution async def async_func2():
pass
async def async_func1():
await async_func2()
@pytest.mark.asyncio
async def test_async_func2(mocker):
mock_async_func2 = mocker.patch(__name__ + '.async_func2')
def return_async_value(val):
f = asyncio.Future()
f.set_result(val)
return f
mock_async_func2.return_value = return_async_value('something')
res = await mock_async_func2()
print(res)
res = await mock_async_func2()
print(res)
# mock_async_func2.assert_called_once_with()
assert res == 'something' this idea come from discussion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to do this:
But got:
My goal is to mock
async_func2
function and catch all calls of it. How can I archive it with this library?The text was updated successfully, but these errors were encountered: