File size: 9,476 Bytes
81e3673 | 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 | #!/usr/bin/env python3
"""
Allure Result Aggregator
Converts pytest JSON results to Allure format and aggregates results
from all platforms (backend, web, mobile) into unified Allure report.
Usage:
python allure_aggregator.py convert-pytest --input report.json --output allure-results/backend --platform backend
python allure_aggregator.py aggregate-allure --backend allure-results/backend --web allure-results/web --output allure-results/
"""
import argparse
import json
import os
import shutil
from pathlib import Path
from typing import Dict, List, Any, Optional
def load_json(file_path: str) -> Dict[str, Any]:
"""Load JSON file with error handling."""
path = Path(file_path)
if not path.exists():
return {"error": f"File not found: {file_path}"}
try:
return json.loads(path.read_text())
except json.JSONDecodeError as e:
return {"error": f"Invalid JSON: {e}"}
def parse_cargo_json_line(line: str) -> Optional[Dict[str, Any]]:
"""Parse a single line of cargo test JSON output.
Args:
line: Single line from cargo test --format json output
Returns:
Parsed test dict if line is a test result, None otherwise
"""
try:
data = json.loads(line.strip())
if data.get("type") == "test":
return {
"name": data.get("name", "unknown"),
"passed": data.get("passed", False),
}
except (json.JSONDecodeError, KeyError):
pass
return None
def extract_tauri_metrics(results: Dict[str, Any], platform: str) -> Dict[str, Any]:
"""Extract metrics from Tauri cargo test JSON format.
Args:
results: Test results dict (may have stats from pre-processing or raw cargo data)
platform: Platform name (desktop)
Returns:
Metrics dict matching Playwright pytest format
"""
# If results already have stats key (pre-processed by CI), use that
if "stats" in results:
return {
"platform": platform,
"total": results["stats"].get("total", 0),
"passed": results["stats"].get("passed", 0),
"failed": results["stats"].get("failed", 0),
"skipped": results["stats"].get("skipped", 0),
"duration": results["stats"].get("duration", 0),
}
# Parse raw cargo test results
if "testResults" in results:
test_results = results.get("testResults", [])
total = len(test_results)
passed = sum(1 for r in test_results if r.get("passed", False))
return {
"platform": platform,
"total": total,
"passed": passed,
"failed": total - passed,
"skipped": 0,
"duration": results.get("duration", 0),
}
# Unknown Tauri format
return {
"platform": platform,
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0,
"error": "Unknown Tauri format",
}
def extract_metrics(results: Dict[str, Any], platform: str) -> Dict[str, Any]:
"""Extract key metrics from platform-specific results.
Args:
results: Platform-specific test results dict
platform: Platform name (web, mobile, desktop)
Returns:
Metrics dict with total/passed/failed/skipped/duration fields
"""
# Check for errors first
if "error" in results:
return {
"platform": platform,
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0,
"error": results["error"],
}
# Playwright pytest format (used by both web E2E and mobile API tests)
if "stats" in results:
return {
"platform": platform,
"total": results["stats"].get("total", 0),
"passed": results["stats"].get("passed", 0),
"failed": results["stats"].get("failed", 0),
"skipped": results["stats"].get("skipped", 0),
"duration": results["stats"].get("duration", 0),
}
# Tauri cargo test format (desktop)
# Note: Mobile API tests use pytest (same as Playwright), no separate Detox parser needed
if "testResults" in results or "test_suites" in results:
return extract_tauri_metrics(results, platform)
# Unknown format
return {
"platform": platform,
"total": 0,
"passed": 0,
"failed": 0,
"skipped": 0,
"duration": 0,
"error": f"Unknown format for {platform}: missing stats or testResults keys",
}
def convert_pytest_to_allure(
pytest_json_path: str,
output_dir: str,
platform: str
) -> int:
"""
Convert pytest JSON results to Allure format.
Args:
pytest_json_path: Path to pytest JSON report
output_dir: Allure results output directory
platform: Platform name (backend, web, mobile)
Returns:
Number of test results converted
"""
results = load_json(pytest_json_path)
if "error" in results:
print(f"Error loading pytest JSON: {results['error']}")
return 0
os.makedirs(output_dir, exist_ok=True)
count = 0
# Parse pytest JSON format (from pytest-json-report)
tests = results.get("tests", [])
for test in tests:
test_name = test.get("name", "unknown")
outcome = test.get("outcome", "passed") # passed, failed, skipped
# Convert to Allure format
allure_result = {
"name": f"[{platform}] {test_name}",
"status": "passed" if outcome == "passed" else ("failed" if outcome == "failed" else "skipped"),
"statusDetails": {
"message": test.get("call", {}).get("longrepr", ""),
"trace": test.get("call", {}).get("traceback", "")
} if outcome == "failed" else {},
"steps": [],
"attachments": [],
"parameters": [
{"name": "platform", "value": platform},
{"name": "file", "value": test.get("file", "")}
],
"start": int(test.get("setup", {}).get("duration", 0) * 1000),
"stop": int(test.get("duration", 0) * 1000)
}
# Write to Allure results directory
test_id = f"{platform}_{test_name.replace('::', '_').replace('/', '_')}"
result_file = os.path.join(output_dir, f"{test_id}-result.json")
with open(result_file, 'w') as f:
json.dump(allure_result, f)
count += 1
print(f"Converted {count} {platform} tests to Allure format")
return count
def aggregate_allure_results(
backend_results: str,
web_results: str,
mobile_results: str,
output_dir: str
) -> int:
"""
Aggregate Allure results from all platforms.
Args:
backend_results: Path to backend Allure results
web_results: Path to web Allure results
mobile_results: Path to mobile Allure results
output_dir: Unified Allure results directory
Returns:
Total number of results aggregated
"""
os.makedirs(output_dir, exist_ok=True)
total = 0
for platform, results_dir in [
("backend", backend_results),
("web", web_results),
("mobile", mobile_results)
]:
if os.path.exists(results_dir):
for file in os.listdir(results_dir):
if file.endswith("-result.json"):
src = os.path.join(results_dir, file)
dst = os.path.join(output_dir, f"{platform}_{file}")
shutil.copy2(src, dst)
total += 1
print(f"Aggregated {total} Allure results from all platforms")
return total
def main():
parser = argparse.ArgumentParser(
description="Convert pytest to Allure and aggregate results"
)
subparsers = parser.add_subparsers(dest="command", help="Command to run")
# convert-pytest command
convert_parser = subparsers.add_parser("convert-pytest", help="Convert pytest JSON to Allure")
convert_parser.add_argument("--input", required=True, help="Pytest JSON report path")
convert_parser.add_argument("--output", required=True, help="Allure results output directory")
convert_parser.add_argument("--platform", required=True, choices=["backend", "web", "mobile"], help="Platform name")
# aggregate-allure command
aggregate_parser = subparsers.add_parser("aggregate-allure", help="Aggregate Allure results from all platforms")
aggregate_parser.add_argument("--backend", required=True, help="Backend Allure results directory")
aggregate_parser.add_argument("--web", required=True, help="Web Allure results directory")
aggregate_parser.add_argument("--mobile", required=True, help="Mobile Allure results directory")
aggregate_parser.add_argument("--output", required=True, help="Unified Allure results directory")
args = parser.parse_args()
if args.command == "convert-pytest":
count = convert_pytest_to_allure(args.input, args.output, args.platform)
print(f"✅ Converted {count} test results to Allure format")
elif args.command == "aggregate-allure":
total = aggregate_allure_results(args.backend, args.web, args.mobile, args.output)
print(f"✅ Aggregated {total} Allure results")
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
import sys
main()
|