Skip to content

test passes but ERROR is thrown when closing a generator, only shows up in pytest-asyncio pytest run #203

Closed
@jreiberkyle

Description

@jreiberkyle

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions