Skip to content

Latest commit

 

History

History
177 lines (130 loc) · 3.75 KB

File metadata and controls

177 lines (130 loc) · 3.75 KB

Testing

This guide covers how to test the QPanda3 Runtime MCP Server.

Running Tests

All Tests

# Using uv
uv run pytest

# With coverage
uv run pytest --cov=src --cov-report=html

Specific Tests

# Single test file
uv run pytest tests/test_server.py

# Single test function
uv run pytest tests/test_server.py::test_setup_account

# With verbose output
uv run pytest -v tests/

Test Structure

tests/
├── __init__.py
├── conftest.py           # Shared fixtures and markers
├── test_server.py        # Server unit tests
├── test_runtime.py       # Runtime logic tests
└── e2e/                  # End-to-end tests
    ├── __init__.py
    ├── conftest.py       # E2E fixtures
    ├── test_mcp_protocol.py         # MCP protocol tests
    ├── test_quantum_workflow.py     # Quantum computing workflows
    ├── test_batch_operations.py     # Batch operations
    ├── test_circuit_observable_binding.py  # Multi-objective decisions
    ├── test_mcp_resources.py        # MCP resources
    └── test_error_handling.py       # Error handling

Writing Tests

Basic Test Example

import pytest
from qpanda3_runtime_mcp_server.runtime import setup_origin_quantum_account

@pytest.mark.asyncio
async def test_setup_account_with_env():
    """Test account setup with environment variables."""
    result = await setup_origin_quantum_account(
        api_key="test_key",
        server_url="https://test.server"
    )
    assert result["status"] in ["success", "error"]

Using Fixtures

# In conftest.py
import pytest

@pytest.fixture
def mock_service():
    """Mock RuntimeService for testing."""
    # Return a mock service object
    pass

# In test file
def test_list_devices(mock_service):
    """Test listing devices with mock service."""
    pass

Async Tests

All async tests should use @pytest.mark.asyncio:

@pytest.mark.asyncio
async def test_async_function():
    result = await some_async_function()
    assert result is not None

Test Categories

Unit Tests

Test individual functions in isolation:

def test_format_device_info():
    """Test device info formatting."""
    from qpanda3_runtime_mcp_server.utils import format_device_info
    
    device = {"id": "20", "name": "QPU-20"}
    result = format_device_info(device)
    assert "QPU-20" in result

Integration Tests

Test interactions with QPanda3 Runtime:

@pytest.mark.integration
async def test_real_device_listing():
    """Test listing real devices (requires credentials)."""
    result = await list_qpu_devices()
    assert result["status"] == "success"

E2E Tests

End-to-end tests verify complete workflows:

# Run E2E tests
uv run pytest tests/e2e/ -v

# Run specific E2E test file
uv run pytest tests/e2e/test_quantum_workflow.py -v

# Run with marker
uv run pytest -m e2e

See E2E Testing Guide for detailed patterns and best practices.

Mock Testing

When testing without real credentials:

from unittest.mock import patch, MagicMock

@pytest.mark.asyncio
async def test_with_mock():
    """Test with mocked RuntimeService."""
    with patch("qpanda3_runtime_mcp_server.runtime._service") as mock:
        mock.list_devices.return_value = [
            {"id": "20", "name": "Test QPU"}
        ]
        
        result = await list_qpu_devices()
        assert result["total_devices"] == 1

Coverage

Generate Coverage Report

# Terminal report
uv run pytest --cov=src

# HTML report
uv run pytest --cov=src --cov-report=html
open htmlcov/index.html

Coverage Goals

  • Aim for 80%+ code coverage
  • Focus on critical paths
  • Don't sacrifice test quality for coverage numbers