Spaces:
Sleeping
Sleeping
File size: 17,012 Bytes
72fc0ee | 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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | """
Test Reporting and Metrics
Track test execution history, performance trends, and coverage metrics.
"""
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import List, Dict, Optional, Any
from enum import Enum
import json
import os
import pandas as pd
import numpy as np
class TestStatus(Enum):
"""Test execution status."""
PASSED = "passed"
FAILED = "failed"
SKIPPED = "skipped"
ERROR = "error"
class TestType(Enum):
"""Type of test."""
UNIT = "unit"
INTEGRATION = "integration"
LOAD = "load"
CHAOS = "chaos"
VALIDATION = "validation"
@dataclass
class TestResult:
"""Single test execution result."""
test_name: str
test_type: TestType
status: TestStatus
duration_ms: float
timestamp: datetime = field(default_factory=datetime.now)
error_message: Optional[str] = None
metadata: Dict[str, Any] = field(default_factory=dict)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
'test_name': self.test_name,
'test_type': self.test_type.value,
'status': self.status.value,
'duration_ms': self.duration_ms,
'timestamp': self.timestamp.isoformat(),
'error_message': self.error_message,
'metadata': self.metadata,
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'TestResult':
"""Create from dictionary."""
return cls(
test_name=data['test_name'],
test_type=TestType(data['test_type']),
status=TestStatus(data['status']),
duration_ms=data['duration_ms'],
timestamp=datetime.fromisoformat(data['timestamp']),
error_message=data.get('error_message'),
metadata=data.get('metadata', {}),
)
@dataclass
class TestRun:
"""Complete test run with multiple results."""
run_id: str
run_type: TestType
start_time: datetime
end_time: datetime
results: List[TestResult]
environment: Dict[str, Any] = field(default_factory=dict)
@property
def duration_seconds(self) -> float:
"""Total run duration."""
return (self.end_time - self.start_time).total_seconds()
@property
def total_tests(self) -> int:
"""Total number of tests."""
return len(self.results)
@property
def passed_tests(self) -> int:
"""Number of passed tests."""
return sum(1 for r in self.results if r.status == TestStatus.PASSED)
@property
def failed_tests(self) -> int:
"""Number of failed tests."""
return sum(1 for r in self.results if r.status == TestStatus.FAILED)
@property
def pass_rate(self) -> float:
"""Pass rate percentage."""
return (self.passed_tests / self.total_tests * 100) if self.total_tests > 0 else 0.0
@property
def avg_duration_ms(self) -> float:
"""Average test duration."""
if not self.results:
return 0.0
return np.mean([r.duration_ms for r in self.results])
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
'run_id': self.run_id,
'run_type': self.run_type.value,
'start_time': self.start_time.isoformat(),
'end_time': self.end_time.isoformat(),
'results': [r.to_dict() for r in self.results],
'environment': self.environment,
}
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'TestRun':
"""Create from dictionary."""
return cls(
run_id=data['run_id'],
run_type=TestType(data['run_type']),
start_time=datetime.fromisoformat(data['start_time']),
end_time=datetime.fromisoformat(data['end_time']),
results=[TestResult.from_dict(r) for r in data['results']],
environment=data.get('environment', {}),
)
class TestReporter:
"""Test execution reporter and history tracker."""
def __init__(self, storage_path: str = "test_reports"):
"""
Initialize test reporter.
Args:
storage_path: Directory for storing test reports
"""
self.storage_path = storage_path
os.makedirs(storage_path, exist_ok=True)
def save_run(self, run: TestRun) -> None:
"""
Save test run to storage.
Args:
run: Test run to save
"""
filepath = os.path.join(self.storage_path, f"{run.run_id}.json")
with open(filepath, 'w') as f:
json.dump(run.to_dict(), f, indent=2)
def load_run(self, run_id: str) -> Optional[TestRun]:
"""
Load test run from storage.
Args:
run_id: Test run identifier
Returns:
Test run or None if not found
"""
filepath = os.path.join(self.storage_path, f"{run_id}.json")
if not os.path.exists(filepath):
return None
with open(filepath, 'r') as f:
data = json.load(f)
return TestRun.from_dict(data)
def list_runs(self,
test_type: Optional[TestType] = None,
since: Optional[datetime] = None,
limit: Optional[int] = None) -> List[TestRun]:
"""
List test runs.
Args:
test_type: Filter by test type
since: Filter runs after this datetime
limit: Maximum number of runs to return
Returns:
List of test runs
"""
runs = []
for filename in os.listdir(self.storage_path):
if not filename.endswith('.json'):
continue
filepath = os.path.join(self.storage_path, filename)
with open(filepath, 'r') as f:
data = json.load(f)
run = TestRun.from_dict(data)
# Apply filters
if test_type and run.run_type != test_type:
continue
if since and run.start_time < since:
continue
runs.append(run)
# Sort by start time (newest first)
runs.sort(key=lambda r: r.start_time, reverse=True)
# Apply limit
if limit:
runs = runs[:limit]
return runs
def get_summary(self, test_type: Optional[TestType] = None) -> pd.DataFrame:
"""
Get summary of test runs.
Args:
test_type: Filter by test type
Returns:
DataFrame with run summaries
"""
runs = self.list_runs(test_type=test_type)
if not runs:
return pd.DataFrame()
data = []
for run in runs:
data.append({
'Run ID': run.run_id,
'Type': run.run_type.value,
'Start Time': run.start_time,
'Duration (s)': run.duration_seconds,
'Total Tests': run.total_tests,
'Passed': run.passed_tests,
'Failed': run.failed_tests,
'Pass Rate (%)': run.pass_rate,
'Avg Duration (ms)': run.avg_duration_ms,
})
return pd.DataFrame(data)
def generate_report(self, run_id: str) -> str:
"""
Generate detailed test report.
Args:
run_id: Test run identifier
Returns:
Formatted report string
"""
run = self.load_run(run_id)
if not run:
return f"Test run {run_id} not found"
report = []
report.append("=" * 80)
report.append(f"TEST REPORT: {run.run_id}")
report.append("=" * 80)
report.append(f"Type: {run.run_type.value}")
report.append(f"Start Time: {run.start_time}")
report.append(f"End Time: {run.end_time}")
report.append(f"Duration: {run.duration_seconds:.2f}s")
report.append("")
report.append("SUMMARY")
report.append("-" * 80)
report.append(f"Total Tests: {run.total_tests}")
report.append(f"Passed: {run.passed_tests}")
report.append(f"Failed: {run.failed_tests}")
report.append(f"Pass Rate: {run.pass_rate:.1f}%")
report.append(f"Avg Duration: {run.avg_duration_ms:.1f}ms")
report.append("")
# Failed tests
failed = [r for r in run.results if r.status == TestStatus.FAILED]
if failed:
report.append("FAILED TESTS")
report.append("-" * 80)
for result in failed:
report.append(f"❌ {result.test_name}")
report.append(f" Duration: {result.duration_ms:.1f}ms")
if result.error_message:
report.append(f" Error: {result.error_message}")
report.append("")
# Environment info
if run.environment:
report.append("ENVIRONMENT")
report.append("-" * 80)
for key, value in run.environment.items():
report.append(f"{key}: {value}")
report.append("")
report.append("=" * 80)
return "\n".join(report)
class TrendAnalyzer:
"""Analyze test trends over time."""
def __init__(self, reporter: TestReporter):
"""
Initialize trend analyzer.
Args:
reporter: Test reporter instance
"""
self.reporter = reporter
def get_pass_rate_trend(self,
test_type: Optional[TestType] = None,
days: int = 30) -> pd.DataFrame:
"""
Get pass rate trend over time.
Args:
test_type: Filter by test type
days: Number of days to analyze
Returns:
DataFrame with daily pass rates
"""
since = datetime.now() - timedelta(days=days)
runs = self.reporter.list_runs(test_type=test_type, since=since)
if not runs:
return pd.DataFrame()
data = []
for run in runs:
data.append({
'date': run.start_time.date(),
'pass_rate': run.pass_rate,
'total_tests': run.total_tests,
})
df = pd.DataFrame(data)
# Group by date and average
return df.groupby('date').agg({
'pass_rate': 'mean',
'total_tests': 'sum',
}).reset_index()
def get_duration_trend(self,
test_type: Optional[TestType] = None,
days: int = 30) -> pd.DataFrame:
"""
Get test duration trend over time.
Args:
test_type: Filter by test type
days: Number of days to analyze
Returns:
DataFrame with daily average durations
"""
since = datetime.now() - timedelta(days=days)
runs = self.reporter.list_runs(test_type=test_type, since=since)
if not runs:
return pd.DataFrame()
data = []
for run in runs:
data.append({
'date': run.start_time.date(),
'avg_duration_ms': run.avg_duration_ms,
})
df = pd.DataFrame(data)
# Group by date and average
return df.groupby('date').agg({
'avg_duration_ms': 'mean',
}).reset_index()
def detect_regressions(self,
test_type: Optional[TestType] = None,
threshold: float = 0.1) -> List[Dict[str, Any]]:
"""
Detect performance regressions.
Args:
test_type: Filter by test type
threshold: Regression threshold (10% by default)
Returns:
List of detected regressions
"""
runs = self.reporter.list_runs(test_type=test_type, limit=10)
if len(runs) < 2:
return []
regressions = []
# Compare latest run with baseline (average of previous runs)
latest = runs[0]
baseline_runs = runs[1:]
# Calculate baseline metrics
baseline_duration = np.mean([r.avg_duration_ms for r in baseline_runs])
baseline_pass_rate = np.mean([r.pass_rate for r in baseline_runs])
# Check for duration regression
duration_change = (latest.avg_duration_ms - baseline_duration) / baseline_duration
if duration_change > threshold:
regressions.append({
'type': 'duration',
'metric': 'avg_duration_ms',
'baseline': baseline_duration,
'current': latest.avg_duration_ms,
'change_pct': duration_change * 100,
'run_id': latest.run_id,
})
# Check for pass rate regression
pass_rate_change = (baseline_pass_rate - latest.pass_rate) / 100
if pass_rate_change > threshold:
regressions.append({
'type': 'pass_rate',
'metric': 'pass_rate',
'baseline': baseline_pass_rate,
'current': latest.pass_rate,
'change_pct': -pass_rate_change * 100,
'run_id': latest.run_id,
})
return regressions
class CoverageTracker:
"""Track test coverage metrics."""
def __init__(self):
"""Initialize coverage tracker."""
self.modules_covered: set = set()
self.functions_covered: set = set()
self.total_modules: int = 0
self.total_functions: int = 0
def record_coverage(self,
module: str,
function: str) -> None:
"""
Record coverage for a module/function.
Args:
module: Module name
function: Function name
"""
self.modules_covered.add(module)
self.functions_covered.add(f"{module}.{function}")
def set_totals(self,
total_modules: int,
total_functions: int) -> None:
"""
Set total counts for coverage calculation.
Args:
total_modules: Total number of modules
total_functions: Total number of functions
"""
self.total_modules = total_modules
self.total_functions = total_functions
@property
def module_coverage(self) -> float:
"""Module coverage percentage."""
if self.total_modules == 0:
return 0.0
return len(self.modules_covered) / self.total_modules * 100
@property
def function_coverage(self) -> float:
"""Function coverage percentage."""
if self.total_functions == 0:
return 0.0
return len(self.functions_covered) / self.total_functions * 100
def get_summary(self) -> Dict[str, Any]:
"""
Get coverage summary.
Returns:
Coverage metrics
"""
return {
'modules_covered': len(self.modules_covered),
'total_modules': self.total_modules,
'module_coverage_pct': self.module_coverage,
'functions_covered': len(self.functions_covered),
'total_functions': self.total_functions,
'function_coverage_pct': self.function_coverage,
}
def generate_report(self) -> str:
"""
Generate coverage report.
Returns:
Formatted report string
"""
report = []
report.append("=" * 60)
report.append("TEST COVERAGE REPORT")
report.append("=" * 60)
report.append(f"Modules: {len(self.modules_covered)}/{self.total_modules} ({self.module_coverage:.1f}%)")
report.append(f"Functions: {len(self.functions_covered)}/{self.total_functions} ({self.function_coverage:.1f}%)")
report.append("")
if self.modules_covered:
report.append("COVERED MODULES")
report.append("-" * 60)
for module in sorted(self.modules_covered):
report.append(f"✓ {module}")
report.append("")
report.append("=" * 60)
return "\n".join(report)
|