|
4 | 4 | Essential dev commands for testing, linting, and type checking |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import datetime |
| 8 | +import json |
7 | 9 | import os |
8 | 10 | import shlex |
9 | 11 | import subprocess |
@@ -216,5 +218,92 @@ def check(): |
216 | 218 | click.echo("\n✅ All checks passed!") |
217 | 219 |
|
218 | 220 |
|
| 221 | +def _extract_first_failure( |
| 222 | + tests: list[dict], |
| 223 | +) -> tuple[str, str, str] | tuple[None, None, None]: |
| 224 | + """ |
| 225 | + Return (nodeid, phase, message) for first failed test, |
| 226 | + checking setup → call → teardown. |
| 227 | + """ |
| 228 | + for t in tests: |
| 229 | + if t.get("outcome") in ("failed", "error"): |
| 230 | + nodeid = t.get("nodeid", "unknown test") |
| 231 | + |
| 232 | + for phase in ["setup", "call", "teardown"]: |
| 233 | + phase_data = t.get(phase) |
| 234 | + if phase_data and phase_data.get("outcome") == "failed": |
| 235 | + longrepr = phase_data.get("longrepr", "") |
| 236 | + if isinstance(longrepr, dict): |
| 237 | + message = longrepr.get("reprcrash", {}).get( |
| 238 | + "message", str(longrepr) |
| 239 | + ) |
| 240 | + else: |
| 241 | + message = str(longrepr) |
| 242 | + |
| 243 | + return nodeid, phase, message |
| 244 | + |
| 245 | + return nodeid, "unknown", "No error details available" |
| 246 | + |
| 247 | + return None, None, None |
| 248 | + |
| 249 | + |
| 250 | +@cli.command() |
| 251 | +@click.option( |
| 252 | + "--file", |
| 253 | + "report_file", |
| 254 | + default=".report.json", |
| 255 | + type=click.File("r"), |
| 256 | + show_default=True, |
| 257 | + help="Path to pytest JSON report file", |
| 258 | +) |
| 259 | +def parse_pytest_report(report_file): |
| 260 | + """Parse pytest JSON report and print summary for CI (GitHub Actions friendly).""" |
| 261 | + report = json.load(report_file) |
| 262 | + |
| 263 | + summary = report.get("summary", {}) |
| 264 | + duration = str(datetime.timedelta(report.get("duration", 0) / 1000)) |
| 265 | + exit_code = report.get("exitcode", 0) |
| 266 | + |
| 267 | + failed = summary.get("failed", 0) |
| 268 | + error = summary.get("error", 0) |
| 269 | + passed = summary.get("passed", 0) |
| 270 | + skipped = summary.get("skipped", 0) |
| 271 | + total = summary.get("total", 0) |
| 272 | + |
| 273 | + # Header |
| 274 | + status = ( |
| 275 | + "❌ Test Results - Failed" if failed or error else "✅ Test Results - Success" |
| 276 | + ) |
| 277 | + click.echo(f"**{status}**") |
| 278 | + click.echo() |
| 279 | + |
| 280 | + # Duration |
| 281 | + click.echo(f"**Duration:** {duration}") |
| 282 | + click.echo() |
| 283 | + |
| 284 | + # Summary |
| 285 | + click.echo("**Summary:**") |
| 286 | + click.echo(f"- Total: {total}") |
| 287 | + click.echo(f"- Passed: {passed}") |
| 288 | + click.echo(f"- Failed: {failed}") |
| 289 | + click.echo(f"- Error: {error}") |
| 290 | + click.echo(f"- Skipped: {skipped}") |
| 291 | + click.echo() |
| 292 | + |
| 293 | + # Failure details |
| 294 | + if failed or error: |
| 295 | + nodeid, phase, message = _extract_first_failure(report.get("tests", [])) |
| 296 | + |
| 297 | + click.echo("**Failure Details:**") |
| 298 | + click.echo(f"- Exit code: `{exit_code}`") |
| 299 | + |
| 300 | + if nodeid: |
| 301 | + click.echo(f"- First failed test: `{nodeid}`") |
| 302 | + if phase: |
| 303 | + click.echo(f"- Phase: `{phase}`") |
| 304 | + if message: |
| 305 | + click.echo(f"- Error:\n\n```\n{message}\n```") |
| 306 | + |
| 307 | + |
219 | 308 | if __name__ == "__main__": |
220 | 309 | cli() |
0 commit comments