Skip to content

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

@danielknell

Description

@danielknell

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

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