`pytest_asyncio_loop_factories` hook is not called when `pytest.mark.asyncio` is added via parametrization
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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của pytest-dev/pytest-asyncio
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
pytest-dev/pytest-asyncio#1475 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 56/100
pytest-dev/pytest-asyncio#1514 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
pytest-dev/pytest-asyncio#1501 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Don't warn about `asyncio_default_fixture_loop_scope` unless the test suite uses `pytest-asyncio`Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
pytest-dev/pytest-asyncio#1344 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
pytest-dev/pytest-asyncio#1334 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của pytest-dev/pytest-asyncio
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
spec-kitty/spec-kitty#5319 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend::vllm diffusion multimodal
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
openai/openai-agents-python#5229 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày