PAO JSON输出字段速查表:result、failures与profile一次讲透
PAO JSON输出字段速查表:result、failures与profile一次讲透
PAO 是 Laravel 出品的「为 AI Agent 优化的 PHP 测试工具输出层」。当 PHPUnit、Pest、PHPStan、Rector 等工具运行在 Claude Code、Cursor 等 AI 环境中时,PAO 会自动把冗长的终端输出压缩成一段结构恒定、字段紧凑的 JSON。这篇速查表带你一次讲透三个核心字段:result(整体结果)、failures(失败明细)、profile(慢测试清单),外加全部顶层字段对照表,新手也能直接上手。
为什么 PHP 测试输出需要变成 JSON?
传统测试工具的输出对人类很友好,但对 AI 是灾难:1000 个测试的进度点号、ANSI 颜色、框线字符会吃掉大量 token。PAO 将其转换为恒定大小的 JSON——哪怕 100 个测试还是 10000 个,输出体积几乎不变,官方数据可节省 99.8% 的 AI token。
关键机制一览:
- 零配置:
composer require laravel/pao --dev后通过 Composer 自动加载钩子,见 README.md - 只在 AI 环境激活:检测到 AI Agent 才转换输出;人工在终端运行时输出完全不变
- 支持工具:PHPUnit 12-13、Pest 4-5、Paratest、PHPStan、Rector,工具名由入口二进制识别,见 src/Execution.php
顶层 JSON 字段速查表(PHPUnit / Pest / Paratest 通用)
以下字段在测试类工具的 JSON 输出中出现,定义参考 src/Drivers/Concerns/TestResultParsable.php:
| 字段 | 类型 | 含义 | 何时出现 |
|---|---|---|---|
tool |
string | 工具名(phpunit / pest / paratest) |
总是 |
result |
string | passed 或 failed,整体结论 |
总是 |
tests |
int | 运行测试总数 | 总是 |
passed |
int | 通过数(= tests − failed − errors − skipped) | 总是 |
assertions |
int | 断言总数 | 总是 |
duration_ms |
int | 总耗时(毫秒) | 总是 |
failed |
int | 失败测试数 | 有失败时 |
failures |
array | 失败明细列表 | 有失败时 |
errors |
int | 报错测试数 | 有错误时 |
error_details |
array | 报错明细列表 | 有错误时 |
skipped |
int | 跳过数 | 有跳过时 |
incomplete |
int | 未完成标记数 | 存在时 |
risky |
int | 有风险测试数 | 存在时 |
deprecations / warnings / notices |
int | 废弃/警告/提示计数 | 大于 0 时 |
php_errors |
int | PHP 原生错误数 | 存在时 |
profile |
array | 最慢测试 Top 10 | 传 --profile 时 |
raw |
array | 插件的额外输出(已清洗) | 有额外输出时 |
💡 设计思路:只有非零的计数项才会写入 JSON,全部通过时输出最精简。
result 字段:只有 passed 和 failed 两个值
result 是 AI 判断"能不能继续"的唯一入口。它的取值逻辑(见 src/Drivers/Concerns/TestResultParsable.php#L231-L232):
passed:断言全部通过。注意——skipped、incomplete 不影响 passed,跳过若干测试依然返回passed(有对应计数字段兜底)failed:存在 failed 或 error,或"零测试"且配置了failOnEmptyTestSuite(此时raw会带"No tests found.",测试见 tests/Drivers/PestTest.php#L79-L90)
一个典型的"全绿"输出:
{
"tool": "pest",
"result": "passed",
"tests": 2,
"passed": 2,
"assertions": 3,
"duration_ms": 210
}
failures 字段:定位失败的四个要素
测试失败时,failures 数组中每一项都包含可直接跳转的信息:
"failures": [
{
"test": "App\\Tests\\FailingTest::test_it_works",
"file": "tests/FailingTest.php",
"line": 14,
"message": "Failed asserting that true is false.",
"trace": ["tests/Support/ChecksTotals.php:13", "tests/FailingTest.php:14"]
}
]
| 子字段 | 说明 |
|---|---|
test |
测试名(含类名),数据驱动测试含数据集标识 |
file / line |
失败精确位置;无法直接取得时会从异常栈中回退解析,见 resolveTestLocation |
message |
断言失败/异常消息,已去除多余空白 |
trace |
最多 5 帧调用栈,且自动过滤 /vendor/ 框架帧,见 stackTraceFrames |
🔍 易混点:failures 对应"断言失败"(failed),而运行时报错(如方法不存在、PHP 致命错误)走 errors + error_details 两个字段,两者结构相同、含义不同,AI 修 bug 时会分别处理。报错场景验证见 tests/Drivers/PestTest.php#L25-L32。
profile 字段:一条命令找出最慢测试
给测试命令加上 --profile,PAO 会在 JSON 中追加 profile 数组——按耗时倒序的 Top 10 慢测试:
"profile": [
{ "test": "App\\Tests\\SlowQueryTest::test_it", "file": "tests/SlowQueryTest.php", "duration_ms": 850 },
{ "test": "App\\Tests\\CacheTest::test_it", "file": "tests/CacheTest.php", "duration_ms": 120 }
]
实现原理:ProfileCollector 通过 PHPUnit 事件系统监听每个测试的 Prepared/Finished 事件计时,排序与截取逻辑在 src/Drivers/Concerns/TestResultParsable.php#L297-L302。Pest 的 --parallel 并行模式同样支持,见 src/Drivers/Pest/Starter.php#L36-L41。
⏱️ 不传 --profile 时该字段完全缺席,不占 token。
其他工具的专属字段
PHPStan 与 Rector 不走测试事件体系,字段略有差异(示例来自 README.md):
- PHPStan:
result+errors计数 +error_details(按文件分组,含line、message、identifier);解析失败或参数错误时原始 stderr 归入raw,见 src/Drivers/Phpstan/Starter.php - Rector:
totals(changed_files/errors)、file_diffs(含统一 diff 与applied_rectors规则名)、changed_files文件列表 raw数组:Pest 插件(如--coverage、--profile)等额外输出会被捕获、剥离 ANSI 与装饰字符后按行放入raw,见 src/Drivers/Concerns/TestResultParsable.php#L82-L97
常见问题:为什么我看不到 JSON?
① 终端里手动运行没变化?
这是预期行为——PAO 只在检测到 AI Agent(环境变量如 AI_AGENT、CLAUDECODE、CURSOR_AGENT)时激活。调试可用 PAO_FORCE=true 强制开启,验证见 tests/Drivers/PestTest.php#L160-L165。
② Docker / 容器里不生效?
把 AI_AGENT、CLAUDECODE 等环境变量转发进容器即可,或对该命令设置 PAO_FORCE=true,详见 README.md。
③ PHPUnit 输出被吞了?
PAO 会向 PHPUnit 注入 --no-output 静默原生进度输出(src/Drivers/Phpunit/Starter.php#L36-L40),Pest 则通过输出过滤器静默 stdout,最终只输出 PAO 生成的 JSON。
速记口诀:result 看结论 → failures / error_details 找位置 → profile 抓慢测试 → raw 兜底看原始输出。掌握这四个入口,AI Agent 修 PHP 测试的效率提升立竿见影 🚀
