212 lines
6.0 KiB
Python
212 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test runner script for CONTOSO API
|
|
|
|
This script provides a convenient way to run different types of tests
|
|
with proper environment setup and reporting.
|
|
|
|
Usage:
|
|
python scripts/run_tests.py [test_type]
|
|
|
|
Test types:
|
|
unit - Run unit tests only
|
|
integration - Run integration tests only
|
|
e2e - Run end-to-end tests only
|
|
all - Run all tests
|
|
coverage - Run tests with coverage report
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import subprocess
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
# Add the project root to Python path
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
def check_environment():
|
|
"""Check if the test environment is properly set up"""
|
|
print("🔍 Checking test environment...")
|
|
|
|
# Check if required packages are available
|
|
try:
|
|
import pytest
|
|
import fastapi
|
|
import pydantic
|
|
print("✅ Required test packages are available")
|
|
except ImportError as e:
|
|
print(f"❌ Missing required package: {e}")
|
|
return False
|
|
|
|
# Check if main application can be imported
|
|
try:
|
|
# Change to project root directory for import
|
|
original_cwd = os.getcwd()
|
|
os.chdir(project_root)
|
|
import main
|
|
os.chdir(original_cwd)
|
|
print("✅ Main application can be imported")
|
|
except ImportError as e:
|
|
print(f"❌ Cannot import main application: {e}")
|
|
os.chdir(original_cwd)
|
|
return False
|
|
|
|
# Check if test files exist
|
|
test_files = [
|
|
"tests/test_e2e.py",
|
|
"tests/conftest.py",
|
|
"pytest.ini"
|
|
]
|
|
|
|
for test_file in test_files:
|
|
if not (project_root / test_file).exists():
|
|
print(f"❌ Missing test file: {test_file}")
|
|
return False
|
|
|
|
print("✅ Test environment is ready")
|
|
return True
|
|
|
|
def run_command(cmd, description):
|
|
"""Run a command and handle the output"""
|
|
print(f"\n🚀 {description}")
|
|
print(f"Command: {' '.join(cmd)}")
|
|
print("-" * 50)
|
|
|
|
# Change to project root directory
|
|
original_cwd = os.getcwd()
|
|
os.chdir(project_root)
|
|
|
|
try:
|
|
# Use the same Python executable that's running this script
|
|
# This ensures we use the virtual environment
|
|
if cmd[0] == "python":
|
|
cmd[0] = sys.executable
|
|
|
|
# Inherit the current environment (including virtual environment)
|
|
result = subprocess.run(cmd, capture_output=False, text=True, env=os.environ.copy())
|
|
os.chdir(original_cwd)
|
|
return result.returncode == 0
|
|
except Exception as e:
|
|
print(f"❌ Error running command: {e}")
|
|
os.chdir(original_cwd)
|
|
return False
|
|
|
|
def run_unit_tests():
|
|
"""Run unit tests"""
|
|
cmd = [
|
|
"python", "-m", "pytest",
|
|
"tests/",
|
|
"-v",
|
|
"--tb=short",
|
|
"-x", # Stop on first failure
|
|
"--ignore=tests/test_e2e.py",
|
|
"--ignore=tests/integration/"
|
|
]
|
|
return run_command(cmd, "Running unit tests")
|
|
|
|
def run_integration_tests():
|
|
"""Run integration tests"""
|
|
cmd = [
|
|
"python", "-m", "pytest",
|
|
"tests/integration/",
|
|
"-v",
|
|
"--tb=short",
|
|
"-m", "integration"
|
|
]
|
|
return run_command(cmd, "Running integration tests")
|
|
|
|
def run_e2e_tests():
|
|
"""Run end-to-end tests"""
|
|
cmd = [
|
|
"python", "-m", "pytest",
|
|
"tests/test_e2e.py",
|
|
"-v",
|
|
"--tb=short",
|
|
"-m", "e2e"
|
|
]
|
|
return run_command(cmd, "Running end-to-end tests")
|
|
|
|
def run_all_tests():
|
|
"""Run all tests"""
|
|
cmd = [
|
|
"python", "-m", "pytest",
|
|
"tests/",
|
|
"-v",
|
|
"--tb=short"
|
|
]
|
|
return run_command(cmd, "Running all tests")
|
|
|
|
def run_coverage_tests():
|
|
"""Run tests with coverage report"""
|
|
# Install coverage if not available
|
|
try:
|
|
import coverage
|
|
except ImportError:
|
|
print("📦 Installing coverage package...")
|
|
subprocess.run([sys.executable, "-m", "pip", "install", "coverage", "pytest-cov"])
|
|
|
|
cmd = [
|
|
"python", "-m", "pytest",
|
|
"tests/",
|
|
"--cov=src",
|
|
"--cov-report=html",
|
|
"--cov-report=term-missing",
|
|
"--cov-fail-under=80"
|
|
]
|
|
return run_command(cmd, "Running tests with coverage")
|
|
|
|
def main():
|
|
"""Main function"""
|
|
parser = argparse.ArgumentParser(description="Run CONTOSO API tests")
|
|
parser.add_argument(
|
|
"test_type",
|
|
choices=["unit", "integration", "e2e", "all", "coverage"],
|
|
help="Type of tests to run"
|
|
)
|
|
parser.add_argument(
|
|
"--skip-env-check",
|
|
action="store_true",
|
|
help="Skip environment check"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
print("🧪 CONTOSO API Test Runner")
|
|
print("=" * 50)
|
|
|
|
# Check environment unless skipped
|
|
if not args.skip_env_check:
|
|
if not check_environment():
|
|
print("\n❌ Environment check failed")
|
|
print("💡 Make sure you're in the project root and virtual environment is activated")
|
|
sys.exit(1)
|
|
|
|
# Run the specified tests
|
|
success = False
|
|
|
|
if args.test_type == "unit":
|
|
success = run_unit_tests()
|
|
elif args.test_type == "integration":
|
|
success = run_integration_tests()
|
|
elif args.test_type == "e2e":
|
|
success = run_e2e_tests()
|
|
elif args.test_type == "all":
|
|
success = run_all_tests()
|
|
elif args.test_type == "coverage":
|
|
success = run_coverage_tests()
|
|
|
|
# Print results
|
|
print("\n" + "=" * 50)
|
|
if success:
|
|
print("✅ Tests completed successfully!")
|
|
if args.test_type == "e2e":
|
|
print("\n💡 If E2E tests were skipped, set E2E_TEST_API_KEY environment variable")
|
|
print(" See docs/TESTING.md for more information")
|
|
else:
|
|
print("❌ Tests failed!")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |