-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
354 lines (288 loc) · 9.5 KB
/
Copy pathrun_tests.py
File metadata and controls
354 lines (288 loc) · 9.5 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
#!/usr/bin/env python3
"""
Professional Test Runner Script
===============================
Command-line interface for running Playwright tests with various options.
"""
import argparse
import asyncio
import os
import sys
from pathlib import Path
from typing import List, Optional, Dict, Any
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
from core.runner import AdvancedTestRunner, TestSuiteConfig
from core.config import config
def parse_arguments() -> argparse.Namespace:
"""Parse command line arguments"""
parser = argparse.ArgumentParser(
description="Professional Playwright Test Runner",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python run_tests.py # Run all tests
python run_tests.py --tags smoke regression # Run specific tags
python run_tests.py --browser firefox # Use Firefox browser
python run_tests.py --parallel 4 # Run with 4 parallel workers
python run_tests.py --headless # Run in headless mode
python run_tests.py --report html # Generate HTML report
python run_tests.py --config custom_config.json # Use custom config
python run_tests.py --test tests/test_login.py # Run specific test file
"""
)
# Test selection
parser.add_argument(
'--test', '-t',
nargs='+',
help='Specific test files or directories to run'
)
parser.add_argument(
'--tags', '--marker', '-m',
nargs='+',
help='Test markers/tags to run (e.g., smoke regression e2e)'
)
# Browser options
parser.add_argument(
'--browser', '-b',
choices=['chromium', 'firefox', 'webkit'],
default=config.browser.name,
help='Browser to use for testing'
)
# Execution options
parser.add_argument(
'--parallel', '-n',
type=int,
default=config.parallel.max_workers,
help='Number of parallel workers'
)
parser.add_argument(
'--headless',
action='store_true',
default=config.headless,
help='Run browser in headless mode'
)
parser.add_argument(
'--headed',
action='store_true',
help='Run browser in headed mode (overrides headless)'
)
# Reporting options
parser.add_argument(
'--report', '-r',
choices=['html', 'json', 'junit', 'allure', 'all'],
default='all',
help='Report format to generate'
)
parser.add_argument(
'--report-dir',
default=config.REPORTS_DIR,
help='Directory to store reports'
)
# Configuration options
parser.add_argument(
'--config',
help='Path to custom configuration file'
)
parser.add_argument(
'--env',
choices=['development', 'staging', 'production', 'testing'],
default='testing',
help='Environment to run tests in'
)
# Debug options
parser.add_argument(
'--debug',
action='store_true',
help='Enable debug mode with verbose logging'
)
parser.add_argument(
'--verbose', '-v',
action='store_true',
help='Verbose output'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Show what would be run without executing tests'
)
# Performance options
parser.add_argument(
'--performance',
action='store_true',
help='Enable performance monitoring'
)
parser.add_argument(
'--accessibility',
action='store_true',
help='Enable accessibility testing'
)
parser.add_argument(
'--security',
action='store_true',
help='Enable security scanning'
)
# Output options
parser.add_argument(
'--output', '-o',
help='Output file for results'
)
parser.add_argument(
'--quiet', '-q',
action='store_true',
help='Quiet mode, minimal output'
)
return parser.parse_args()
def load_custom_config(config_path: str) -> Dict[str, Any]:
"""Load custom configuration from file"""
if not Path(config_path).exists():
print(f"Error: Configuration file '{config_path}' not found")
sys.exit(1)
try:
import json
with open(config_path, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading configuration: {e}")
sys.exit(1)
def build_pytest_command(args: argparse.Namespace) -> List[str]:
"""Build pytest command from arguments"""
cmd = ['python', '-m', 'pytest']
# Test selection
if args.test:
cmd.extend(args.test)
else:
cmd.append('tests/')
# Markers/tags
if args.tags:
cmd.extend(['-m', ' or '.join(args.tags)])
# Parallel execution
if args.parallel > 1:
cmd.extend(['-n', str(args.parallel)])
# Browser configuration (passed via environment)
os.environ['BROWSER_NAME'] = args.browser
os.environ['HEADLESS'] = 'false' if args.headed else str(args.headless).lower()
os.environ['ENVIRONMENT'] = args.environment
# Reporting
if args.report in ['html', 'all']:
cmd.extend(['--html', f'{args.report_dir}/html-report/index.html'])
if args.report in ['junit', 'all']:
cmd.extend(['--junitxml', f'{args.report_dir}/junit-xml/results.xml'])
if args.report in ['allure', 'all']:
cmd.extend(['--alluredir', f'{args.report_dir}/allure-results'])
# Debug options
if args.debug or args.verbose:
cmd.append('-v')
cmd.append('-s')
if args.debug:
os.environ['LOG_LEVEL'] = 'DEBUG'
if args.quiet:
cmd.append('-q')
if args.dry_run:
cmd.append('--collect-only')
# Performance and other features
if args.performance:
os.environ['PERFORMANCE_MONITORING'] = 'true'
if args.accessibility:
os.environ['ACCESSIBILITY_CHECKING'] = 'true'
if args.security:
os.environ['SECURITY_SCANNING'] = 'true'
return cmd
async def run_with_advanced_runner(args: argparse.Namespace) -> None:
"""Run tests using the advanced test runner"""
# Load custom config if provided
config_data = {}
if args.config:
config_data = load_custom_config(args.config)
# Override config with command line arguments
config_data.update({
'name': f'Playwright Test Suite - {args.env}',
'browser': args.browser,
'headless': args.headless if not args.headed else False,
'parallel_workers': args.parallel,
'environment': args.env,
'report_path': args.report_dir,
})
# Create test suite configuration
suite_config = TestSuiteConfig(**config_data)
# Create and run test runner
runner = AdvancedTestRunner(suite_config)
results = await runner.run_suite()
# Output results
if args.output:
import json
with open(args.output, 'w') as f:
json.dump({
'summary': {
'total': results.total_tests,
'passed': results.passed,
'failed': results.failed,
'skipped': results.skipped,
'errors': results.errors,
'duration': results.duration,
},
'results': [
{
'test_name': r.test_name,
'status': r.status,
'duration': r.duration,
'error_message': r.error_message,
} for r in results.results
]
}, f, indent=2)
# Print summary
if not args.quiet:
print(f"\n{'='*60}")
print("TEST EXECUTION SUMMARY")
print(f"{'='*60}")
print(f"Total Tests: {results.total_tests}")
print(f"Passed: {results.passed}")
print(f"Failed: {results.failed}")
print(f"Skipped: {results.skipped}")
print(f"Errors: {results.errors}")
print(".2f")
print(f"Pass Rate: {(results.passed / results.total_tests * 100):.1f}%" if results.total_tests > 0 else "0%")
print(f"{'='*60}")
# Exit with appropriate code
if results.failed > 0 or results.errors > 0:
sys.exit(1)
def run_with_pytest(args: argparse.Namespace) -> None:
"""Run tests using pytest directly"""
import subprocess
cmd = build_pytest_command(args)
if not args.quiet:
print(f"Running command: {' '.join(cmd)}")
print(f"{'='*60}")
try:
result = subprocess.run(cmd, cwd=Path(__file__).parent)
sys.exit(result.returncode)
except KeyboardInterrupt:
print("\nTest execution interrupted by user")
sys.exit(130)
except Exception as e:
print(f"Error running tests: {e}")
sys.exit(1)
def main() -> None:
"""Main entry point"""
args = parse_arguments()
# Set environment variables
os.environ['PYTHONPATH'] = str(Path(__file__).parent)
# Create reports directory
Path(args.report_dir).mkdir(parents=True, exist_ok=True)
# Choose runner based on arguments
use_advanced_runner = (
args.config or
args.report in ['json', 'all'] or
args.performance or
args.accessibility or
args.security or
args.output
)
if use_advanced_runner:
# Use advanced runner for complex scenarios
asyncio.run(run_with_advanced_runner(args))
else:
# Use pytest directly for simple scenarios
run_with_pytest(args)
if __name__ == '__main__':
main()