import pytest import subprocess import time import requests import os import signal import sys # Define URLs BACKEND_URL = "http://localhost:8000" FRONTEND_URL = "http://localhost:5173" @pytest.fixture(scope="session") def app_process(): """ Starts the application using start_app.sh and stops it after tests. """ print("\n[Fixture] Starting application...") project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) start_script = os.path.join(project_root, "start_app.sh") stop_script = os.path.join(project_root, "stop_app.sh") # Start the app subprocess.run([start_script], cwd=project_root, check=True) # Wait for Backend and Frontend to be ready if not wait_for_service(BACKEND_URL, timeout=30): pytest.fail("Backend failed to start.") if not wait_for_service(FRONTEND_URL, timeout=30): pytest.fail("Frontend failed to start.") yield # Stop the app print("\n[Fixture] Stopping application...") subprocess.run([stop_script], cwd=project_root, check=True) def wait_for_service(url, timeout=30): """Polls a service URL until it returns 200 or timeout expires.""" start_time = time.time() while time.time() - start_time < timeout: try: response = requests.get(url) if response.status_code == 200: print(f"Service {url} is ready.") return True except requests.ConnectionError: pass time.sleep(1) print(f"Service {url} timed out.") return False