How to create transactional fixture with peewee_async and pytest

337 views Asked by At

I want to use transactions for tests with the pytest's fixtures as follows:

import pytest
from app.db.clients import (
    get_database,  # Returns instance of the class PooledMySQLDatabase
    BaseManager  # Extends the class peewee_async.Manager
)

db = get_database()
db.set_allow_sync(False)

objects = BaseManager(database=db)

@pytest.yield_fixture
async def transactional():
    async with objects.transaction() as trx:
        yield trx
        await trx.rollback()

@pytest.mark.usefixtures('transactional')
@pytest.mark.asyncio:
async def test_transactional_fixture():
    pass  # Do something with the objects

However the code above doesn't work as expected in return tests are collected but aren't executing. Looks like pytest tries to yield tests infinite. I have no idea how to run transactional tests with such technology stack. Could someone help me, please?

enter image description here

P.S. The code snippet above is just represention of the project's workflow (screenshot was taken from real project).

0

There are 0 answers