#!/bin/bash # Run backend test suites # Usage: ./backend/tests/run_ppstructure_tests.sh [test_type] # test_type: unit, api, e2e, all (default: all) set -e SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$( cd "$SCRIPT_DIR/../.." && pwd )" cd "$PROJECT_ROOT" # Activate virtual environment if [ -f "$PROJECT_ROOT/venv/bin/activate" ]; then source "$PROJECT_ROOT/venv/bin/activate" echo "✓ Activated venv: $PROJECT_ROOT/venv" else echo "⚠ Warning: venv not found at $PROJECT_ROOT/venv" echo " Tests will use system Python environment" fi # Colors for output GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Default test type TEST_TYPE="${1:-all}" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}Tool_OCR Backend Test Runner${NC}" echo -e "${BLUE}========================================${NC}" echo "" # Derive API base URL for E2E checks (same env vars used by pytest e2e tests) DEFAULT_BACKEND_PORT="${BACKEND_PORT:-8000}" DEFAULT_API_BASE_URL="http://localhost:${DEFAULT_BACKEND_PORT}" E2E_API_BASE_URL="${TOOL_OCR_E2E_API_BASE_URL:-$DEFAULT_API_BASE_URL}" # Function to run tests run_tests() { local test_name=$1 local test_path=$2 local markers=$3 shift 3 local extra_args=("$@") echo -e "${GREEN}Running ${test_name}...${NC}" if [ -n "$markers" ]; then pytest "$test_path" -v -m "$markers" --tb=short "${extra_args[@]}" || { echo -e "${RED}✗ ${test_name} failed${NC}" return 1 } else pytest "$test_path" -v --tb=short "${extra_args[@]}" || { echo -e "${RED}✗ ${test_name} failed${NC}" return 1 } fi echo -e "${GREEN}✓ ${test_name} passed${NC}" echo "" } # Run tests based on type case "$TEST_TYPE" in unit) echo -e "${YELLOW}Running Unit Tests...${NC}" echo "" run_tests "Unit Tests" "backend/tests" "not integration" \ --ignore=backend/tests/api --ignore=backend/tests/e2e ;; api) echo -e "${YELLOW}Running API Integration Tests...${NC}" echo "" run_tests "API Tests" "backend/tests/api" "not integration" ;; e2e) echo -e "${YELLOW}Running E2E Tests...${NC}" echo "" echo -e "${YELLOW}⚠ Note: E2E tests require backend server running${NC}" echo -e "${YELLOW}⚠ Provide credentials via TOOL_OCR_E2E_USERNAME / TOOL_OCR_E2E_PASSWORD${NC}" echo "" run_tests "E2E Tests" "backend/tests/e2e" "" ;; performance) echo -e "${RED}Performance suite no longer exists.${NC}" echo "Use: $0 unit | $0 api | $0 e2e | $0 all" exit 1 ;; all) echo -e "${YELLOW}Running All Tests...${NC}" echo "" # Unit tests run_tests "Unit Tests" "backend/tests" "not integration" \ --ignore=backend/tests/api --ignore=backend/tests/e2e # API tests run_tests "API Tests" "backend/tests/api" "not integration" # E2E tests (optional, requires server) echo -e "${YELLOW}E2E Tests (requires server running)...${NC}" if curl -s "${E2E_API_BASE_URL%/}/health" > /dev/null 2>&1; then run_tests "E2E Tests" "backend/tests/e2e" "" else echo -e "${YELLOW}⚠ Skipping E2E tests - server not running${NC}" echo -e "${YELLOW} Expected health endpoint: ${E2E_API_BASE_URL%/}/health${NC}" echo "" fi ;; *) echo -e "${RED}Invalid test type: $TEST_TYPE${NC}" echo "Usage: $0 [unit|api|e2e|all]" exit 1 ;; esac echo -e "${BLUE}========================================${NC}" echo -e "${GREEN}✓ All requested tests completed${NC}" echo -e "${BLUE}========================================${NC}" exit 0