Files
ComfyUI/tests-api/conftest.py

143 lines
3.2 KiB
Python
Raw Permalink Normal View History

"""
Test fixtures for API testing
"""
import os
import pytest
import yaml
import requests
import logging
from typing import Dict, Any, Generator, Optional
from urllib.parse import urljoin
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Default server configuration
DEFAULT_SERVER_URL = "http://127.0.0.1:8188"
@pytest.fixture(scope="session")
def api_spec_path() -> str:
"""
Get the path to the OpenAPI specification file
2025-05-20 12:26:56 -07:00
Returns:
Path to the OpenAPI specification file
"""
return os.path.abspath(os.path.join(
2025-05-20 12:26:56 -07:00
os.path.dirname(__file__),
"..",
"openapi.yaml"
))
@pytest.fixture(scope="session")
def api_spec(api_spec_path: str) -> Dict[str, Any]:
"""
Load the OpenAPI specification
2025-05-20 12:26:56 -07:00
Args:
api_spec_path: Path to the spec file
2025-05-20 12:26:56 -07:00
Returns:
Parsed OpenAPI specification
"""
with open(api_spec_path, 'r') as f:
return yaml.safe_load(f)
@pytest.fixture(scope="session")
def base_url() -> str:
"""
Get the base URL for the API server
2025-05-20 12:26:56 -07:00
Returns:
Base URL string
"""
# Allow overriding via environment variable
return os.environ.get("COMFYUI_SERVER_URL", DEFAULT_SERVER_URL)
@pytest.fixture(scope="session")
def server_available(base_url: str) -> bool:
"""
Check if the server is available
2025-05-20 12:26:56 -07:00
Args:
base_url: Base URL for the API
2025-05-20 12:26:56 -07:00
Returns:
True if the server is available, False otherwise
"""
try:
response = requests.get(base_url, timeout=2)
return response.status_code == 200
except requests.RequestException:
logger.warning(f"Server at {base_url} is not available")
return False
@pytest.fixture
def api_client(base_url: str) -> Generator[Optional[requests.Session], None, None]:
"""
Create a requests session for API testing
2025-05-20 12:26:56 -07:00
Args:
base_url: Base URL for the API
2025-05-20 12:26:56 -07:00
Yields:
Requests session configured for the API
"""
session = requests.Session()
2025-05-20 12:26:56 -07:00
# Helper function to construct URLs
def get_url(path: str) -> str:
# Paths in the OpenAPI spec already include /api prefix where needed
return urljoin(base_url, path)
2025-05-20 12:26:56 -07:00
# Add url helper to the session
session.get_url = get_url # type: ignore
2025-05-20 12:26:56 -07:00
yield session
2025-05-20 12:26:56 -07:00
# Cleanup
session.close()
@pytest.fixture
def api_get_json(api_client: requests.Session):
"""
Helper fixture for making GET requests and parsing JSON responses
2025-05-20 12:26:56 -07:00
Args:
api_client: API client session
2025-05-20 12:26:56 -07:00
Returns:
Function that makes GET requests and returns JSON
"""
def _get_json(path: str, **kwargs):
url = api_client.get_url(path) # type: ignore
response = api_client.get(url, **kwargs)
2025-05-20 12:26:56 -07:00
if response.status_code == 200:
try:
return response.json()
except ValueError:
return None
return None
2025-05-20 12:26:56 -07:00
return _get_json
@pytest.fixture
def require_server(server_available):
"""
Skip tests if server is not available
2025-05-20 12:26:56 -07:00
Args:
server_available: Whether the server is available
"""
if not server_available:
2025-05-20 12:26:56 -07:00
pytest.skip("Server is not available")