I want to create a mongodb session with motor that will start and end with the tests with pytest and the anyio pytest plugin. But after tests are run, the entries persist in the database.
# db.py
mongo_client = AsyncIOMotorClient('conn url')
# conftest.py
from .db import mongo_client
@pytest.fixture(scope='session')
def anyio_backend() -> str:
return 'asyncio'
@pytest.fixture(scope='session', autouse=True)
async def mongo_session(anyio_backend) -> AsyncGenerator[None, None]:
async with await mongo_client.start_session() as session:
session.start_transaction()
yield
await session.abort_transaction()
# test_mongo.py
from .db import mongo_client
async def test_add_item() -> None:
# add item into db from mongo_cilent
I expected to see no entries in the database after running tests, but I see entries. Meaning this fixture was not called.