File size: 14,175 Bytes
aef804e | 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | #!/usr/bin/env python3
"""
Test Report Generator for Phase 208 Integration & Performance Testing
Generates comprehensive test reports combining integration, contract, performance,
and quality test results. Supports JSON, HTML, and Markdown output formats.
Usage:
python generate_test_report.py --format markdown --output TEST_REPORT.md
python generate_test_report.py --include integration,performance
"""
import argparse
import json
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
class TestReportGenerator:
"""Generates comprehensive test reports from multiple test suites."""
def __init__(self, output: str = "test-report.json", format_type: str = "json",
include: Optional[List[str]] = None):
self.output = Path(output)
self.format_type = format_type
self.include = include or ["integration", "contract", "performance", "quality"]
self.report_data = {
"generated_at": datetime.utcnow().isoformat() + "Z",
"test_suites": {},
"summary": {}
}
def run_test_suite(self, suite_name: str, test_path: str,
extra_args: Optional[List[str]] = None) -> Dict[str, Any]:
"""Run a test suite and capture results.
Args:
suite_name: Name of the test suite
test_path: Path to tests
extra_args: Additional pytest arguments
Returns:
Dictionary with test results
"""
print(f"Running {suite_name} tests...")
cmd = ["pytest", "-v", "--tb=short", test_path]
if extra_args:
cmd.extend(extra_args)
result = subprocess.run(
cmd,
capture_output=True,
text=True,
cwd=Path(__file__).parent.parent.parent.parent
)
# Parse output for test counts
output = result.stdout + result.stderr
lines = output.split('\n')
passed = failed = skipped = 0
duration = 0.0
for line in lines:
if " passed" in line:
try:
parts = line.split()
for i, part in enumerate(parts):
if part == "passed":
passed = int(parts[i-1])
elif part == "failed":
failed = int(parts[i-1])
elif part == "skipped":
skipped = int(parts[i-1])
except (ValueError, IndexError):
pass
if "in" in line and "s" in line:
try:
duration_str = line.split("in ")[1].split("s")[0].strip()
duration = float(duration_str)
except (ValueError, IndexError):
pass
return {
"total": passed + failed + skipped,
"passed": passed,
"failed": failed,
"skipped": skipped,
"duration": duration,
"exit_code": result.returncode,
"output": output if result.returncode != 0 else ""
}
def generate_integration_report(self) -> Dict[str, Any]:
"""Generate integration test report."""
if "integration" not in self.include:
return {}
result = self.run_test_suite(
"Integration",
"tests/integration/workflows/"
)
return {
"type": "integration",
"tests": result,
"coverage": self._get_coverage_data()
}
def generate_contract_report(self) -> Dict[str, Any]:
"""Generate contract test report."""
if "contract" not in self.include:
return {}
result = self.run_test_suite(
"Contract",
"tests/integration/contracts/",
["-m", "contract"]
)
return {
"type": "contract",
"tests": result,
"schema_validations": result.get("passed", 0)
}
def generate_performance_report(self) -> Dict[str, Any]:
"""Generate performance test report."""
if "performance" not in self.include:
return {}
result = self.run_test_suite(
"Performance",
"tests/integration/performance/",
["--benchmark-only"]
)
return {
"type": "performance",
"tests": result,
"benchmarks": self._parse_benchmark_data(result.get("output", ""))
}
def generate_quality_report(self) -> Dict[str, Any]:
"""Generate quality test report."""
if "quality" not in self.include:
return {}
result = self.run_test_suite(
"Quality",
"tests/integration/quality/"
)
return {
"type": "quality",
"tests": result,
"flakiness_rate": self._calculate_flakiness_rate(result)
}
def _get_coverage_data(self) -> Dict[str, float]:
"""Extract coverage data if available."""
coverage_file = Path("tests/coverage_reports/metrics/coverage.json")
if not coverage_file.exists():
return {}
try:
with open(coverage_file) as f:
data = json.load(f)
return {
"line_coverage": data.get("totals", {}).get("percent_covered", 0),
"branch_coverage": data.get("totals", {}).get("percent_covered", 0)
}
except (json.JSONDecodeError, KeyError):
return {}
def _parse_benchmark_data(self, output: str) -> Dict[str, Any]:
"""Parse benchmark data from pytest-benchmark output."""
benchmarks = {}
lines = output.split('\n')
for line in lines:
if "P50" in line or "P95" in line or "P99" in line:
parts = line.split()
if len(parts) >= 4:
name = parts[0]
benchmarks[name] = {
"p50": float(parts[1]) if len(parts) > 1 else 0,
"p95": float(parts[2]) if len(parts) > 2 else 0,
"p99": float(parts[3]) if len(parts) > 3 else 0
}
return benchmarks
def _calculate_flakiness_rate(self, result: Dict[str, Any]) -> float:
"""Calculate flakiness rate from test results."""
total = result.get("total", 0)
failed = result.get("failed", 0)
if total == 0:
return 0.0
return round((failed / total) * 100, 2)
def generate_summary(self) -> Dict[str, Any]:
"""Generate overall summary from all test suites."""
total_tests = 0
total_passed = 0
total_failed = 0
total_duration = 0.0
for suite_data in self.report_data["test_suites"].values():
tests = suite_data.get("tests", {})
total_tests += tests.get("total", 0)
total_passed += tests.get("passed", 0)
total_failed += tests.get("failed", 0)
total_duration += tests.get("duration", 0)
pass_rate = round((total_passed / total_tests * 100) if total_tests > 0 else 0, 2)
return {
"total_tests": total_tests,
"total_passed": total_passed,
"total_failed": total_failed,
"pass_rate": pass_rate,
"total_duration": round(total_duration, 2)
}
def generate(self) -> None:
"""Generate the complete test report."""
# Generate individual suite reports
integration = self.generate_integration_report()
if integration:
self.report_data["test_suites"]["integration"] = integration
contract = self.generate_contract_report()
if contract:
self.report_data["test_suites"]["contract"] = contract
performance = self.generate_performance_report()
if performance:
self.report_data["test_suites"]["performance"] = performance
quality = self.generate_quality_report()
if quality:
self.report_data["test_suites"]["quality"] = quality
# Generate summary
self.report_data["summary"] = self.generate_summary()
# Write output
self._write_output()
def _write_output(self) -> None:
"""Write report in specified format."""
if self.format_type == "json":
self._write_json()
elif self.format_type == "html":
self._write_html()
elif self.format_type == "markdown":
self._write_markdown()
else:
print(f"Unknown format: {self.format_type}")
sys.exit(1)
def _write_json(self) -> None:
"""Write JSON report."""
with open(self.output, 'w') as f:
json.dump(self.report_data, f, indent=2)
print(f"✓ JSON report written to {self.output}")
def _write_html(self) -> None:
"""Write HTML report."""
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Test Report</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; }}
.summary {{ background: #f0f0f0; padding: 15px; border-radius: 5px; }}
.suite {{ margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }}
.pass {{ color: green; }}
.fail {{ color: red; }}
h1 {{ color: #333; }}
h2 {{ color: #666; border-bottom: 2px solid #ddd; padding-bottom: 10px; }}
</style>
</head>
<body>
<h1>Test Report</h1>
<p><strong>Generated:</strong> {self.report_data['generated_at']}</p>
<div class="summary">
<h2>Summary</h2>
<p><strong>Total Tests:</strong> {self.report_data['summary']['total_tests']}</p>
<p><strong>Passed:</strong> <span class="pass">{self.report_data['summary']['total_passed']}</span></p>
<p><strong>Failed:</strong> <span class="fail">{self.report_data['summary']['total_failed']}</span></p>
<p><strong>Pass Rate:</strong> {self.report_data['summary']['pass_rate']}%</p>
<p><strong>Duration:</strong> {self.report_data['summary']['total_duration']}s</p>
</div>
<div class="suites">
"""
for suite_name, suite_data in self.report_data['test_suites'].items():
tests = suite_data.get('tests', {})
html += f"""
<div class="suite">
<h2>{suite_name.title()} Tests</h2>
<p><strong>Total:</strong> {tests.get('total', 0)}</p>
<p><strong>Passed:</strong> <span class="pass">{tests.get('passed', 0)}</span></p>
<p><strong>Failed:</strong> <span class="fail">{tests.get('failed', 0)}</span></p>
<p><strong>Duration:</strong> {tests.get('duration', 0)}s</p>
</div>
"""
html += """
</div>
</body>
</html>
"""
with open(self.output, 'w') as f:
f.write(html)
print(f"✓ HTML report written to {self.output}")
def _write_markdown(self) -> None:
"""Write Markdown report."""
md = f"""# Test Report
**Generated:** {self.report_data['generated_at']}
## Summary
| Metric | Value |
|--------|-------|
| Total Tests | {self.report_data['summary']['total_tests']} |
| Passed | {self.report_data['summary']['total_passed']} |
| Failed | {self.report_data['summary']['total_failed']} |
| Pass Rate | {self.report_data['summary']['pass_rate']}% |
| Duration | {self.report_data['summary']['total_duration']}s |
## Test Suites
"""
for suite_name, suite_data in self.report_data['test_suites'].items():
tests = suite_data.get('tests', {})
md += f"""### {suite_name.title()} Tests
| Metric | Value |
|--------|-------|
| Total | {tests.get('total', 0)} |
| Passed | {tests.get('passed', 0)} |
| Failed | {tests.get('failed', 0)} |
| Skipped | {tests.get('skipped', 0)} |
| Duration | {tests.get('duration', 0)}s |
"""
# Add suite-specific details
if suite_name == "integration" and "coverage" in suite_data:
coverage = suite_data["coverage"]
md += f"""**Coverage:**
- Line Coverage: {coverage.get('line_coverage', 0)}%
- Branch Coverage: {coverage.get('branch_coverage', 0)}%
"""
if suite_name == "performance" and "benchmarks" in suite_data:
md += "**Benchmarks:**\n\n"
for name, metrics in suite_data["benchmarks"].items():
md += f"- {name}: P50={metrics.get('p50', 0)}ms, P95={metrics.get('p95', 0)}ms, P99={metrics.get('p99', 0)}ms\n"
md += "\n"
if suite_name == "quality" and "flakiness_rate" in suite_data:
md += f"**Flakiness Rate:** {suite_data['flakiness_rate']}%\n\n"
md += """---
*Generated by Test Report Generator for Phase 208*
"""
with open(self.output, 'w') as f:
f.write(md)
print(f"✓ Markdown report written to {self.output}")
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Generate comprehensive test reports for Phase 208"
)
parser.add_argument(
"--output", "-o",
default="test-report.json",
help="Output file path (default: test-report.json)"
)
parser.add_argument(
"--format", "-f",
choices=["json", "html", "markdown"],
default="json",
help="Output format (default: json)"
)
parser.add_argument(
"--include", "-i",
default="integration,contract,performance,quality",
help="Comma-separated list of test types to include"
)
args = parser.parse_args()
include = [s.strip() for s in args.include.split(',')]
generator = TestReportGenerator(
output=args.output,
format_type=args.format,
include=include
)
try:
generator.generate()
sys.exit(0)
except Exception as e:
print(f"Error generating report: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
|