-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
152 lines (119 loc) · 4.24 KB
/
Copy pathrun_tests.py
File metadata and controls
152 lines (119 loc) · 4.24 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 R2BC experimental output unit tests
This script runs all unit tests for the policy checkpoint saving/loading
and gif generation functionality.
"""
import unittest
import sys
from pathlib import Path
import argparse
# Add the project root to the Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def discover_and_run_tests(test_dir="tests", pattern="test_*.py", verbosity=2):
"""
Discover and run all tests in the specified directory
Args:
test_dir: Directory containing test files
pattern: Pattern to match test files
verbosity: Verbosity level (0=quiet, 1=normal, 2=verbose)
Returns:
bool: True if all tests passed, False otherwise
"""
# Create test loader
loader = unittest.TestLoader()
# Discover tests
start_dir = Path(test_dir)
if not start_dir.exists():
print(f"Error: Test directory '{test_dir}' does not exist!")
return False
suite = loader.discover(str(start_dir), pattern=pattern)
# Create test runner
runner = unittest.TextTestRunner(verbosity=verbosity, buffer=True)
# Run tests
print(f"Running tests from {test_dir} with pattern {pattern}")
print("=" * 70)
result = runner.run(suite)
# Print summary
print("\n" + "=" * 70)
print("TEST SUMMARY")
print("=" * 70)
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}")
if result.failures:
print(f"\nFAILURES ({len(result.failures)}):")
for test, traceback in result.failures:
print(f" - {test}")
if result.errors:
print(f"\nERRORS ({len(result.errors)}):")
for test, traceback in result.errors:
print(f" - {test}")
success = len(result.failures) == 0 and len(result.errors) == 0
if success:
print("\n✅ ALL TESTS PASSED!")
else:
print(f"\n❌ {len(result.failures) + len(result.errors)} TEST(S) FAILED!")
return success
def run_specific_test_module(module_name, verbosity=2):
"""
Run tests from a specific module
Args:
module_name: Name of the test module (e.g., 'test_policy_checkpoints')
verbosity: Verbosity level
Returns:
bool: True if all tests passed, False otherwise
"""
try:
# Import the test module
test_module = __import__(f"tests.{module_name}", fromlist=[module_name])
# Create test suite from module
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(test_module)
# Run tests
runner = unittest.TextTestRunner(verbosity=verbosity, buffer=True)
result = runner.run(suite)
return len(result.failures) == 0 and len(result.errors) == 0
except ImportError as e:
print(f"Error: Could not import test module '{module_name}': {e}")
return False
def main():
"""Main entry point for test runner"""
parser = argparse.ArgumentParser(description="Run R2BC experimental output tests")
parser.add_argument(
"--module", "-m",
help="Run tests from specific module (e.g., test_policy_checkpoints)"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Increase verbosity"
)
parser.add_argument(
"--quiet", "-q",
action="store_true",
help="Reduce verbosity"
)
parser.add_argument(
"--pattern", "-p",
default="test_*.py",
help="Pattern to match test files (default: test_*.py)"
)
args = parser.parse_args()
# Determine verbosity level
verbosity = 2 # Default
if args.quiet:
verbosity = 0
elif args.verbose:
verbosity = 2
# Run tests
if args.module:
success = run_specific_test_module(args.module, verbosity)
else:
success = discover_and_run_tests(pattern=args.pattern, verbosity=verbosity)
# Exit with appropriate code
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()