|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +pytest script for ArXplorer test suite. |
| 4 | +Usage: |
| 5 | + python scripts/run_tests.py # Run all tests |
| 6 | + python scripts/run_tests.py --fast # Run only fast unit tests |
| 7 | + python scripts/run_tests.py --coverage # Generate coverage report |
| 8 | + python scripts/run_tests.py --verbose # Verbose output |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +import subprocess |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +def run_tests( |
| 17 | + fast_only: bool = False, |
| 18 | + coverage: bool = True, |
| 19 | + verbose: bool = False, |
| 20 | + html_report: bool = True, |
| 21 | + fail_under: int = 70 |
| 22 | +): |
| 23 | + """ |
| 24 | + Run pytest with specified options. |
| 25 | + |
| 26 | + Args: |
| 27 | + fast_only: Skip slow integration tests |
| 28 | + coverage: Generate coverage report |
| 29 | + verbose: Verbose test output |
| 30 | + html_report: Generate HTML coverage report |
| 31 | + fail_under: Minimum coverage percentage required |
| 32 | + """ |
| 33 | + # Ensure we're in the project root |
| 34 | + project_root = Path(__file__).parent.parent |
| 35 | + tests_dir = project_root / "tests" |
| 36 | + |
| 37 | + if not tests_dir.exists(): |
| 38 | + print(f"Tests directory not found: {tests_dir}") |
| 39 | + return 1 |
| 40 | + |
| 41 | + # Base pytest command |
| 42 | + cmd = ["pytest", str(tests_dir)] |
| 43 | + |
| 44 | + # Add markers for fast-only mode |
| 45 | + if fast_only: |
| 46 | + cmd.extend(["-m", "not slow"]) |
| 47 | + |
| 48 | + # Coverage options |
| 49 | + if coverage: |
| 50 | + cmd.extend([ |
| 51 | + "--cov=src", |
| 52 | + "--cov-report=term-missing", |
| 53 | + "--cov-report=xml", |
| 54 | + ]) |
| 55 | + if html_report: |
| 56 | + cmd.append("--cov-report=html") |
| 57 | + |
| 58 | + # Verbosity |
| 59 | + if verbose: |
| 60 | + cmd.append("-vv") |
| 61 | + else: |
| 62 | + cmd.append("-v") |
| 63 | + |
| 64 | + # Show short traceback on failures |
| 65 | + cmd.append("--tb=short") |
| 66 | + |
| 67 | + # Color output |
| 68 | + cmd.append("--color=yes") |
| 69 | + |
| 70 | + print("=" * 60) |
| 71 | + print("Running ArXplorer Test Suite") |
| 72 | + print("=" * 60) |
| 73 | + print(f"Project root: {project_root}") |
| 74 | + print(f"Command: {' '.join(cmd)}") |
| 75 | + print() |
| 76 | + |
| 77 | + # Run pytest from project root |
| 78 | + result = subprocess.run(cmd, cwd=project_root) |
| 79 | + |
| 80 | + # Check coverage threshold if enabled |
| 81 | + if coverage and result.returncode == 0: |
| 82 | + print() |
| 83 | + print("=" * 60) |
| 84 | + print(f"Checking coverage threshold (>= {fail_under}%)") |
| 85 | + print("=" * 60) |
| 86 | + threshold_cmd = ["coverage", "report", f"--fail-under={fail_under}"] |
| 87 | + threshold_result = subprocess.run(threshold_cmd, cwd=project_root) |
| 88 | + |
| 89 | + if threshold_result.returncode != 0: |
| 90 | + print() |
| 91 | + print(f"overage below {fail_under}% threshold") |
| 92 | + return threshold_result.returncode |
| 93 | + else: |
| 94 | + print() |
| 95 | + print(f"Coverage meets {fail_under}% threshold") |
| 96 | + |
| 97 | + return result.returncode |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + """Parse arguments and run tests.""" |
| 102 | + args = sys.argv[1:] |
| 103 | + |
| 104 | + fast_only = "--fast" in args |
| 105 | + no_coverage = "--no-coverage" in args |
| 106 | + verbose = "--verbose" in args or "-v" in args |
| 107 | + no_html = "--no-html" in args |
| 108 | + |
| 109 | + # Check for help |
| 110 | + if "--help" in args or "-h" in args: |
| 111 | + print(__doc__) |
| 112 | + return 0 |
| 113 | + |
| 114 | + exit_code = run_tests( |
| 115 | + fast_only=fast_only, |
| 116 | + coverage=not no_coverage, |
| 117 | + verbose=verbose, |
| 118 | + html_report=not no_html, |
| 119 | + fail_under=70 |
| 120 | + ) |
| 121 | + |
| 122 | + if exit_code == 0: |
| 123 | + print() |
| 124 | + print("=" * 60) |
| 125 | + print("All tests passed!") |
| 126 | + print("=" * 60) |
| 127 | + if not no_coverage: |
| 128 | + print() |
| 129 | + print("Coverage reports generated:") |
| 130 | + print(" - Terminal: (displayed above)") |
| 131 | + print(" - XML: coverage.xml") |
| 132 | + if not no_html: |
| 133 | + print(" - HTML: htmlcov/index.html") |
| 134 | + else: |
| 135 | + print() |
| 136 | + print("=" * 60) |
| 137 | + print("Tests failed") |
| 138 | + print("=" * 60) |
| 139 | + |
| 140 | + return exit_code |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + sys.exit(main()) |
0 commit comments