-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·236 lines (190 loc) · 7.81 KB
/
Copy pathrun_tests.py
File metadata and controls
executable file
·236 lines (190 loc) · 7.81 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
import os
import sys
import subprocess
import multiprocessing
from pathlib import Path
from helpers.constants.framework_constants import TRACES_DIR, ALLURE_RESULTS_DIR
from helpers.file_system import create_reports_structure
from utils.prepration import run_options
from utils.logger import (
log_info, log_warning, log_success, log_failure,
log_info_emoji
)
from utils.reporting import combine_allure_reports, server_report
def distribute_features(feature_files, max_workers):
"""Distribute feature files across workers."""
worker_tasks = [[] for _ in range(max_workers)]
for i, feature_file in enumerate(feature_files):
worker_tasks[i % max_workers].append(feature_file)
return worker_tasks
def run_worker_features(feature_files, report_dir, tags=None):
try:
cmd = [
sys.executable, '-m', 'behave',
'-f', 'allure_behave.formatter:AllureFormatter',
'-o', report_dir,
'--no-capture',
'--no-capture-stderr'
]
if tags:
for tag in tags:
cmd.extend(['-t', tag])
cmd += [str(f) for f in feature_files]
# Stream output, filter empty lines
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
output_lines = []
for line in process.stdout:
if not line.strip():
continue
output_lines.append(line)
log_info(line.rstrip())
exit_code = process.wait(timeout=600)
# Parse failing scenarios from output
full_output = '\n'.join(output_lines)
return {
'worker_id': report_dir.split('_')[-1],
'exit_code': exit_code,
'stdout': full_output,
'stderr': '',
'error': None if exit_code == 0 else f"Worker {report_dir.split('_')[-1]} failed with exit code {exit_code}"
}
except subprocess.TimeoutExpired:
return {
'worker_id': report_dir.split('_')[-1],
'exit_code': 1,
'stdout': '',
'stderr': 'Test timed out after 10 minutes',
'error': 'Test timed out after 10 minutes'
}
except Exception as e:
return {
'worker_id': report_dir.split('_')[-1],
'exit_code': 1,
'stdout': '',
'stderr': str(e),
'error': str(e)
}
def filter_features_by_tags(feature_files, tags):
if not tags:
return feature_files
relevant_features = []
for feature_file in feature_files:
try:
with open(feature_file, 'r', encoding='utf-8') as f:
content = f.read()
# Check if any of the tags are present in the feature file
for tag in tags:
if tag in content:
relevant_features.append(feature_file)
break
except Exception as e:
log_warning(f"Could not read {feature_file}: {e}")
# If we can't read the file, include it to be safe
relevant_features.append(feature_file)
return relevant_features
def run_behave_parallel(feature_files, max_workers=None, tags=None):
# Filter feature files based on tags
if tags:
feature_files = filter_features_by_tags(feature_files, tags)
log_info_emoji("📁", f"Running {len(feature_files)} relevant feature files (filtered by tags: {tags})")
if max_workers is None:
max_workers = min(multiprocessing.cpu_count(), len(feature_files))
if len(feature_files) == 0:
log_warning("⚠️ No feature files found matching the specified tags.")
return True
# Adjust max_workers to actual number of feature files if fewer
actual_workers = min(max_workers, len(feature_files))
log_info_emoji("🚀", f"Running {len(feature_files)} feature files with {actual_workers} parallel workers")
log_info("=" * 50)
# Create individual report directories for each worker
report_dirs = []
for i in range(max_workers):
report_dir = f"reports/workers/worker_{i}"
os.makedirs(report_dir, exist_ok=True)
report_dirs.append(report_dir)
# Create a pool of workers
with multiprocessing.Pool(processes=max_workers) as pool:
worker_tasks = distribute_features(feature_files, max_workers)
# Run behave for each worker's feature files
results = pool.starmap(
run_worker_features,
[(tasks, report_dirs[i], tags) for i, tasks in enumerate(worker_tasks) if tasks]
)
# Check results
failed_tests = [result for result in results if result['exit_code'] != 0]
# Combine Allure reports
combine_allure_reports(report_dirs)
log_info("=" * 50)
if failed_tests:
return False
else:
return True
def run_behave_command(args):
"""Run behave with the specified arguments and capture failing scenarios."""
cmd = [sys.executable, '-m', 'behave']
# Add formatter for Allure reporting
cmd.extend(['-f', 'allure_behave.formatter:AllureFormatter', '-o', ALLURE_RESULTS_DIR])
# Add feature files if specified
if args.features:
cmd.extend(args.features)
# Add tags if specified
if args.tags:
for tag in args.tags:
cmd.extend(['-t', tag])
# Run the command with live output streaming
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
output_lines = []
for line in process.stdout:
if not line.strip():
continue
output_lines.append(line)
log_info(line.rstrip())
exit_code = process.wait(timeout=600)
# Create a result object similar to subprocess.run
result = type('Result', (), {'returncode': exit_code, 'stdout': '\n'.join(output_lines)})()
return result
def main():
args = run_options()
os.environ['HEADLESS'] = 'True' if args.headless else 'False'
log_info_emoji("🌐", f"Headless Mode: {str(os.environ['HEADLESS']).capitalize()}")
os.environ['BROWSER'] = args.browser
log_info_emoji("🌐", f"Browser: {str(args.browser).capitalize()}")
if args.tracing:
log_info_emoji("�� ", f"Tracing Enabled | Trace files will be saved to {TRACES_DIR}")
create_reports_structure()
features_dir = Path("features")
if not features_dir.exists():
log_failure("Error: 'features' directory not found!")
sys.exit(1)
# Check if feature files exist
if not args.features:
feature_files = list(features_dir.glob("*.feature"))
if not feature_files:
log_failure("Error: No feature files found in 'features' directory!")
sys.exit(1)
log_info_emoji("📁", f"Found {len(feature_files)} feature files")
else:
log_info_emoji("📁", f"Running specified feature files: {args.features}")
feature_files = [Path(f) for f in args.features]
# Run tests
if args.parallel:
log_info_emoji("🔄", "Running tests in parallel mode")
# Determine the number of workers for parallel execution
max_workers = args.workers if args.workers else min(multiprocessing.cpu_count(), len(feature_files))
log_info_emoji("👥", f"Using {max_workers} workers")
success = run_behave_parallel(feature_files, max_workers, tags=args.tags)
result = type('Result', (), {'returncode': 0 if success else 1})()
else:
log_info_emoji("🔄", "Running tests in sequential mode")
log_info("=" * 50)
result = run_behave_command(args)
# Handle test results
if result.returncode == 0:
log_success("All tests passed!")
else:
log_failure("Some tests failed!")
server_report(args)
if result.returncode != 0:
sys.exit(result.returncode)
if __name__ == "__main__":
main()