run_coverage.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Coverage test runner for MCP Gateway.
  5. This script runs all tests and generates coverage reports in multiple formats.
  6. """
  7. import os
  8. import subprocess
  9. import sys
  10. # Fix Windows console encoding
  11. if sys.platform == 'win32':
  12. import io
  13. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
  14. sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
  15. def build_coverage_commands():
  16. """Build coverage commands using the current Python interpreter."""
  17. return [
  18. (
  19. [sys.executable, '-m', 'coverage', 'run', '-m', 'unittest',
  20. 'discover', '-s', 'tests', '-p', 'test_*.py'],
  21. 'Running tests with coverage',
  22. ),
  23. (
  24. [sys.executable, '-m', 'coverage', 'report'],
  25. 'Generating console coverage report',
  26. ),
  27. (
  28. [sys.executable, '-m', 'coverage', 'html'],
  29. 'Generating HTML coverage report',
  30. ),
  31. (
  32. [sys.executable, '-m', 'coverage', 'xml'],
  33. 'Generating XML coverage report (for CI/CD)',
  34. ),
  35. ]
  36. def run_command(cmd, description):
  37. """Run a command and print status."""
  38. print(f"\n{'='*60}")
  39. print(f"[*] {description}")
  40. print(f"{'='*60}\n")
  41. result = subprocess.run(cmd, shell=False)
  42. if result.returncode != 0:
  43. print(f"\n[X] {description} failed!")
  44. return False
  45. print(f"\n[OK] {description} completed successfully!")
  46. return True
  47. def main():
  48. """Main test runner."""
  49. print("\n=== MCP Gateway Coverage Test Runner ===")
  50. print("=" * 60)
  51. # Change to script directory
  52. script_dir = os.path.dirname(os.path.abspath(__file__))
  53. os.chdir(script_dir)
  54. # Step 1: Clean old coverage data
  55. if os.path.exists('.coverage'):
  56. os.remove('.coverage')
  57. print("[*] Cleaned old coverage data")
  58. for cmd, description in build_coverage_commands():
  59. if not run_command(cmd, description):
  60. return 1
  61. # Summary
  62. print("\n" + "=" * 60)
  63. print("=== Coverage Reports Generated ===")
  64. print("=" * 60)
  65. print(" [*] Console: (shown above)")
  66. print(f" [*] HTML: {os.path.join(script_dir, 'htmlcov', 'index.html')}")
  67. print(f" [*] XML: {os.path.join(script_dir, 'coverage.xml')}")
  68. print("=" * 60)
  69. # Open HTML report (optional)
  70. if '--open' in sys.argv or '-o' in sys.argv:
  71. html_path = os.path.join(script_dir, 'htmlcov', 'index.html')
  72. print("\n[*] Opening HTML report in browser...")
  73. if sys.platform == 'win32':
  74. os.startfile(html_path)
  75. elif sys.platform == 'darwin':
  76. subprocess.run(['open', html_path])
  77. else:
  78. subprocess.run(['xdg-open', html_path])
  79. print("\n[OK] All coverage reports generated successfully!")
  80. print("[TIP] Run 'python run_coverage.py --open' to open HTML report automatically\n")
  81. return 0
  82. if __name__ == '__main__':
  83. sys.exit(main())