Add pytest fixture for database session in tests
Introduce a `db_session` pytest fixture to provide a fresh SQLite in-memory database for each test function. This ensures isolated and consistent database state across tests by setting up and tearing down the database automatically.
This commit is contained in:
22
backend/tests/conftest.py
Normal file
22
backend/tests/conftest.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# tests/conftest.py
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.utils.test_utils import setup_test_db, teardown_test_db
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
"""
|
||||
Creates a fresh SQLite in-memory database for each test function.
|
||||
|
||||
Yields a SQLAlchemy session that can be used for testing.
|
||||
"""
|
||||
# Set up the database
|
||||
test_engine, TestingSessionLocal = setup_test_db()
|
||||
|
||||
# Create a session
|
||||
with TestingSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
# Clean up
|
||||
teardown_test_db(test_engine)
|
||||
Reference in New Issue
Block a user