2025-03-03 17:50:11 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
Script to run the test suite for ARCH application.
|
|
|
|
Executes all tests and generates JSON and HTML reports.
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import shutil
|
|
|
|
from datetime import datetime
|
|
|
|
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
def run_tests(args):
|
|
|
|
"""Run pytest with specified arguments and generate reports."""
|
|
|
|
# Create test reports directory if needed
|
2025-03-04 06:38:19 -03:00
|
|
|
os.makedirs("test_reports", exist_ok=True)
|
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Generate timestamp for report files
|
2025-03-04 06:38:19 -03:00
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Base pytest arguments
|
|
|
|
pytest_args = [
|
2025-03-04 06:38:19 -03:00
|
|
|
"-v", # Verbose output
|
|
|
|
"--no-header", # No header in the output
|
|
|
|
"--tb=short", # Short traceback
|
|
|
|
f"--junitxml=test_reports/junit_{timestamp}.xml", # JUnit XML report
|
|
|
|
"--cov=app", # Coverage for app module
|
|
|
|
"--cov=routes", # Coverage for routes
|
|
|
|
"--cov=services", # Coverage for services
|
|
|
|
"--cov=utils", # Coverage for utils
|
|
|
|
"--cov-report=html:test_reports/coverage", # HTML coverage report
|
|
|
|
"-p",
|
|
|
|
"tests.json_reporter", # Load the JSON reporter plugin explicitly
|
2025-03-03 17:50:11 -03:00
|
|
|
]
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Add test files/directories
|
|
|
|
if args.tests:
|
|
|
|
pytest_args.extend(args.tests)
|
|
|
|
else:
|
2025-03-04 06:38:19 -03:00
|
|
|
pytest_args.append("tests/")
|
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Execute tests
|
|
|
|
print(f"\n{'='*80}")
|
|
|
|
print(f"Running tests at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
print(f"{'='*80}")
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
result = pytest.main(pytest_args)
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Generate JSON report
|
|
|
|
print(f"\n{'='*80}")
|
|
|
|
print(f"Test report available at: test_reports/test_results_{timestamp}.json")
|
|
|
|
print(f"Coverage report available at: test_reports/coverage/index.html")
|
|
|
|
print(f"{'='*80}\n")
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
return result
|
|
|
|
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Run tests for ARCH application")
|
2025-03-04 06:38:19 -03:00
|
|
|
parser.add_argument(
|
|
|
|
"tests", nargs="*", help="Specific test files or directories to run"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--clean",
|
|
|
|
action="store_true",
|
|
|
|
help="Clean test storage and results before running",
|
|
|
|
)
|
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
args = parser.parse_args()
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
if args.clean:
|
|
|
|
# Clean temporary test storage
|
2025-03-04 06:38:19 -03:00
|
|
|
if os.path.exists("test_storage"):
|
|
|
|
shutil.rmtree("test_storage")
|
2025-03-03 17:50:11 -03:00
|
|
|
# Clean previous test reports
|
2025-03-04 06:38:19 -03:00
|
|
|
if os.path.exists("test_reports"):
|
|
|
|
shutil.rmtree("test_reports")
|
2025-03-03 17:50:11 -03:00
|
|
|
print("Cleaned test storage and reports.")
|
2025-03-04 06:38:19 -03:00
|
|
|
|
2025-03-03 17:50:11 -03:00
|
|
|
# Run tests and exit with the pytest result code
|
|
|
|
sys.exit(run_tests(args))
|