feat: 测试报告对齐原平台样式,新增pytest结果解析+HTML报告链接

This commit is contained in:
jiawei.chen 2026-07-08 16:25:26 +08:00
parent 252d397651
commit fd8731fe03
2 changed files with 149 additions and 65 deletions

View File

@ -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)]

View File

@ -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<Report[]>([])
const [selected, setSelected] = useState<Report | null>(null)
const [detail, setDetail] = useState<ReportDetail | null>(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 (
<div className="space-y-6">
<div className="bg-[rgb(var(--color-surface))] border border-[rgb(var(--color-border))] rounded-xl overflow-hidden">
<div className="p-4 border-b border-[rgb(var(--color-border))] flex items-center gap-2">
<BarChart3 className="w-4 h-4 text-[rgb(var(--color-accent))]" />
<span className="text-sm font-medium text-[rgb(var(--color-text-primary))]">测试报告</span>
<span className="text-xs px-1.5 py-0.5 rounded bg-[rgb(var(--color-accent)/0.1)] text-[rgb(var(--color-accent))]">{reports.length}</span>
</div>
{reports.length === 0 ? (
<div className="p-12 text-center text-sm text-[rgb(var(--color-text-secondary))]">暂无报告,运行任务后自动生成</div>
) : (
<div className="divide-y divide-[rgb(var(--color-border))]">
{reports.map(report => (
<div key={report.task_id} className="p-4 hover:bg-[rgb(var(--color-surface-hover))] transition-colors cursor-pointer" onClick={() => viewDetail(report.task_id)}>
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-8 h-8 rounded-lg flex items-center justify-center shrink-0" style={{ backgroundColor: report.result === 'PASS' ? 'rgb(var(--color-success)/0.1)' : 'rgb(var(--color-error)/0.1)' }}>
{report.result === 'PASS' ? <CheckCircle2 className="w-4 h-4 text-[rgb(var(--color-success))]" /> : <XCircle className="w-4 h-4 text-[rgb(var(--color-error))]" />}
</div>
<div>
<div className="flex items-center gap-2">
<span className="text-sm font-medium text-[rgb(var(--color-text-primary))]">{report.task_name}</span>
<span className="text-[10px] px-1.5 py-0.5 rounded font-medium" style={{ backgroundColor: report.result === 'PASS' ? 'rgb(var(--color-success)/0.1)' : 'rgb(var(--color-error)/0.1)', color: report.result === 'PASS' ? 'rgb(var(--color-success))' : 'rgb(var(--color-error))' }}>{report.result}</span>
</div>
<div className="text-xs mt-1 text-[rgb(var(--color-text-secondary))]">通过 {report.pass}/{report.total_runs} · 失败 {report.fail}{report.finished_at && ` · ${new Date(report.finished_at).toLocaleString()}`}</div>
</div>
</div>
<Eye className="w-4 h-4 text-[rgb(var(--color-text-secondary))]" />
</div>
</div>
))}
</div>
)}
</div>
<div className="space-y-4">
<h2 className="text-sm font-medium text-[rgb(var(--color-text-primary))]">报告中心 ({reports.length})</h2>
{selected && (
{!detail ? (
<div className="bg-[rgb(var(--color-surface))] border border-[rgb(var(--color-border))] rounded-xl overflow-hidden">
<div className="p-4 border-b border-[rgb(var(--color-border))] flex items-center justify-between">
<div className="flex items-center gap-2">
<Eye className="w-4 h-4 text-[rgb(var(--color-accent))]" />
<span className="text-sm font-semibold text-[rgb(var(--color-text-primary))]">{selected.task_name}</span>
<span className="text-[10px] px-1.5 py-0.5 rounded font-medium" style={{ backgroundColor: selected.result === 'PASS' ? 'rgb(var(--color-success)/0.1)' : 'rgb(var(--color-error)/0.1)', color: selected.result === 'PASS' ? 'rgb(var(--color-success))' : 'rgb(var(--color-error))' }}>{selected.result}</span>
</div>
<button onClick={() => setSelected(null)} className="text-xs text-[rgb(var(--color-text-secondary))]">关闭</button>
</div>
<div className="p-4 space-y-4">
<div className="grid grid-cols-4 gap-4">
{[{ 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 => (
<div key={stat.label} className="rounded-lg p-3 bg-[rgb(var(--color-surface-hover))]">
<div className="text-xs text-[rgb(var(--color-text-secondary))]">{stat.label}</div>
<div className="text-lg font-bold" style={{ color: stat.color }}>{stat.value}</div>
</div>
<table className="w-full text-sm">
<thead>
<tr className="border-b border-[rgb(var(--color-border))] text-xs text-[rgb(var(--color-text-secondary))]">
<th className="text-left px-4 py-3 font-medium">任务</th>
<th className="text-left px-4 py-3 font-medium">产品</th>
<th className="text-left px-4 py-3 font-medium">结果</th>
<th className="text-left px-4 py-3 font-medium">通过/总计</th>
<th className="text-left px-4 py-3 font-medium">时间</th>
<th className="text-right px-4 py-3 font-medium">操作</th>
</tr>
</thead>
<tbody>
{reports.length === 0 && (
<tr><td colSpan={6} className="text-center py-12 text-[rgb(var(--color-text-secondary))] text-xs">暂无报告</td></tr>
)}
{reports.map(r => (
<tr key={r.task_id} className="border-b border-[rgb(var(--color-border)/0.5)] hover:bg-[rgb(var(--color-surface-hover))] transition-colors">
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-primary))]">{r.task_name}</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-secondary))]">{r.product}</td>
<td className="px-4 py-3">
<div className="flex items-center gap-1">
{r.result === 'PASS' ? <CheckCircle2 className="w-4 h-4 text-[rgb(var(--color-success))]" /> : <XCircle className="w-4 h-4 text-[rgb(var(--color-error))]" />}
<span className="text-xs" style={{ color: r.result === 'PASS' ? 'rgb(var(--color-success))' : 'rgb(var(--color-error))' }}>{r.result === 'PASS' ? '通过' : '失败'}</span>
</div>
</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-primary))]">{r.pass}/{r.total_runs}</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-secondary))]">{r.finished_at?.split('T')[1]?.slice(0, 5) || '-'}</td>
<td className="px-4 py-3 text-right">
<button onClick={() => viewReport(r.task_id)} className="p-1.5 rounded hover:bg-[rgb(var(--color-accent)/0.2)] text-[rgb(var(--color-accent))] cursor-pointer" title="查看详情"><Eye className="w-3.5 h-3.5" /></button>
</td>
</tr>
))}
</div>
{selected.logs && selected.logs.length > 0 && (
</tbody>
</table>
</div>
) : (
<div className="space-y-4">
<button onClick={() => setDetail(null)} className="text-xs text-[rgb(var(--color-accent))] hover:underline cursor-pointer flex items-center gap-1"><ArrowLeft className="w-3 h-3" />返回列表</button>
<div className="bg-[rgb(var(--color-surface))] border border-[rgb(var(--color-border))] rounded-xl p-5">
<div className="flex items-center justify-between mb-4">
<div>
<div className="flex items-center gap-1 mb-2 text-xs text-[rgb(var(--color-text-secondary))]"><Terminal className="w-3 h-3" /> 运行日志</div>
<div className="max-h-80 overflow-auto p-3 rounded-lg font-mono text-xs bg-[rgb(var(--color-page))]">{selected.logs.map((log, i) => (<div key={i} style={{ color: levelColor(log.level) }}><span>[{log.ts}]</span> {log.msg}</div>))}</div>
<h3 className="text-sm font-semibold text-[rgb(var(--color-text-primary))]">{detail.task_name}</h3>
<p className="text-xs text-[rgb(var(--color-text-secondary))]">Product: {detail.product}</p>
</div>
<div className="px-3 py-1 rounded-lg text-xs font-medium" style={{ backgroundColor: detail.result === 'PASS' ? 'rgb(var(--color-success)/0.2)' : 'rgb(var(--color-error)/0.2)', color: detail.result === 'PASS' ? 'rgb(var(--color-success))' : 'rgb(var(--color-error))' }}>{detail.result === 'PASS' ? '通过' : '失败'}</div>
</div>
<div className="grid grid-cols-4 gap-4 mb-6">
<div className="text-center"><div className="text-lg font-bold text-[rgb(var(--color-text-primary))]">{detail.total_runs}</div><div className="text-xs text-[rgb(var(--color-text-secondary))]">总计运行</div></div>
<div className="text-center"><div className="text-lg font-bold text-[rgb(var(--color-success))]">{detail.pass}</div><div className="text-xs text-[rgb(var(--color-text-secondary))]">通过</div></div>
<div className="text-center"><div className="text-lg font-bold text-[rgb(var(--color-error))]">{detail.fail}</div><div className="text-xs text-[rgb(var(--color-text-secondary))]">失败</div></div>
<div className="text-center"><div className="text-lg font-bold text-[rgb(var(--color-text-primary))]">{detail.total_runs > 0 ? Math.round(detail.pass / detail.total_runs * 100) : 0}%</div><div className="text-xs text-[rgb(var(--color-text-secondary))]">通过率</div></div>
</div>
{detail.html_report && (
<div className="mb-4">
<a href={detail.html_report} target="_blank" rel="noopener noreferrer" className="inline-flex items-center gap-2 px-4 py-2 bg-[rgb(var(--color-accent)/0.1)] text-[rgb(var(--color-accent))] text-xs font-medium rounded-lg hover:bg-[rgb(var(--color-accent)/0.2)] transition-colors"><ExternalLink className="w-3.5 h-3.5" />查看完整测试报告 Dashboard</a>
</div>
)}
{selected.screenshots && selected.screenshots.length > 0 && (
{detail.results && detail.results.length > 0 && (
<div>
<div className="text-xs mb-2 text-[rgb(var(--color-text-secondary))]">截图 ({selected.screenshots.length})</div>
<div className="flex gap-2 flex-wrap">{selected.screenshots.map(s => (<img key={s} src={`/artifacts/screenshots/${s}`} alt={s} className="rounded-lg border border-[rgb(var(--color-border))] max-h-32" />))}</div>
<div className="text-xs font-medium text-[rgb(var(--color-text-primary))] mb-3 flex items-center gap-2"><FileText className="w-3.5 h-3.5" />测试结果</div>
<div className="space-y-2">
{detail.results.filter(r => r.status !== 'summary').map((r, i) => (
<div key={i} className="flex items-center justify-between bg-[rgb(var(--color-surface-hover))] rounded-lg px-4 py-3">
<div className="flex-1 min-w-0">
<div className="text-xs text-[rgb(var(--color-text-primary))] truncate">{r.name.split('::').pop() || r.name}</div>
<div className="text-[10px] text-[rgb(var(--color-text-secondary))]">{r.module}</div>
</div>
<div className="flex items-center gap-3 shrink-0 ml-3">
<span className="text-xs text-[rgb(var(--color-text-secondary))]">{r.duration}</span>
{r.status === 'passed' ? <CheckCircle2 className="w-4 h-4 text-[rgb(var(--color-success))]" /> : r.status === 'failed' ? <XCircle className="w-4 h-4 text-[rgb(var(--color-error))]" /> : <span className="text-xs text-[rgb(var(--color-text-secondary))]">SKIP</span>}
</div>
</div>
))}
{detail.results.filter(r => r.status === 'summary').map((r, i) => (
<div key={'summary'} className="flex items-center justify-between bg-[rgb(var(--color-surface-hover))] rounded-lg px-4 py-3">
<div className="text-xs text-[rgb(var(--color-text-primary))]">总计: {r.pass || 0} passed, {r.fail || 0} failed, {r.skip || 0} skipped</div>
<div className="text-xs text-[rgb(var(--color-text-secondary))]">{r.duration}</div>
</div>
))}
</div>
</div>
)}
{detail.screenshots && detail.screenshots.length > 0 && (
<div className="mt-6">
<div className="text-xs font-medium text-[rgb(var(--color-text-primary))] mb-3">截图</div>
<div className="flex gap-2 flex-wrap">
{detail.screenshots.map(s => (<img key={s} src={'/artifacts/screenshots/' + s} alt={s} className="w-32 h-24 object-cover rounded-lg border border-[rgb(var(--color-border))]" />))}
</div>
</div>
)}
</div>