So let’s assume that I try to retrieve all users for Pytest tests as a fixture:
@pytest_asyncio.fixture(scope="function")
async def test_users(db_session):
async with db_session.begin():
cursor = await db_session.execute(
select(User)
)
return cursor.scalars().fetchall()
The thing is that when I try to access objects’ attributes in another function (fixture, for example), I get an error sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called.
@pytest_asyncio.fixture(scope="function")
async def another_fixture(test_users: List[User]):
print(test_users[0].id) # sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called
Do I do something wrong? I tried to user db_session.refresh() on each object, but it didn’t work.