willemt/pytest-asyncio-cooperative

pytest-asyncio-cooperative is not incompatible with pytest-xdist and got PytestUnhandledCoroutineWarning during run cases

Closed this issue · 1 comments

Repro Code:

test_tmp.py

import logging
import pytest
import asyncio
import time


@pytest.fixture(scope="session", params=["A"])
def fixture_A(request):
    return request.param


@pytest.fixture(scope="session", params=["B"])
def fixture_B(fixture_A, request):
    return request.param


@pytest.mark.asyncio_cooperative
async def test_async_01(fixture_B):
    param = fixture_B
    logging.info(f"time => {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}")
    await asyncio.sleep(5)


@pytest.mark.asyncio_cooperative
async def test_async_02(fixture_B):
    param = fixture_B
    logging.info(f"time => {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}")
    await asyncio.sleep(5)

Output Message With pytest command: pytest tests/test_tmp.py -n 2

[gw0] Python 3.8.5 (default, Jul 28 2020, 12:59:40)  -- [GCC 9.3.0]
[gw1] Python 3.8.5 (default, Jul 28 2020, 12:59:40)  -- [GCC 9.3.0]
gw0 [2] / gw1 [2]
scheduling tests via LoadScheduling
/home/hhb/.virtualenvs/haa_test/lib/python3.8/site-packages/_pytest/python.py:172: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
You need to install a suitable plugin for your async framework, for example:
  - anyio
  - pytest-asyncio
  - pytest-tornasync
  - pytest-trio
  - pytest-twisted
  warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))
/home/hhb/.virtualenvs/haa_test/lib/python3.8/site-packages/_pytest/python.py:172: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
You need to install a suitable plugin for your async framework, for example:
  - anyio
  - pytest-asyncio
  - pytest-tornasync
  - pytest-trio
  - pytest-twisted
  warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

hi @SilenceHe
I’m going close this as wontfix. Also, I will add an error when xdist with -n > 1 is detected.

pytest-xdist takes over runtestloop over here: https://github.com/pytest-dev/pytest-xdist/blob/1dc019709c15678faa622834d3bc851abddb426c/src/xdist/remote.py#L56

It runs a while loop on each item it needs to test here: https://github.com/pytest-dev/pytest-xdist/blob/1dc019709c15678faa622834d3bc851abddb426c/src/xdist/remote.py#L72

async-cooperative needs to control runtestloop itself so that it can run await asyncio.wait over a list of tasks. This means xdist and async-cooperative are incompatible.

We need to send a PR to xdist to support async-cooperative.