Я думаю, что приведенный ниже пример является действительно распространенным случаем:
- создать соединение с базой данных один раз ,
- передать это соединение для проверкикоторые вставляют данные
- , передают соединение в тест, который проверяет данные.
Изменение области действия @pytest.fixture(scope="module")
вызывает ScopeMismatch: You tried to access the 'function' scoped fixture 'event_loop' with a 'module' scoped request object, involved factories
.
Кроме того,test_insert
и test_find
сопрограмма не нуждается в аргументе event_loop, поскольку цикл доступен уже при передаче соединения.
Есть идеи, как исправить эти две проблемы?
import pytest
@pytest.fixture(scope="function") # <-- want this to be scope="module"; run once!
@pytest.mark.asyncio
async def connection(event_loop):
""" Expensive function; want to do in the module scope. Only this function needs `event_loop`!
"""
conn await = make_connection(event_loop)
return conn
@pytest.mark.dependency()
@pytest.mark.asyncio
async def test_insert(connection, event_loop): # <-- does not need event_loop arg
""" Test insert into database.
NB does not need event_loop argument; just the connection.
"""
_id = 0
success = await connection.insert(_id, "data")
assert success == True
@pytest.mark.dependency(depends=['test_insert'])
@pytest.mark.asyncio
async def test_find(connection, event_loop): # <-- does not need event_loop arg
""" Test database find.
NB does not need event_loop argument; just the connection.
"""
_id = 0
data = await connection.find(_id)
assert data == "data"