Skip to content

Cannot run the event loop when using pytest asyncio tests after Playwright tests #167

Closed
@medihack

Description

@medihack

When I run async tests using pytest-asyncio (marked with @pytest.mark.asyncio) after integration tests using playwright-pytest (which only uses playwright.sync_api calls), then those tests fail with the below error message. If I run the async tests before the integration tests then everything works fine.
Why is there still some event loop running after the integration tests finished even if I don't use async calls with Playwright?

Edit:
After some research, it seems to be a known problem, which is simply an incompatibility between pytest-asyncio and playwright-pytest. playwright-pytest seems to start an event loop (even when using the sync API) and pytest-asyncio doesn't get along with an already started one.

See pytest-dev/pytest-asyncio#359

args = (), kwargs = {}, coro = <coroutine object test_start_transmit_file at 0x7f1c08343f40>
task = <Task pending name='Task-10' coro=<test_start_transmit_file() running at /workspaces/adit/adit/core/tests/utils/test_file_transmit.py:18>>

    @functools.wraps(func)
    def inner(*args, **kwargs):
        coro = func(*args, **kwargs)
        if not inspect.isawaitable(coro):
            pyfuncitem.warn(
                pytest.PytestWarning(
                    f"The test {pyfuncitem} is marked with '@pytest.mark.asyncio' "
                    "but it is not an async function. "
                    "Please remove asyncio marker. "
                    "If the test is not marked explicitly, "
                    "check for global markers applied via 'pytestmark'."
                )
            )
            return
        task = asyncio.ensure_future(coro, loop=_loop)
        try:
>           _loop.run_until_complete(task)

/opt/pysetup/.venv/lib/python3.10/site-packages/pytest_asyncio/plugin.py:525: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.10/asyncio/base_events.py:625: in run_until_complete
    self._check_running()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

    def _check_running(self):
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
>           raise RuntimeError(
                'Cannot run the event loop while another loop is running')
E           RuntimeError: Cannot run the event loop while another loop is running

/usr/local/lib/python3.10/asyncio/base_events.py:586: RuntimeError
======================================================================== short test summary info =========================================================================
FAILED adit/core/tests/utils/test_file_transmit.py::test_start_transmit_file - RuntimeError: Cannot run the event loop while another loop is running
====================================================================== 1 failed, 1 passed in 22.01s ======================================================================
--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/logging/__init__.py", line 1103, in emit
    stream.write(msg + self.terminator)
  File "/opt/pysetup/.venv/lib/python3.10/site-packages/colorama/ansitowin32.py", line 47, in write
    self.__convertor.write(text)
  File "/opt/pysetup/.venv/lib/python3.10/site-packages/colorama/ansitowin32.py", line 177, in write
    self.write_and_convert(text)
  File "/opt/pysetup/.venv/lib/python3.10/site-packages/colorama/ansitowin32.py", line 205, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "/opt/pysetup/.venv/lib/python3.10/site-packages/colorama/ansitowin32.py", line 210, in write_plain_text
    self.wrapped.write(text[start:end])
ValueError: I/O operation on closed file.
Call stack:
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1784, in call_exception_handler
    self.default_exception_handler(context)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1758, in default_exception_handler
    logger.error('\n'.join(log_lines), exc_info=exc_info)
Message: "Task was destroyed but it is pending!\ntask: <Task pending name='Task-10' coro=<test_start_transmit_file() running at /workspaces/adit/adit/core/tests/utils/test_file_transmit.py:18>>"
Arguments: ()

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