Skip to content

Commit 2e46c19

Browse files
SetupError supports custom error message
1 parent 8a3ff74 commit 2e46c19

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tests/conftest.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from typing import Generator
23
from dotenv import load_dotenv
34
from sqlmodel import create_engine, Session, select
45
from sqlalchemy import Engine
@@ -14,7 +15,9 @@
1415
# Define a custom exception for test setup errors
1516
class SetupError(Exception):
1617
"""Exception raised for errors in the test setup process."""
17-
pass
18+
def __init__(self, message="An error occurred during test setup"):
19+
self.message = message
20+
super().__init__(self.message)
1821

1922

2023
@pytest.fixture(scope="session")
@@ -30,7 +33,7 @@ def engine() -> Engine:
3033

3134

3235
@pytest.fixture(scope="session", autouse=True)
33-
def set_up_database(engine):
36+
def set_up_database(engine) -> Generator[None, None, None]:
3437
"""
3538
Set up the test database before running the test suite.
3639
Drop all tables and recreate them to ensure a clean state.
@@ -41,7 +44,7 @@ def set_up_database(engine):
4144

4245

4346
@pytest.fixture
44-
def session(engine):
47+
def session(engine) -> Generator[Session, None, None]:
4548
"""
4649
Provide a session for database operations in tests.
4750
"""
@@ -50,7 +53,7 @@ def session(engine):
5053

5154

5255
@pytest.fixture(autouse=True)
53-
def clean_db(session: Session):
56+
def clean_db(session: Session) -> None:
5457
"""
5558
Cleans up the database tables before each test.
5659
"""
@@ -63,7 +66,7 @@ def clean_db(session: Session):
6366

6467
# Test user fixture
6568
@pytest.fixture()
66-
def test_user(session: Session):
69+
def test_user(session: Session) -> User:
6770
"""
6871
Creates a test user in the database.
6972
"""
@@ -80,7 +83,7 @@ def test_user(session: Session):
8083

8184
# Unauthenticated client fixture
8285
@pytest.fixture()
83-
def unauth_client(session: Session):
86+
def unauth_client(session: Session) -> Generator[TestClient, None, None]:
8487
"""
8588
Provides a TestClient instance without authentication.
8689
"""
@@ -95,7 +98,7 @@ def get_session_override():
9598

9699
# Authenticated client fixture
97100
@pytest.fixture()
98-
def auth_client(session: Session, test_user: User):
101+
def auth_client(session: Session, test_user: User) -> Generator[TestClient, None, None]:
99102
"""
100103
Provides a TestClient instance with valid authentication tokens.
101104
"""
@@ -117,7 +120,7 @@ def get_session_override():
117120

118121

119122
@pytest.fixture
120-
def test_organization(session: Session):
123+
def test_organization(session: Session) -> Organization:
121124
"""Create a test organization for use in tests"""
122125
organization = Organization(name="Test Organization")
123126
session.add(organization)

0 commit comments

Comments
 (0)