MCP Operator

by willer
Verified
#!/usr/bin/env python """ Test runner for MCP Browser Operator Runs both unit tests and integration tests """ import os import sys import unittest import argparse from pathlib import Path # Ensure the src directory is in the path src_dir = Path(__file__).parent / "src" sys.path.insert(0, str(src_dir)) def main(): """Run tests for MCP Browser Operator""" parser = argparse.ArgumentParser(description="Run tests for MCP Browser Operator") parser.add_argument( "--unit-only", action="store_true", help="Run only unit tests (no integration tests)" ) parser.add_argument( "--integration-only", action="store_true", help="Run only integration tests (no unit tests)" ) parser.add_argument( "--verbose", "-v", action="store_true", help="Enable verbose output" ) parser.add_argument( "--test", "-t", type=str, help="Specific test to run (e.g. 'TestBrowserOperatorMethods')" ) args = parser.parse_args() # Set up test discovery test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests") runner = unittest.TextTestRunner(verbosity=2 if args.verbose else 1) loader = unittest.TestLoader() # If a specific test was requested if args.test: print(f"Running specific test: {args.test}") test_suite = None # First try it as a class name for pattern in ["test_mcp_methods.py", "test_mcp_integration.py", "test_real_multistep.py"]: try: module = loader.discover(test_dir, pattern=pattern) for suite in module: for test_class in suite: if test_class.__class__.__name__ == args.test: test_suite = test_class break except Exception: pass # If not found as a class, try as a pattern if not test_suite: test_suite = loader.discover(test_dir, pattern=f"*{args.test}*.py") if not test_suite or not list(test_suite): print(f"Error: Test '{args.test}' not found") return 1 # Otherwise run by pattern elif args.unit_only: print("Running unit tests only...") test_suite = loader.discover(test_dir, pattern="test_mcp_methods.py") elif args.integration_only: print("Running integration tests only...") test_suite = loader.discover(test_dir, pattern="test_mcp_integration.py") else: print("Running all tests...") test_suite = loader.discover(test_dir, pattern="test_*.py") # Run tests print("-" * 70) result = runner.run(test_suite) print("-" * 70) # Print summary print(f"Tests run: {result.testsRun}") print(f"Errors: {len(result.errors)}") print(f"Failures: {len(result.failures)}") print(f"Skipped: {len(result.skipped)}") # Manual testing instructions if not args.unit_only and not args.integration_only and not args.test: print("\nFor manual testing with the MCP Inspector, run: ./mcp-test") # Return exit code based on test result return 0 if result.wasSuccessful() else 1 if __name__ == "__main__": sys.exit(main())