Skip to content
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

running shared tests while using hypothesis and pytest-asyncio throws an Event loop is closed error #3122

Closed
danielknell opened this issue Oct 19, 2021 · 2 comments

Comments

@danielknell
Copy link
Contributor

if you have a class containing some shared tests (e.g. for contract testing between several implementations of an interface), and use both hypothesis and pytest-asyncio on the shared tests, pytest will raise an error when running the test for the second time.

i have no idea what the inner workings are here but it looks like the corountine is associated to an event loop, then cached and it tries to run it again after that event loop is closed?

removing either hypothesis or pytest-asyncio makes it work as expected, its only when both are in play that the error appears.

minimal test case

import hypothesis
import hypothesis.strategies
import pytest


class Shared:
    @pytest.mark.asyncio
    @hypothesis.given(value=hypothesis.strategies.integers())
    async def test_foo(self, value: int) -> None:
        assert value == value


class TestCaseOne(Shared):
    pass


class TestCaseTwo(Shared):
    pass

output

$pytest test_bug.py
================================ test session starts =================================
platform linux -- Python 3.9.7, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /workspace, configfile: pyproject.toml
plugins: asyncio-0.15.1, hypothesis-6.14.5, anyio-3.3.0
collected 2 items                                                                    

test_bug.py .F                                                                 [100%]

====================================== FAILURES ======================================
________________________________ TestCaseTwo.test_foo ________________________________

self = <test_bug.TestCaseTwo object at 0xffffb3c18850>

    @pytest.mark.asyncio
>   @hypothesis.given(value=hypothesis.strategies.integers())

test_bug.py:8: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/home/vscode/.cache/pypoetry/virtualenvs/api-xS3fZVNL-py3.9/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191: in inner
    coro = func(**kwargs)
/home/vscode/.cache/pypoetry/virtualenvs/api-xS3fZVNL-py3.9/lib/python3.9/site-packages/pytest_asyncio/plugin.py:193: in inner
    task = asyncio.ensure_future(coro, loop=_loop)
/usr/local/lib/python3.9/asyncio/tasks.py:668: in ensure_future
    task = loop.create_task(coro_or_future)
/usr/local/lib/python3.9/asyncio/base_events.py:431: in create_task
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

/usr/local/lib/python3.9/asyncio/base_events.py:510: RuntimeError
------------------------------------- Hypothesis -------------------------------------
Falsifying example: test_foo(
    value=0, self=<test_bug.TestCaseTwo at 0xffffb3c18850>,
)
============================== short test summary info ===============================
FAILED test_bug.py::TestCaseTwo::test_foo - RuntimeError: Event loop is closed
============================ 1 failed, 1 passed in 0.20s =============================

environment

hypothesis: 6.23.3
pytest: 6.2.4
pytest-asyncio: 0.15.1
@danielknell
Copy link
Contributor Author

danielknell commented Oct 19, 2021

@pytest.fixture
def event_loop(request):
    yield asyncio.get_event_loop()

seems to work as a workaround, but I'm assuming theres good reason for creating a new event loop each test...

@Zac-HD
Copy link
Member

Zac-HD commented Oct 19, 2021

This is probably a pytest-asyncio error - I know that they have Hypothesis integration, but we don't have any special logic for asyncio. Wish I could be more helpful 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants