diff --git a/backend/app.py b/backend/app.py index d65f7ce..68d61f5 100644 --- a/backend/app.py +++ b/backend/app.py @@ -135,17 +135,29 @@ def _run_task_process(task: dict): task["finished_at"] = datetime.now().isoformat() + # Parse test results for better report + test_results = _parse_pytest_results(logs_all) + total_pass = sum(1 for r in test_results if r.get("status") == "passed") + total_fail = sum(1 for r in test_results if r.get("status") == "failed") + total_skip = sum(1 for r in test_results if r.get("status") == "skipped") + total = total_pass + total_fail + total_skip + if total == 0: + total = 1 + total_pass = 1 if task["status"] == "pass" else 0 + total_fail = 0 if task["status"] == "pass" else 1 + report = { "task_id": tid, "task_name": task["name"], "product": task.get("product", "test-agent"), "module": task.get("module", ""), - "total_runs": 1, - "pass": 1 if task["status"] == "pass" else 0, - "fail": 0 if task["status"] == "pass" else 1, + "total_runs": total, + "pass": total_pass, + "fail": total_fail, "started_at": task["started_at"], "finished_at": task["finished_at"], "result": "PASS" if task["status"] == "pass" else "FAIL", + "results": test_results, } reports_db[tid] = report @@ -448,6 +460,12 @@ def _run_existing_tests(task, _log, logs_all): proc.wait() process_pids.pop(task["id"], None) + # Parse pytest results + test_results = _parse_pytest_results(logs_all) + if test_results: + results_path = REPORTS_DIR / f"{task['id']}_results.json" + results_path.write_text(json.dumps(test_results, ensure_ascii=False, indent=2)) + if proc.returncode == 0: _log("SUCCESS", "全部测试通过") else: @@ -602,7 +620,22 @@ def full_pipeline(repo_id): return jsonify(task) -def _run_ui_fix(task, _log, logs_all): +def _parse_pytest_results(logs: list) -> list[dict]: + import re + results = [] + for line in logs: + line_str = line if isinstance(line, str) else line.get("msg", "") + # Parse PASSED/FAILED lines like: tests/data_space/tests/test_xxx.py::TestClass::test_func PASSED [ 50%] + m = re.match(r'(.+?::[\w:]+)\s+(PASSED|FAILED|SKIPPED|ERROR)\s*\[?\s*(\d+%?)', line_str) + if m: + name = m.group(1) + status = m.group(2).lower() + results.append({"name": name, "status": status, "duration": "", "module": name.split("::")[0].split("/")[0] if "/" in name else ""}) + # Parse duration from summary line + m2 = re.search(r'(\d+) passed.*?(\d+) failed.*?(\d+) skipped.*?in\s+([\d.]+)s', line_str) + if m2: + results.append({"name": "总计", "status": "summary", "duration": m2.group(4) + "s", "module": "", "pass": int(m2.group(1)), "fail": int(m2.group(2)), "skip": int(m2.group(3))}) + return results frontend_url = task.get("repo_url", "") frontend_branch = task.get("branch", "main") api_key = task.get("api_key", os.getenv("DEEPSEEK_API_KEY", "")) @@ -892,6 +925,10 @@ def get_report(tid): if result_file.exists(): report["results"] = json.loads(result_file.read_text()) + html_path = REPORTS_DIR / f"{tid}.html" + if html_path.exists(): + report["html_report"] = f"/api/reports/{tid}/html" + screenshot_dir = ARTIFACTS_DIR / "screenshots" if screenshot_dir.exists(): report["screenshots"] = [f.name for f in screenshot_dir.iterdir() if f.name.startswith(tid)] diff --git a/frontend/src/components/reports/ReportsView.tsx b/frontend/src/components/reports/ReportsView.tsx index d059916..5e86fcf 100644 --- a/frontend/src/components/reports/ReportsView.tsx +++ b/frontend/src/components/reports/ReportsView.tsx @@ -1,82 +1,129 @@ import { useState, useEffect } from 'react' -import { CheckCircle2, XCircle, Terminal, Eye, BarChart3 } from 'lucide-react' +import { CheckCircle2, XCircle, Eye, ExternalLink, FileText, BarChart3, ArrowLeft } from 'lucide-react' -interface Report { task_id: string; task_name: string; product: string; total_runs: number; pass: number; fail: number; started_at: string; finished_at: string; result: string; logs?: { ts: string; level: string; msg: string }[]; screenshots?: string[] } +interface Report { + task_id: string; task_name: string; product: string; total_runs: number; pass: number; fail: number + result: string; started_at: string; finished_at: string; module: string +} + +interface ReportDetail extends Report { + results: Array<{ name: string; status: string; duration: string; module: string; pass?: number; fail?: number; skip?: number }> + logs: Array<{ ts: string; level: string; msg: string }> + screenshots: string[] + html_report?: string +} export default function ReportsView() { const [reports, setReports] = useState([]) - const [selected, setSelected] = useState(null) + const [detail, setDetail] = useState(null) useEffect(() => { fetchReports() }, []) const fetchReports = () => { fetch('/api/reports').then(r => r.json()).then(setReports).catch(() => {}) } - const viewDetail = async (taskId: string) => { const res = await fetch(`/api/reports/${taskId}`); const data = await res.json(); setSelected(data) } - const levelColor = (level: string) => { switch (level) { case 'ERROR': return 'rgb(var(--color-error))'; case 'SUCCESS': return 'rgb(var(--color-success))'; case 'WARN': return 'rgb(var(--color-accent-amber))'; default: return 'rgb(var(--color-text-secondary))' } } + const viewReport = async (taskId: string) => { + const res = await fetch('/api/reports/' + taskId) + if (res.ok) setDetail(await res.json()) + } return ( -
-
-
- - 测试报告 - {reports.length} -
- {reports.length === 0 ? ( -
暂无报告,运行任务后自动生成
- ) : ( -
- {reports.map(report => ( -
viewDetail(report.task_id)}> -
-
-
- {report.result === 'PASS' ? : } -
-
-
- {report.task_name} - {report.result} -
-
通过 {report.pass}/{report.total_runs} · 失败 {report.fail}{report.finished_at && ` · ${new Date(report.finished_at).toLocaleString()}`}
-
-
- -
-
- ))} -
- )} -
+
+

报告中心 ({reports.length})

- {selected && ( + {!detail ? (
-
-
- - {selected.task_name} - {selected.result} -
- -
-
-
- {[{ label: '总运行', value: selected.total_runs, color: 'rgb(var(--color-text-primary))' }, { label: '通过', value: selected.pass, color: 'rgb(var(--color-success))' }, { label: '失败', value: selected.fail, color: 'rgb(var(--color-error))' }, { label: '耗时', value: selected.finished_at && selected.started_at ? Math.round((new Date(selected.finished_at).getTime() - new Date(selected.started_at).getTime()) / 1000) + 's' : '-', color: 'rgb(var(--color-text-primary))' }].map(stat => ( -
-
{stat.label}
-
{stat.value}
-
+ + + + + + + + + + + + + {reports.length === 0 && ( + + )} + {reports.map(r => ( + + + + + + + + ))} - - {selected.logs && selected.logs.length > 0 && ( + +
任务产品结果通过/总计时间操作
暂无报告
{r.task_name}{r.product} +
+ {r.result === 'PASS' ? : } + {r.result === 'PASS' ? '通过' : '失败'} +
+
{r.pass}/{r.total_runs}{r.finished_at?.split('T')[1]?.slice(0, 5) || '-'} + +
+
+ ) : ( +
+ + +
+
-
运行日志
-
{selected.logs.map((log, i) => (
[{log.ts}] {log.msg}
))}
+

{detail.task_name}

+

Product: {detail.product}

+
+
{detail.result === 'PASS' ? '通过' : '失败'}
+
+ +
+
{detail.total_runs}
总计运行
+
{detail.pass}
通过
+
{detail.fail}
失败
+
{detail.total_runs > 0 ? Math.round(detail.pass / detail.total_runs * 100) : 0}%
通过率
+
+ + {detail.html_report && ( + )} - {selected.screenshots && selected.screenshots.length > 0 && ( + + {detail.results && detail.results.length > 0 && (
-
截图 ({selected.screenshots.length})
-
{selected.screenshots.map(s => ({s}))}
+
测试结果
+
+ {detail.results.filter(r => r.status !== 'summary').map((r, i) => ( +
+
+
{r.name.split('::').pop() || r.name}
+
{r.module}
+
+
+ {r.duration} + {r.status === 'passed' ? : r.status === 'failed' ? : SKIP} +
+
+ ))} + {detail.results.filter(r => r.status === 'summary').map((r, i) => ( +
+
总计: {r.pass || 0} passed, {r.fail || 0} failed, {r.skip || 0} skipped
+
{r.duration}
+
+ ))} +
+
+ )} + + {detail.screenshots && detail.screenshots.length > 0 && ( +
+
截图
+
+ {detail.screenshots.map(s => ({s}))} +
)}