33
44import pytest
55from _pytest .config import Config
6- from _pytest .nodes import Item
76from fastapi import FastAPI
87from fastapi .testclient import TestClient
98from sqlmodel import Session , delete
109
1110from app .core .factory import create_app
1211from app .resource_adapters .persistence .sqlmodel .database import get_engine
1312from app .resource_adapters .persistence .sqlmodel .issues import Issue
13+ from config import settings
1414
1515
16- def pytest_configure (config : Config ) -> None :
17- """Register env marker."""
18- config .addinivalue_line (
19- "markers" , "env(name): mark test to run only on named environment"
20- )
21-
22-
23- def pytest_runtest_setup (item : Item ) -> None :
24- """Set up environment for each test based on env marker."""
25- envnames = [mark .args [0 ] for mark in item .iter_markers (name = "env" )]
26- if envnames :
27- # Set environment to the first env marker found
28- os .environ ["APP_ENV" ] = envnames [0 ]
29- else :
30- # Default to testing environment if no env marker
31- os .environ ["APP_ENV" ] = "testing"
16+ @pytest .fixture (scope = "session" , autouse = True )
17+ def set_test_settings ():
18+ """Fixture to force the use of the 'testing' environment for all tests."""
19+ settings .configure (FORCE_ENV_FOR_DYNACONF = "testing" )
3220
3321
3422def pytest_unconfigure (config : Config ) -> None :
@@ -46,7 +34,10 @@ async def test_lifespan(app: FastAPI):
4634
4735
4836# Create test app using the factory with test lifespan
49- app = create_app (lifespan_handler = test_lifespan )
37+ @pytest .fixture (name = "app" )
38+ def test_app ():
39+ """Create test app instance only during test execution."""
40+ return create_app (lifespan_handler = test_lifespan )
5041
5142
5243@pytest .fixture (name = "session" )
@@ -60,6 +51,6 @@ def test_session():
6051
6152
6253@pytest .fixture (name = "client" )
63- def client_fixture ():
54+ def client_fixture (app : FastAPI ):
6455 with TestClient (app ) as client :
6556 yield client
0 commit comments