You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am running python 3.7.9 and pytest-asyncio 0.14.0 / pytest 6.2.1. When I run the following code in pytest, I get an error thrown even though the test passes.
$>pytest -vv test.py --log-cli-level=DEBUG
test.py
import pytest
@pytest.mark.asyncio
async def test_generator_limit():
async def gen():
yield 1
yield 2
yield 3
yield 4
async for res in gen():
if res == 2:
break
ERROR asyncio:base_events.py:1619 Task was destroyed but it is pending! task: <Task pending coro=<<async_generator_athrow without __name__>()>>
If I run this code as a script, I get no such error
import asyncio
async def test_generator_limit():
async def gen():
yield 1
yield 2
yield 3
yield 4
async for res in gen():
if res == 2:
break
asyncio.run(test_generator_limit(), debug=True)
(NOTE: the value of debug does not change the results)
This error only shows up when I introduce the break. According to PEP 525 this appears to be valid async code.
The text was updated successfully, but these errors were encountered:
jreiberkyle
changed the title
ERROR when closing a generator, only shows up in pytest-asyncio test run
test passes but ERROR is thrown when closing a generator, only shows up in pytest-asyncio pytest run
Jan 30, 2021
Thanks for the report and for the great examples! The issue is still present in pytest-async v0.16.
asyncio.run cleans up any async generator functions before exiting (see #222). This is why you don't see the "Task pending error" when running your second example. pytest-asyncio does no such thing when the event_loop fixture is torn down, resulting in the error you describe.
Issue #222 proposed that pytest-asyncio should behave similar to asyncio.run and clean up existing tasks. We closed that in favour of #235. I hope you don't mind if I do the same here. Let's continue the discussion in #235.
I am running python 3.7.9 and pytest-asyncio 0.14.0 / pytest 6.2.1. When I run the following code in pytest, I get an error thrown even though the test passes.
$>pytest -vv test.py --log-cli-level=DEBUG
test.py
ERROR asyncio:base_events.py:1619 Task was destroyed but it is pending! task: <Task pending coro=<<async_generator_athrow without __name__>()>>
If I run this code as a script, I get no such error
(NOTE: the value of
debug
does not change the results)This error only shows up when I introduce the
break
. According to PEP 525 this appears to be valid async code.The text was updated successfully, but these errors were encountered: