Fixture set up and tear down in pytest

July 25, 2024

 
@pytest.fixture(scope="session", autouse=True)
def fixt_datastore(
    fixt_datastore_settings: DatabaseConnectionSettings,
) -> Generator[Datastore, None, None]:
    datastore = create_datastore(fixt_datastore_settings)
 
    with datastore._session_factory.get_session() as session:
        engine = session.get_bind()
        assert engine.url.drivername == "sqlite"
 
    datastore.create_all_models()
    yield datastore
    datastore.drop_all_models()

Autouse:

Fixture factory:

Sharing in-memory sqlite database across tests: