#!/bin/bash # A script to automate running tests locally. # It starts the FastAPI server, runs the specified tests, and then shuts down the server. # --- Configuration --- # Set the default path for tests to run. This will be used if no argument is provided. DEFAULT_TEST_PATH="integration_tests/" # You can override the default with a command-line argument, e.g., './run_integration_tests.sh tests/test_app.py' TEST_PATH=${1:-$DEFAULT_TEST_PATH} echo "--- Starting AI Hub Server for Tests ---" # Start the uvicorn server in the background # We bind it to 127.0.0.1 to ensure it's not accessible from outside the local machine. uvicorn app.main:app --host 127.0.0.1 --port 8000 & # Get the Process ID (PID) of the background server SERVER_PID=$! # Define a cleanup function to be called on exit cleanup() { echo "" echo "--- Shutting Down Server (PID: $SERVER_PID) ---" kill $SERVER_PID } # Register the cleanup function to run when the script exits # This ensures the server is stopped even if tests fail or the script is interrupted (e.g., with Ctrl+C). trap cleanup EXIT echo "Server started with PID: $SERVER_PID. Waiting for it to initialize..." # Wait a few seconds to ensure the server is fully up and running sleep 5 echo "--- Running tests in: $TEST_PATH ---" # Execute the Python tests using pytest on the specified path # The '-s' flag shows print statements from the tests. pytest -s "$TEST_PATH" # Capture the exit code of the test script TEST_EXIT_CODE=$? # The 'trap' will automatically call the cleanup function now. # Exit with the same code as the test script (0 for success, non-zero for failure). exit $TEST_EXIT_CODE