pytest-dev / pytest-dev/pytest-asyncio

`pytest_asyncio_loop_factories` hook is not called when `pytest.mark.asyncio` is added via parametrization

Open
#1,463 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
207
Avg merge
5h 35m
Merged PRs (30d)
9

Description

Description

If pytest.mark.asyncio is added via parametrization (pytest.mark.parametrize() or pytest.fixture(params=...)), the test functions ignores the configured loop factories.

Steps to reproduce

In conftest.py:

from typing import Any
import uvloop


def pytest_asyncio_loop_factories() -> dict[str, Any]:
    return {"uvloop": uvloop.new_event_loop}

In test_async.py:

import asyncio
import uvloop
import pytest
import trio


@pytest.mark.parametrize(
    "backend",
    [
        pytest.param("asyncio", marks=pytest.mark.asyncio),
        pytest.param("trio", marks=pytest.mark.trio),  # <- Come from pytest-trio
    ]
)
async def test_async(backend: str) -> None:
    match backend:
        case "asyncio":
            assert isinstance(asyncio.get_running_loop(), uvloop.Loop)
        case "trio":
            assert trio.lowlevel.current_root_task() is not None

Expected the runner to be uvloop.Loop but it uses the default loop.

Workaround

There is a workaround possible to avoid the issue by using the test inheritance:

import asyncio
import uvloop
import pytest
import trio


class _BaseTestAsync:

    async def test_async(self, backend: str) -> None:
        match backend:
            case "asyncio":
                assert isinstance(asyncio.get_running_loop(), uvloop.Loop)
            case "trio":
                assert trio.lowlevel.current_root_task() is not None


@pytest.mark.asyncio
class TestAsyncIO(_BaseTestAsync):

    @pytest.fixture
    @staticmethod
    def backend() -> str:
        return "asyncio"

@pytest.mark.trio
class TestTrio(_BaseTestAsync):

    @pytest.fixture
    @staticmethod
    def backend() -> str:
        return "trio"

But compared to fixture parametrization, it is a bit complicated.

Use case

I have a project which must run with asyncio and trio but do not rely on anyio, therefore I use the fixture parametrization to test on all supported runners.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the conftest.py and test_async.py examples, then trace pytest-asyncio's handling of parametrized asyncio marks and loop-factory configuration. Done means parametrized tests marked with pytest.mark.asyncio use the configured uvloop factory while other parametrized runners continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.