Replacing the event_loop fixture with a custom implementation is deprecated

74 Views Asked by At

I have a quite simple test database setup (pytest-asyncio 0.23.5.port1, pytest 8.1.1):

conftest.py

import asyncio
import os
from pathlib import Path

import pytest
from alembic import command
from alembic.config import Config
from sqlalchemy_utils import database_exists, create_database, drop_database

from app.main.conf import settings
from app.main.database import session_factory, engine


@pytest.fixture(scope="session")
def event_loop():
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture(scope="session")
def alembic_config():
    base_dir = Path(__file__).resolve().parent.parent
    alembic_cfg = Config(os.path.join(base_dir, "alembic.ini"))
    yield alembic_cfg


@pytest.fixture(scope="session")
def db(alembic_config):
    url = settings.DB.sync_url
    if database_exists(url):
        raise ValueError(
            f"База данных для тестов {settings.DB.NAME} уже существует. "
            f"Удалите ее и перезапустите тесты."
        )
    create_database(url)
    command.upgrade(alembic_config, "head")
    yield
    drop_database(url)


@pytest.fixture
async def session(db):
    async with engine.begin() as connection:
        session = session_factory(bind=connection)
        yield session
        await session.close()
        await connection.rollback()

pyproject.toml:

[tool.pytest.ini_options]
testpaths = [
    "src/tests",
]
pythonpath = ["src"]
required_plugins = [
    "pytest-asyncio",
    "pytest-cov",
    "pytest-mock",
]
asyncio_mode = "auto"
env = [
    "DB_DATABASE=nkz-profiles-new-tests"
]

A tests:

from sqlalchemy import select

from app.models import Country


async def test_1(session):
    stmt = select(Country)
    result = await session.scalars(stmt)
    assert list(result.all()) == []


async def test_create(session):
    country = Country(name="12w", code="cacsas")
    session.add(country)
    await session.commit()


async def test_2(session):
    stmt = select(Country)
    result = await session.scalars(stmt)
    assert list(result.all()) == []

When I run tests pytest-asyncio generates warning:

src/tests/usecases/libs/list_document_types/test_view.py::test_1
/Users/alber.aleksandrov/PycharmProjects/nkz-profiles-new/.venv/lib/python3.11/site-packages/pytest_asyncio/plugin.py:769: DeprecationWarning: The event_loop fixture provided by pytest-asyncio has been redefined in
/Users/alber.aleksandrov/PycharmProjects/nkz-profiles-new/src/tests/conftest.py:14 Replacing the event_loop fixture with a custom implementation is deprecated and will lead to errors in the future. If you want to request an asyncio event loop with a scope other than function
scope, use the "scope" argument to the asyncio mark when marking the tests. If you want to return different types of event loops, use the event_loop_policy fixture.

How to fix my code to skip this warning?

0

There are 0 best solutions below