# https://itnext.io/setting-up-transactional-tests-with-pytest-and-sqlalchemy-b2d726347629 import pytest import sys sys.path.append("app") import models # noqa E402 (import not at top of file) from sqlalchemy import create_engine # noqa E402 from sqlalchemy.orm import ( # noqa E402 scoped_session, Session, sessionmaker, ) @pytest.fixture(scope="session") def engine(): return create_engine( "mysql+mysqldb://dev_urma_testing:dev_urma_testing@" "localhost/dev_urma_testing", encoding='utf-8', pool_pre_ping=True, future=True ) @pytest.fixture(scope="session") def setup_database(engine): """ Made scope=function (the default) to ensure any committed objects are removed """ from app.models import Base # noqa E402 Base.metadata.create_all(engine) # seed_database() yield Base.metadata.drop_all(engine) @pytest.fixture def session(setup_database, engine): session = scoped_session(sessionmaker(autoflush=False, bind=engine)) session.begin() yield session session.rollback()