#!/usr/bin/env python3 """ Test runner script for the backend-py project. Run this script to execute all unit tests. """ import sys import subprocess from pathlib import Path def run_tests(): """Run all tests using pytest.""" print("๐Ÿงช Running Python Backend Unit Tests\n") # Change to project directory project_root = Path(__file__).parent try: # Run pytest with coverage if available cmd = [ sys.executable, "-m", "pytest", "test-unit/", "-v", "--tb=short", "--color=yes" ] # Try to add coverage if pytest-cov is available try: import pytest_cov cmd.extend([ "--cov=src", "--cov-report=term-missing", "--cov-report=html:htmlcov" ]) print("๐Ÿ“Š Running tests with coverage analysis") except ImportError: print("๐Ÿ“ Running tests without coverage (install pytest-cov for coverage)") print(f"๐Ÿƒ Command: {' '.join(cmd)}\n") result = subprocess.run(cmd, cwd=project_root) if result.returncode == 0: print("\nโœ… All tests passed!") if 'pytest_cov' in locals(): print("๐Ÿ“Š Coverage report generated in htmlcov/index.html") else: print(f"\nโŒ Tests failed with exit code {result.returncode}") return result.returncode except FileNotFoundError: print("โŒ pytest not found. Install it with: pip install pytest pytest-asyncio") return 1 except Exception as e: print(f"โŒ Error running tests: {e}") return 1 def run_specific_test(test_file): """Run a specific test file.""" print(f"๐ŸŽฏ Running specific test: {test_file}\n") try: cmd = [sys.executable, "-m", "pytest", f"test-unit/{test_file}", "-v"] result = subprocess.run(cmd, cwd=Path(__file__).parent) return result.returncode except Exception as e: print(f"โŒ Error running test {test_file}: {e}") return 1 def main(): """Main entry point.""" if len(sys.argv) > 1: # Run specific test file test_file = sys.argv[1] if not test_file.startswith("test_"): test_file = f"test_{test_file}" if not test_file.endswith(".py"): test_file = f"{test_file}.py" return run_specific_test(test_file) else: # Run all tests return run_tests() if __name__ == "__main__": exit_code = main() sys.exit(exit_code)