trio icon indicating copy to clipboard operation
trio copied to clipboard

testing: "There is no current event loop in thread 'MainThread'."

Open alexted opened this issue 4 years ago • 0 comments

Hello! I'm using: manjaro linux 21.2.1, python 3.10.1, fastapi 0.72.0, pytest 6.2.5, pytest-trio 0.7.0, sqlalchemy 1.4.31, alembic 1.7.5, asyncpg 0.25.0, postgresql 14.1. Now, i have issue when i try to execute my tests, which work correct with fastapi <= 0.68 .

I have a two modules of unit-tests:

test_sum.py

import pytest
from httpx import AsyncClient

pytestmark = pytest.mark.anyio


async def test_sum(app):
    async with AsyncClient(app=app, base_url='http://127.0.0.1:5000') as ac:
        response = await ac.post('/v1/sum', json={"x": 4, "y": 6})

    assert response.status_code == 200
    assert response.json() == {"sum": 10}

test_items.py

import pytest
from httpx import AsyncClient

from tests.expected_data import created_item
from tests.moks_data import items

pytestmark = pytest.mark.anyio


async def test_create_item(app):
    async with AsyncClient(app=app, base_url='http://test') as ac:
        response = await ac.post('/v1/items', json={
            "name": "TestName",
            "description": "Test description",
            "price": 100
        })

    assert response.status_code == 200
    assert response.json() == created_item


async def test_get_item(fill_db, app):
    async with AsyncClient(app=app, base_url='http://test') as ac:
        response = await ac.get('/v1/items/2')

    assert response.status_code == 200
    assert response.json() == items[1]

conftest.py

import pytest
from alembic.command import downgrade, upgrade
from alembic.config import Config as AlembicConfig

from core.app.web import create_app
from core.data.postgres.engine import session_scope
from core.data.postgres.models import Base, Item
from tests.moks_data import items


@pytest.fixture
def app():
    app_instance = create_app()
    return app_instance

@pytest.fixture(scope='session')
def db_session():
    config = AlembicConfig('alembic.ini')
    config.attributes['configure_logger'] = False

    upgrade(config, 'head')

    yield 'on head'

    downgrade(config, 'base')


@pytest.fixture(autouse=True)
async def clear_data(db_session):
    yield 'I will clear tables for you'

    reference_value_tables = [f'{name}' for name in ('role',)]

    async with session_scope() as session:
        for name, table in Base.metadata.tables.items():
            if name not in reference_value_tables:
                await session.execute(table.delete())


@pytest.fixture
async def fill_db(event_loop):
    items_list = [Item(**item) for item in items]
    async with session_scope() as session:
        session.add_all(items_list)
        await session.flush()

When i run my tests - tests in test_sum is successfully ended but tests in test_items is failed with error: There is no current event loop in thread 'MainThread'.

Please tell me what I'm doing wrong and how to solve this problem?

alexted avatar Jan 21 '22 14:01 alexted