|
|
|
""" |
|
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") |
|
|
|
|
|
project_root = Path(__file__).parent |
|
|
|
try: |
|
|
|
cmd = [ |
|
sys.executable, "-m", "pytest", |
|
"test-unit/", |
|
"-v", |
|
"--tb=short", |
|
"--color=yes" |
|
] |
|
|
|
|
|
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: |
|
|
|
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: |
|
|
|
return run_tests() |
|
|
|
if __name__ == "__main__": |
|
exit_code = main() |
|
sys.exit(exit_code) |