Buckets:

download
raw
4.81 kB
"""
Pytest Configuration for Parallel Test Execution
This module configures pytest-xdist for optimal parallel test execution.
"""
import os
from typing import List
import pytest
def get_optimal_worker_count() -> int:
"""
Calculate optimal number of parallel workers based on system resources.
Returns:
int: Number of workers (default: auto-detect)
"""
import multiprocessing
# Get CPU count
cpu_count = multiprocessing.cpu_count()
# Use 75% of available CPUs by default
workers = max(1, int(cpu_count * 0.75))
# Allow override via environment variable
env_workers = os.environ.get("PYTEST_WORKERS")
if env_workers:
workers = int(env_workers)
return workers
def pytest_xdist_node_collection_finished(node, ids):
"""
Hook called when a worker node finishes collecting tests.
Used for debugging parallel execution.
"""
print(f"\n[Parallel] Node {node.gateway.id} collected {len(ids)} tests")
def pytest_xdist_make_scheduler(config, log):
"""
Custom scheduler for test distribution.
Uses 'loadgroup' to keep related tests together for efficiency.
"""
from xdist.scheduler import LoadGroupScheduling
# Use LoadGroupScheduling to keep test groups together
return LoadGroupScheduling(config, log)
def get_test_groups() -> List[str]:
"""
Define test groups for parallel execution.
Tests in the same group run on the same worker.
Returns:
List of test group names
"""
return [
"unit/agent1", # Core modules
"unit/agent2", # API routes
"unit/agent3", # Tools
"unit/agent4", # Database
"unit/agent5", # Guardrails
"integration", # Integration tests
"performance", # Performance tests
"security", # Security tests
"chaos", # Chaos tests
"monitoring", # Monitoring tests
]
# Pytest markers for parallel execution
def pytest_configure(config):
"""Configure pytest markers for parallel execution."""
config.addinivalue_line(
"markers",
"parallel_group(name): mark test to run in specific parallel group",
)
config.addinivalue_line("markers", "serial: mark test to run serially (not in parallel)")
config.addinivalue_line("markers", "isolated: mark test that requires isolated environment")
def pytest_collection_modifyitems(config, items):
"""
Modify test collection for optimal parallel distribution.
Groups tests by directory to minimize worker setup overhead.
"""
# Add parallel_group marker based on test path
for item in items:
# Skip if already marked
if item.get_closest_marker("parallel_group"):
continue
# Skip if marked as serial
if item.get_closest_marker("serial"):
continue
# Determine group from file path
path = str(item.fspath)
if "unit/agent1" in path:
item.add_marker(pytest.mark.parallel_group("agent1"))
elif "unit/agent2" in path:
item.add_marker(pytest.mark.parallel_group("agent2"))
elif "unit/agent3" in path:
item.add_marker(pytest.mark.parallel_group("agent3"))
elif "unit/agent4" in path:
item.add_marker(pytest.mark.parallel_group("agent4"))
elif "unit/agent5" in path:
item.add_marker(pytest.mark.parallel_group("agent5"))
elif "integration" in path:
item.add_marker(pytest.mark.parallel_group("integration"))
elif "performance" in path:
item.add_marker(pytest.mark.parallel_group("performance"))
elif "security" in path:
item.add_marker(pytest.mark.parallel_group("security"))
elif "chaos" in path:
item.add_marker(pytest.mark.parallel_group("chaos"))
elif "monitoring" in path:
item.add_marker(pytest.mark.parallel_group("monitoring"))
# Fixture for resource-intensive tests
@pytest.fixture(scope="session")
def parallel_worker_id():
"""
Get the current parallel worker ID.
Returns 'master' if not running in parallel mode.
"""
import os
return os.environ.get("PYTEST_XDIST_WORKER", "master")
@pytest.fixture(scope="session")
def parallel_worker_count():
"""Get the total number of parallel workers."""
import os
return int(os.environ.get("PYTEST_XDIST_WORKER_COUNT", "1"))
@pytest.fixture(scope="function")
def isolated_temp_dir(tmp_path, parallel_worker_id):
"""
Create an isolated temporary directory for each worker.
Prevents conflicts between parallel workers.
"""
worker_dir = tmp_path / parallel_worker_id
worker_dir.mkdir(exist_ok=True)
return worker_dir
# Import pytest for marker definitions
import pytest

Xet Storage Details

Size:
4.81 kB
·
Xet hash:
ad5ed0a8cf71a8bf0ed77a0f8e166411bbcf402796324f471488938451e18ecf

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.