Integration Testing
Relevant source files
The following files were used as context for generating this wiki page:
Integration testing in the mt5linux project ensures the seamless communication between the Python client (both synchronous and asynchronous) and the MetaTrader 5 (MT5) terminal running within a Wine-based Linux container. The scope includes validating gRPC communication, resilience mechanisms like circuit breakers and auto-reconnection, and the accurate retrieval of market data and execution of trade orders.
These tests rely on a live MT5 terminal environment, typically provisioned via Docker, to verify that the bridge behaves correctly under real-world conditions, including market closures and connection interruptions.
Sources: tests/integration/test_client.py:1-25, tests/integration/test_async_client.py:1-20, DOCKER_COMPOSE_USAGE.md:1-10
Testing Infrastructure and Environment
The integration testing suite utilizes Docker Compose to manage the MT5 container environment. A single source of truth, docker-compose.yaml, defines the configuration for gRPC ports, VNC access, and health check endpoints.
Docker Configuration
Containers are parametrized via environment variables to allow for isolated test scenarios, preventing port conflicts and ensuring clean states through volume management.
| Variable | Default Value | Description |
|---|---|---|
MT5_GRPC_PORT | 28812 | Host port for gRPC communication |
MT5_VNC_PORT | 23000 | Port for GUI access via VNC |
MT5_HEALTH_PORT | 28002 | Port for container health monitoring |
MT5_CONTAINER_NAME | mt5linux-test | Prefix for container and volume naming |
Sources: DOCKER_COMPOSE_USAGE.md:15-35
Fixture Management
The project uses pytest fixtures (e.g., mt5, async_mt5) that handle the lifecycle of the connection. These fixtures ensure the terminal is available, initialized, and logged in before tests execute. Resilience tests verify that these fixtures can survive connection loss and maintain isolation between sequential test runs.
Sources: tests/integration/test_fixture_resilience.py:10-50, tests/integration/test_client.py:28-32
Client Resilience and Reliability
A core focus of integration testing is the resilience layer, which manages gRPC channel stability. The MetaTrader5 sync client inherits its resilience features from the AsyncMetaTrader5 client.
Circuit Breaker Logic
Integration tests validate the circuit breaker transitions between CLOSED, OPEN, and HALF_OPEN states based on operation success or failure.
Connection Recovery
Tests simulate disconnects to verify auto-reconnect behavior. The client is expected to recover its session and re-initialize with provided credentials (Login, Password, Server) without manual intervention.
Sources: tests/integration/test_client.py:200-230, tests/integration/test_bridge_behavior.py:85-110
API Feature Coverage
The suite performs comprehensive validation of the MT5 API across various financial instruments and data types.
Market Data Retrieval
Tests cover the retrieval of OHLCV (Open, High, Low, Close, Volume) rates and tick data. Parametrized tests verify these operations across multiple timeframes (M1 to MN1) and symbol groups (Forex, Metals, Indices).
- copy_rates_from_pos: Validates retrieval by bar offset.
- copy_rates_range: Validates retrieval within specific datetime intervals.
- copy_ticks_from: Validates tick data streams including
COPY_TICKS_ALLandCOPY_TICKS_TRADEflags.
Sources: tests/integration/test_parametrized.py:125-250, tests/integration/test_client.py:135-165
Trading Operations
Integration tests perform "read-only" trade validations using order_check and order_calc_margin. These ensure the bridge correctly serializes trade requests to the terminal and returns structured results.
Property-Based and Edge Case Testing
The suite employs Hypothesis for property-based testing and specific tests for edge cases to ensure robustness against invalid inputs.
Property-Based Validation
Properties verified include:
- Count Property: The number of returned rates must be less than or equal to the requested count.
- Volume Property:
order_checkmust accept valid floats and integers for volume while rejecting negatives.
Sources: tests/integration/test_parametrized.py:345-385
Error Handling
Tests specifically target API boundaries and error states:
- Invalid Symbols: Verifying that
symbol_inforeturnsNonefor non-existent symbols instead of crashing. - Reversed Dates: Ensuring
copy_rates_rangehandlesdate_from > date_togracefully. - Large Queries: Testing the limits of data serialization when requesting thousands of bars or symbols.
Sources: tests/integration/test_edge_cases.py:25-70, tests/integration/test_bridge_behavior.py:75-80
Summary
Integration testing serves as the definitive gate for verifying that the mt5linux bridge correctly implements the MetaTrader 5 API on Linux. By combining containerized environment management, resilience validation, and exhaustive API coverage, the project maintains high reliability for automated trading operations. The use of both synchronous and asynchronous integration tests ensures parity across both client implementations.
Sources: tests/integration/test_client.py, tests/integration/test_async_client.py