Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#1,463 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
testing-qa

Hướng nghiên cứu

Tái hiện vấn đề bằng các ví dụ conftest.py và test_async.py, sau đó theo dõi cách pytest-asyncio xử lý parametrized asyncio marks và cấu hình loop-factory. Công việc được coi là hoàn tất khi các parametrized tests được đánh dấu bằng pytest.mark.asyncio sử dụng uvloop factory đã cấu hình, trong khi các parametrized runners khác vẫn tiếp tục hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
1.7k
Fork
207
Merge trung bình
5 giờ 2 phút
Pull request đã merge (30 ngày)
11

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pytest-dev/pytest-asyncio

Tất cả issue của pytest-dev/pytest-asyncio

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.