I am running a FastAPI application with PostgreSQL database and SQLAlchemy. I used sessionmaker to create and use database sessions, and I have a custom alembic migration script that needs to be performed on the database. from app.core.config import DATABASE_URL from app.models import User, Passwords from app.db import Base from alembic import context config = context.config config.set_main_option('sqlalchemy.url', DATABASE_URL) fileConfig(config.config_file_name) target_metadata = Base.metadata def run_migrations_offline(): url = config.get_main_option("sqlalchemy.url") context.configure( url=url, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, ) with context.begin_transaction(): context.run_migrations() I want to be able to create a test database, apply the same migration, and teardown after the test. I have tried the following in the conftest.py # Apply migrations at beginning and end of testing session @pytest.fixture(scope="session") def apply_migrations(): warnings.filterwarnings("ignore", category=DeprecationWarning) environ["TEST"] = "True" config = Config("alembic.ini") alembic.command.upgrade(config, "head") yield alembic.command.downgrade(config, "base") @pytest.fixture(scope="session") def create_test_db(): url = f"{str(DATABASE_URL)}_test" if database_exists(url): drop_database(url) create_database(url) apply_migrations() engine = create_engine(url, echo=True) yield drop_database(url) But it does not seem to do anything. I am new to testing and I can't find a decent guide on how to set this up properly. So any kind of help would be greatly appreciated. Continue reading...