Repository navigation
Expand file tree
/
Copy pathpytest
More file actions
executable file
·368 lines (308 loc) · 12.9 KB
/
Copy pathpytest
File metadata and controls
executable file
·368 lines (308 loc) · 12.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
# encoding: utf-8
"""
Bot PyTest Tool
A specialized pytest wrapper for testing bot microservices in the botlab-core framework.
This tool handles the complex setup required to test microservices within the bot environment,
including file merging, variable loading, and proper test isolation.
USAGE EXAMPLES:
./pytest # Run all tests in default bot
./pytest --core ../botlab-private/ # Run tests with custom core directory
./pytest --mut meta_analysis # Run tests for specific microservice
./pytest --mut meta_analysis --test-file test_missedbathroom.py # Run specific test file
./pytest --bundle com.ppc.Bot --verbose # Run tests with verbose output
./pytest --variables data.variable # Run tests with preloaded variables
"""
import importlib
import os
import shutil
import sys
from argparse import ArgumentParser, RawDescriptionHelpFormatter
import pytest
parser = ArgumentParser(
description="""
Bot PyTest Tool - A specialized pytest wrapper for testing bot microservices.
This tool creates a temporary test environment by merging bot files and microservices,
then runs pytest within that environment. It handles the complex setup required to
test microservices within the botlab-core framework.
KEY FEATURES:
• Automatic bot environment setup and teardown
• Microservice-specific test filtering
• Variable preloading for regression testing
• Safe test isolation in temporary directories
• Support for custom core repositories
""",
formatter_class=RawDescriptionHelpFormatter,
add_help=True,
epilog="""
EXAMPLES:
Run all tests in default bot:
./pytest
Run tests for specific microservice:
./pytest --mut meta_analysis
Run specific test file:
./pytest --mut meta_analysis --test-file test_missedbathroom.py
Run with custom core directory:
./pytest --core ../botlab-private/
Run with preloaded variables:
./pytest --variables data.variable --location_id 123
Run with verbose output:
./pytest --mut meta_analysis --verbose
""",
)
# ============================================================================
# ARGUMENT GROUPS
# ============================================================================
developer_group = parser.add_argument_group(
"Bot Configuration",
"Core bot setup and environment configuration"
)
developer_group.add_argument(
"--core",
dest="core_directory",
metavar="PATH",
help="Absolute path to the core bot repository. Required when using a "
"separate core repository structure (e.g., ../botlab-private/). "
"If not specified, uses current working directory.",
)
developer_group.add_argument(
"-b",
"--bundle",
dest="bundle_id",
metavar="BUNDLE",
help="Bot bundle ID to test within (e.g., com.ppc.Bot, com.ppc.Tests). "
"Default: com.ppc.Tests",
)
developer_group.add_argument(
"-d",
"--directory",
dest="directory",
default="",
metavar="DIR",
help="Specific directory within the bot to test (e.g., 'intelligence', 'tests'). "
"Default: empty (tests entire bot)",
)
# ============================================================================
# TEST FILTERING
# ============================================================================
test_group = parser.add_argument_group(
"Test Filtering",
"Options to filter which tests to run"
)
test_group.add_argument(
"-m",
"--mut",
dest="mut",
action="append",
metavar="MICROSERVICE",
help="Filter tests to specific microservice packages. Can be specified multiple times. "
"Example: --mut meta_analysis --mut occupancy_analysis",
)
test_group.add_argument(
"-t",
"--test-file",
dest="test_file",
metavar="FILE",
help="Run tests from a specific test file. When used with --mut, automatically searches "
"in the microservice's tests directory. Example: test_missedbathroom.py or "
"test_missedbathroom.py::test_function",
)
# ============================================================================
# TEST DATA & OUTPUT
# ============================================================================
data_group = parser.add_argument_group(
"Test Data & Output",
"Options for test data loading and output control"
)
data_group.add_argument(
"--variables",
dest="variables",
nargs="+",
metavar="FILE",
help="Preload *.variable files into test cases. Useful for regression testing. "
"Example: --variables data1.variable data2.variable",
)
data_group.add_argument(
"--location_id",
dest="location_id",
metavar="ID",
help="Location ID to preload into test cases. Used for location-specific testing.",
)
data_group.add_argument(
"-v",
"--verbose",
dest="verbose",
action="store_true",
default=False,
help="Enable verbose pytest output (equivalent to pytest -vv)",
)
# ============================================================================
# MAIN EXECUTION
# ============================================================================
def main():
"""Main execution function for the Bot PyTest Tool."""
# Parse command line arguments
args = parser.parse_args()
print("=" * 80)
print("BOT PYTEST TOOL")
print("=" * 80)
print(f"Arguments: {args}")
print()
# Determine core directory
core = os.getcwd()
if args.core_directory is not None:
core = args.core_directory
if not os.path.isdir(core):
raise ValueError(f"Core directory does not exist: {core}")
print(f"Using BOTLAB_CORE = {core}")
print()
# ============================================================================
# BOTENGINE SETUP
# ============================================================================
print("Setting up botengine...")
# Load the botengine module
try:
shutil.copy(os.path.join("./", "botengine"), "botengine.py")
except (FileNotFoundError, PermissionError) as e:
raise RuntimeError(f"Failed to copy botengine: {e}")
botengine = importlib.import_module("botengine")
# ============================================================================
# BOT GENERATION
# ============================================================================
print("Generating bot environment...")
# Determine bot name and base path
botname = "com.ppc.Tests"
if args.bundle_id is not None:
botname = args.bundle_id
base_path = os.path.join(os.getcwd(), "." + botname)
print(f"Using base_path = {base_path}")
# Merge bot files from core repository
botengine._merge_redirects(
os.path.join(core, botname), base_path, botname, core_directory=core
)
# ============================================================================
# MICROSERVICE SETUP
# ============================================================================
print("Setting up microservices...")
# Copy all available microservice packages to ensure compatibility
# This is necessary when loading ISM data environments from other bots,
# as we need to be able to unpickle objects and land on files that exist.
original_intelligence_path = os.path.join(os.getcwd(), "." + botname, "intelligence")
target_intelligence_path = os.path.join(base_path, "intelligence")
if os.path.exists(original_intelligence_path):
for original_microservice_dir in os.listdir(original_intelligence_path):
original_dir_path = os.path.join(original_intelligence_path, original_microservice_dir)
target_dir_path = os.path.join(target_intelligence_path, original_microservice_dir)
if os.path.isdir(original_dir_path):
if not os.path.exists(target_dir_path):
print(f" Copying microservice: {original_microservice_dir}")
shutil.copytree(original_dir_path, target_dir_path)
# ============================================================================
# PYTEST ARGUMENT BUILDING
# ============================================================================
print("Building pytest arguments...")
extras = []
# Add microservice filtering
if args.mut is not None:
for mut in args.mut:
print(f" Filtering by microservice: {mut}")
extras += ["-k", mut]
# Add specific test file
if args.test_file is not None:
print(f" Running specific test file: {args.test_file}")
# If we have microservice filtering, construct the proper path to the test file
if args.mut is not None and len(args.mut) > 0:
# Assume the test file is in the microservice's tests directory
microservice_name = args.mut[0] # Use the first microservice if multiple specified
test_file_path = os.path.join(base_path, "intelligence", microservice_name, "tests", args.test_file)
# Check if the file exists
if os.path.exists(test_file_path):
print(f" Found test file at: {test_file_path}")
extras += [test_file_path]
else:
# Try alternative locations
alternative_paths = [
os.path.join(base_path, "intelligence", microservice_name, args.test_file),
os.path.join(base_path, args.test_file),
args.test_file # Fallback to original path
]
found = False
for alt_path in alternative_paths:
if os.path.exists(alt_path):
print(f" Found test file at: {alt_path}")
extras += [alt_path]
found = True
break
if not found:
print(f" Warning: Could not find test file '{args.test_file}' in any expected location")
print(f" Searched in:")
for path in [test_file_path] + alternative_paths:
print(f" - {path}")
extras += [args.test_file] # Let pytest handle the error
else:
# No microservice filtering, use the file as-is
extras += [args.test_file]
# Add variable files
if args.variables is not None:
print(" Loading variable files:")
for variable_path in args.variables:
if not os.path.exists(variable_path):
raise FileNotFoundError(f"Variable file not found: {variable_path}")
print(f" {variable_path}")
extras += ["--variables", " ".join(args.variables)]
# Add location ID
if args.location_id is not None:
print(f" Setting location ID: {args.location_id}")
extras += ["--location_id", args.location_id]
# ============================================================================
# CLEANUP AND FINAL SETUP
# ============================================================================
print("Finalizing setup...")
# Clean up temporary botengine.py file
try:
os.remove("botengine.py")
except (FileNotFoundError, PermissionError) as e:
print(f"Warning: Could not remove botengine.py: {e}")
# Copy botengine pytest stubs to the test environment
botengine_destination = os.path.join(base_path, "botengine_pytest.py")
try:
shutil.copyfile("./botengine_pytest.py", botengine_destination)
except (FileNotFoundError, PermissionError) as e:
raise RuntimeError(f"Failed to copy botengine_pytest.py: {e}")
# Add the bot path to Python's module search path
sys.path.insert(0, base_path)
# Import localization module to ensure gettext is initialized
try:
import localization # noqa: F401, E402 # type: ignore
except ImportError:
print("Warning: Could not import localization module")
# ============================================================================
# RUN PYTEST
# ============================================================================
print("=" * 80)
print("RUNNING PYTEST")
print("=" * 80)
# Build the final pytest command
pytest_args = [
"-vv" if args.verbose else "-q",
os.path.join(base_path, args.directory),
"-s"
] + extras
print(f"Pytest command: pytest {' '.join(pytest_args)}")
print()
# Execute pytest
exit_code = pytest.main(pytest_args)
print("=" * 80)
print(f"PYTEST COMPLETED (exit code: {exit_code})")
print("=" * 80)
return exit_code
if __name__ == "__main__":
try:
exit_code = main()
sys.exit(exit_code)
except KeyboardInterrupt:
print("\nInterrupted by user")
sys.exit(1)
except Exception as e:
print(f"\nError: {e}")
sys.exit(1)