vimalk78's picture
Add complete Python backend with AI-powered crossword generation
38c016b
raw
history blame
2.66 kB
#!/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)