Workflow 系列(05):评测体系——三层测试结构与 Trace 追踪

📅 2026/7/3 1:47:58
Workflow 系列(05):评测体系——三层测试结构与 Trace 追踪
为什么 Workflow 需要独立的评测体系传统软件测试覆盖代码正确性。Workflow 多了两层不确定性:LLM 输出不确定:同一个输入,不同运行结果可能不同跨步骤依赖:Phase 3 的问题可能在 Phase 7 才暴露,排查链路长没有评测体系,Workflow 修改后只能靠完整端到端验证:慢、贵、覆盖不全。三层测试把这个问题分解。三层评测结构Layer 3:端到端测试(Workflow 级) 从触发到完成的完整链路 测试用例:eval/cases.yaml 指标:完成率、Phase 4 平均轮次、人工门触发率 Layer 2:集成测试(Phase 级) 跨 Step 的数据流是否正确传递 跨 Phase 的路由逻辑是否正确 Layer 1:单元测试(Step 级) 每个子 Agent 的输出是否符合输出契约 不调用真实 LLM,只验证 JSON Schema测试优先级:Layer 1 应该是最多的,运行最快,能在秒级发现契约问题。Layer 3 运行最慢、成本最高,只在修改影响主链路时跑。Layer 1:Step 级单元测试单元测试的目标:验证子 Agent 输出的 JSON 结构是否符合预期契约,不需要真实的 LLM 调用。# tests/unit/test_phase3_output.pyimportjsonfrompathlibimportPathdeftest_analysis_output_schema():"""Phase 3 的输出文件必须符合 analysis_final.json 的 Schema"""output=json.loads(Path("test_fixtures/phase3/analysis_final.json").read_text())# 必填字段assert"passed"inoutputassertisinstance(output["passed"],bool)assert"confidence"inoutputassert0.0=output["confidence"]=1.0assert"root_cause"inoutputassertisinstance(output["root_cause"],str|type(None))assert"evidence"inoutputassertisinstance(output["evidence"],list)# 失败时的必填字段ifnotoutput["passed"]:assert"error"inoutputassertoutput["error"]# 不能是空字符串deftest_fix_candidate_output_schema():"""Phase 4 每个候选的输出文件 Schema"""forcandidatein["candidate_a","candidate_b","candidate_c"]:output_file=Path(