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

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

Closed
medihack opened this issue May 13, 2023 · 1 comment

Comments

@medihack
Copy link

medihack commented May 13, 2023

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: ()
@medihack
Copy link
Author

medihack commented May 14, 2023

After some investigation, there is a simple workaround by installing nest_asyncio and then simply put this in the root conftest.py:

def pytest_configure():
    nest_asyncio.apply()

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

1 participant