File size: 2,656 Bytes
38c016b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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)