-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_tests.py
More file actions
152 lines (121 loc) · 4.31 KB
/
run_tests.py
File metadata and controls
152 lines (121 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
Test runner for the trading bot system.
This script runs all unit tests and provides a comprehensive test report.
"""
import unittest
import sys
import os
import time
from io import StringIO
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
SRC_PATH = os.path.join(PROJECT_ROOT, 'src')
if SRC_PATH not in sys.path:
sys.path.insert(0, SRC_PATH)
def discover_and_run_tests():
"""Discover and run all tests in the tests directory."""
# Add current directory to path
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
# Discover tests
loader = unittest.TestLoader()
start_dir = os.path.join(current_dir, 'tests')
# Change to the project directory to ensure proper imports
original_cwd = os.getcwd()
try:
os.chdir(current_dir)
suite = loader.discover('tests', pattern='test_*.py')
finally:
os.chdir(original_cwd)
# Create test runner with detailed output
stream = StringIO()
runner = unittest.TextTestRunner(
stream=stream,
verbosity=2,
failfast=False,
buffer=True
)
print("🧪 TRADING BOT UNIT TESTS")
print("=" * 50)
start_time = time.time()
result = runner.run(suite)
end_time = time.time()
# Print results
output = stream.getvalue()
print(output)
# Summary
print("\n" + "=" * 50)
print("TEST SUMMARY")
print("=" * 50)
print(f"Tests run: {result.testsRun}")
print(f"Failures: {len(result.failures)}")
print(f"Errors: {len(result.errors)}")
print(f"Skipped: {len(result.skipped) if hasattr(result, 'skipped') else 0}")
print(f"Success rate: {((result.testsRun - len(result.failures) - len(result.errors)) / result.testsRun * 100):.1f}%")
print(f"Duration: {end_time - start_time:.2f} seconds")
# Detailed failure/error report
if result.failures:
print(f"\n❌ FAILURES ({len(result.failures)}):")
for test, traceback in result.failures:
print(f" - {test}")
if result.errors:
print(f"\n💥 ERRORS ({len(result.errors)}):")
for test, traceback in result.errors:
print(f" - {test}")
# Overall result
if result.wasSuccessful():
print("\n✅ ALL TESTS PASSED!")
return True
else:
print("\n❌ SOME TESTS FAILED!")
return False
def run_specific_test_module(module_name):
"""Run tests from a specific module."""
try:
# Add current directory to path for imports
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)
# Import the specific test module
test_module = __import__(f'tests.{module_name}', fromlist=[module_name])
# Create test suite from the module
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(test_module)
# Run tests
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
return result.wasSuccessful()
except ImportError as e:
print(f"❌ Could not import test module '{module_name}': {e}")
return False
def main():
"""Main test runner."""
import argparse
parser = argparse.ArgumentParser(description='Trading Bot Test Runner')
parser.add_argument(
'--module',
help='Run tests from specific module (e.g., test_risk_management)',
choices=['test_multi_asset_tester', 'test_optimizer', 'test_risk_management']
)
parser.add_argument(
'--list',
action='store_true',
help='List all available test modules'
)
args = parser.parse_args()
if args.list:
print("Available test modules:")
print(" - test_risk_management # Core risk management tests (ESSENTIAL)")
print(" - test_multi_asset_tester # Multi-asset testing functionality")
print(" - test_optimizer # Strategy parameter optimization")
return
if args.module:
print(f"Running tests from module: {args.module}")
success = run_specific_test_module(args.module)
else:
print("Running all tests...")
success = discover_and_run_tests()
# Exit with appropriate code
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()