Skip to content

Instantly share code, notes, and snippets.

@fmind
Last active January 7, 2022 08:25
Show Gist options
  • Save fmind/184609dd8b1519893945e133d3639a9e to your computer and use it in GitHub Desktop.
Save fmind/184609dd8b1519893945e133d3639a9e to your computer and use it in GitHub Desktop.
Snippets for Pytest fixtures.
"""Declare fixtures for SQLAlchemy (session and engine)."""
# IMPORTS
import sqlalchemy as sql
from sqlalchemy import orm
from project import models # TODO
# FIXTURES
@pytest.fixture(scope="function")
def engine() -> sql.engine.Engine:
"""Create an SQLAlchemy engine for testing purposes."""
engine = sql.create_engine("sqlite:///:memory:")
return engine
@pytest.fixture(scope="function")
def session(engine: sql.engine.Engine) -> T.Iterator[orm.Session]:
"""Create an SQLAlchemy session for testing purposes."""
models.Base.metadata.create_all(engine)
with orm.Session(engine) as session_:
yield session_
session_.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment