Skip to content

Commit 7917b5a

Browse files
committed
integration test
1 parent 8acedf1 commit 7917b5a

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

.github/workflows/test-metatrader5-integration.yml

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test | Test MetaTrader5 Integration
1+
name: Test MetaTrader5 Integration
22

33
on:
44
push:
@@ -39,7 +39,7 @@ jobs:
3939
exit 1
4040
}
4141
42-
# Start MT5 in portable mode
42+
# Start MT5 in portable mode and give it time to initialize
4343
$mtProcess = Start-Process -FilePath $mtPath -ArgumentList "/portable" -PassThru
4444
$processId = $mtProcess.Id
4545
Write-Host "Started MetaTrader 5 terminal with process ID: $processId"
@@ -50,38 +50,7 @@ jobs:
5050
python -m pip install --upgrade pip
5151
pip install MetaTrader5
5252
53-
- name: Test MetaTrader5 connection
53+
- name: Test MetaTrader5 connection with improved script
5454
run: |
55-
# Create a resilient test script
56-
$script = @"
57-
import MetaTrader5 as mt5
58-
import time
59-
import sys
60-
61-
print(f"MetaTrader5 package version: {mt5.__version__}")
62-
63-
# Try to connect with retries
64-
max_attempts = 3
65-
for attempt in range(max_attempts):
66-
print(f"Connection attempt {attempt+1}/{max_attempts}...")
67-
68-
if mt5.initialize():
69-
print("MetaTrader5 initialized successfully")
70-
mt5.shutdown()
71-
sys.exit(0)
72-
else:
73-
error = mt5.last_error()
74-
print(f"initialize() failed, error code = {error}")
75-
if attempt < max_attempts - 1:
76-
time.sleep(5)
77-
78-
# If we're still running, all attempts failed
79-
# But we'll exit with success for CI purposes
80-
print("Warning: Could not connect to MetaTrader5, but continuing...")
81-
sys.exit(0)
82-
"@
83-
84-
# Save and run the script
85-
$script | Out-File -FilePath "test_mt5_connection.py" -Encoding utf8
86-
python test_mt5_connection.py
55+
python tests/integration/test_mt5_connection.py
8756

tests/integration/test_mt5_connection.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Integration tests for MetaTrader 5 connection functionality."""
22

33
import sys
4-
4+
import os
5+
import time
56
import MetaTrader5 as mt5
67

78

@@ -13,22 +14,44 @@ def test_mt5_connection() -> bool:
1314
"""
1415
print(f"MetaTrader5 package version: {mt5.__version__}")
1516

16-
# Initialize MetaTrader 5
17-
if not mt5.initialize():
18-
print(f"initialize() failed, error code = {mt5.last_error()}")
19-
sys.exit(1)
20-
21-
print(mt5.terminal_info())
22-
print(f"MetaTrader5 terminal copyright: {mt5.terminal_info().copyright}")
23-
print(f"MetaTrader5 terminal name: {mt5.terminal_info().name}")
24-
print(f"MetaTrader5 terminal path: {mt5.terminal_info().path}")
25-
26-
authorized = mt5.login()
27-
if authorized:
28-
print(f"Connected to account: {mt5.account_info().login}")
29-
print(f"Balance: {mt5.account_info().balance}")
30-
17+
# Define path to the MetaTrader terminal executable
18+
path = "C:\\Program Files\\MetaTrader 5\\terminal64.exe"
19+
print(f"MetaTrader5 path: {path}")
20+
21+
# Try to initialize with explicit path parameter
22+
if not mt5.initialize(path=path):
23+
error = mt5.last_error()
24+
print(f"initialize() with path failed, error code = {error}")
25+
26+
# Try the second method: initialize with just a delay
27+
time.sleep(3) # Give some time before retry
28+
print("Retrying initialization...")
29+
if not mt5.initialize():
30+
error = mt5.last_error()
31+
print(f"initialize() without path failed, error code = {error}")
32+
sys.exit(1)
33+
34+
# Connection successful
35+
print("MetaTrader5 initialized successfully")
36+
37+
# Get account info
38+
account_info = mt5.account_info()
39+
if account_info is not None:
40+
print(f"Account info: server={account_info.server}, balance={account_info.balance}")
41+
else:
42+
print("Failed to get account info - this is normal without login credentials")
43+
44+
# Get terminal info
45+
terminal_info = mt5.terminal_info()
46+
if terminal_info is not None:
47+
print(f"Terminal info: connected={terminal_info.connected}, path={terminal_info.path}")
48+
else:
49+
print("Failed to get terminal info")
50+
51+
# Shutdown the connection
52+
print("Shutting down MetaTrader5 connection...")
3153
mt5.shutdown()
54+
print("Done")
3255

3356
print("Test completed successfully")
3457
return True

0 commit comments

Comments
 (0)