platform-apex-test-run
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseplatform-apex-test-run: Salesforce Test Execution & Coverage Analysis
platform-apex-test-run: Salesforce测试执行与覆盖率分析
Use this skill when the user needs Apex test execution and failure analysis: running tests, checking coverage, interpreting failures, improving coverage, and managing a disciplined test-fix loop for Salesforce code.
当用户需要Apex测试执行与失败分析时使用此技能:运行测试、检查覆盖率、解读失败原因、提升覆盖率,以及为Salesforce代码管理规范的测试修复循环。
When This Skill Owns the Task
此技能负责的任务场景
Use when the work involves:
platform-apex-test-run- workflows
sf apex run test - Apex unit-test failures
- code coverage analysis
- identifying uncovered lines and missing test scenarios
- structured test-fix loops for Apex code
Delegate elsewhere when the user is:
- writing or refactoring production Apex → skill
platform-apex-generate - testing Agentforce agents → skill
agentforce-test - testing LWC with Jest → experience-lwc-generate
当工作涉及以下内容时,使用:
platform-apex-test-run- 工作流
sf apex run test - Apex单元测试失败
- 代码覆盖率分析
- 识别未覆盖代码行和缺失的测试场景
- Apex代码的结构化测试修复循环
当用户进行以下操作时,请转交至其他技能:
- 编写或重构生产环境Apex代码 → 使用技能
platform-apex-generate - 测试Agentforce代理 → 使用技能
agentforce-test - 使用Jest测试LWC → experience-lwc-generate
Required Context to Gather First
需先收集的必要上下文
Ask for or infer:
- target org alias
- desired test scope: single class, specific methods, suite, or local tests
- coverage threshold expectation
- whether the user wants diagnosis only or a test-fix loop
- whether related test data factories already exist
询问或推断以下信息:
- 目标组织别名
- 期望的测试范围:单个类、特定方法、测试套件或本地测试
- 覆盖率阈值要求
- 用户仅需要诊断还是需要完整的测试修复循环
- 是否已存在相关测试数据工厂
Recommended Workflow
推荐工作流程
1. Discover test scope
1. 确定测试范围
Identify:
- existing test classes
- target production classes
- test data factories / setup helpers
识别:
- 现有测试类
- 目标生产类
- 测试数据工厂/设置辅助工具
2. Run the smallest useful test set first
2. 先运行最小有效测试集
Start narrow when debugging a failure; widen only after the fix is stable.
调试失败问题时从窄范围开始;修复稳定后再扩大范围。
3. Analyze results
3. 分析结果
Focus on:
- failing methods
- exception types and stack traces
- uncovered lines / weak coverage areas
- whether failures indicate bad test data, brittle assertions, or broken production logic
重点关注:
- 失败的方法
- 异常类型和堆栈跟踪
- 未覆盖代码行/覆盖率薄弱区域
- 失败原因是测试数据问题、断言脆弱还是生产逻辑损坏
4. Run a disciplined fix loop
4. 执行规范的修复循环
When the issue is code or test quality:
- delegate code fixes to skill when needed
platform-apex-generate - add or improve tests
- rerun focused tests before broader regression
当问题源于代码或测试质量时:
- 必要时将代码修复任务转交至技能
platform-apex-generate - 添加或优化测试
- 在进行更广泛的回归测试前,重新运行聚焦测试
5. Improve coverage intentionally
5. 有针对性地提升覆盖率
Cover:
- positive path
- negative / exception path
- bulk path (251+ records where appropriate)
- callout or async path when relevant
覆盖以下场景:
- 正向路径
- 异常/错误路径
- 批量路径(适当时使用251+条记录)
- 相关的调用或异步路径
High-Signal Rules
高价值规则
| Rule | Rationale |
|---|---|
Default to | Ensures test isolation; prevents reliance on org-specific data |
| Every test must assert meaningful outcomes | Tests with no assertions prove nothing and give false confidence |
| Test bulk behavior with 251+ records | Triggers process in batches of 200; 251 records crosses the boundary |
Use factories / | Consistent data creation in one place; rolled back between test methods |
Pair | Ensures async operations (queueable, future) complete before assertions |
| Do not hide flaky org dependencies inside tests | Prevents intermittent failures tied to org state |
| 规则 | 原理 |
|---|---|
默认使用 | 确保测试隔离;避免依赖组织特定数据 |
| 每个测试必须断言有意义的结果 | 无断言的测试无法证明任何内容,会带来虚假的信心 |
| 使用251+条记录测试批量行为 | 触发按200条批量处理的流程;251条记录会跨越边界 |
当工厂/ | 在一处统一创建数据;测试方法间会回滚数据 |
异步操作需搭配 | 确保异步操作(可排队、未来式)在断言前完成 |
| 不要在测试中隐藏不稳定的组织依赖 | 避免与组织状态相关的间歇性失败 |
Gotchas
常见陷阱
| Issue | Resolution |
|---|---|
| Test passes locally but fails in CI org | Check for |
| Coverage drops unexpectedly after refactor | Run focused class-level tests first, then widen to |
| "Uncommitted work pending" error in callout test | DML and HTTP callouts cannot be mixed in the same test context without |
| Mock not taking effect in test | Ensure |
| |
| 问题 | 解决方案 |
|---|---|
| 本地测试通过但CI环境测试失败 | 检查是否使用了 |
| 重构后覆盖率意外下降 | 先运行聚焦类级别的测试,再扩大到 |
| 调用测试中出现"未提交的工作待处理"错误 | 在同一测试上下文中,DML和HTTP调用不能混合使用,需用 |
| 测试中Mock未生效 | 确保在发起调用的代码执行前调用 |
测试方法中缺失 | |
Output Format
输出格式
When finishing, report in this order:
- What tests were run
- Pass/fail summary
- Coverage result
- Root-cause findings
- Fix or next-run recommendation
Suggested shape:
text
Test run: <scope>
Org: <alias>
Result: <passed / partial / failed>
Coverage: <percent / key classes>
Issues: <highest-signal failures>
Next step: <fix class, add test, rerun scope, or widen regression>完成任务后,按以下顺序报告:
- 运行的测试范围
- 通过/失败汇总
- 覆盖率结果
- 根本原因分析
- 修复或下一步测试建议
建议格式:
text
Test run: <scope>
Org: <alias>
Result: <passed / partial / failed>
Coverage: <percent / key classes>
Issues: <highest-signal failures>
Next step: <fix class, add test, rerun scope, or widen regression>Cross-Skill Integration
跨技能集成
| Need | Delegate to | Reason |
|---|---|---|
| Fix production code or author test classes | | Code generation and repair |
| Create bulk / edge-case test data | platform-data-manage | Realistic test datasets |
| Deploy updated tests to org | platform-metadata-deploy | Deployment workflows |
| Inspect detailed runtime logs | platform-apex-logs-debug | Deeper failure analysis |
| 需求 | 转交至 | 原因 |
|---|---|---|
| 修复生产代码或编写测试类 | | 代码生成与修复 |
| 创建批量/边缘场景测试数据 | platform-data-manage | 生成真实测试数据集 |
| 将更新后的测试部署到组织 | platform-metadata-deploy | 部署工作流 |
| 查看详细运行时日志 | platform-apex-logs-debug | 深度失败分析 |
Reference File Index
参考文件索引
| File | When to read |
|---|---|
| All |
| Test class templates — basic, bulk (251+), mock callout, and data factory patterns |
| Core testing principles — AAA pattern, naming conventions, bulk, negative, and mock strategies |
| Agentic test-fix loop implementation and failure analysis decision tree |
| HttpCalloutMock, DML mocking, StubProvider, and selector mocking patterns |
| Techniques to reduce test execution time — DML mocking, SOQL mocking, loop optimizations |
| Template: standard test class with |
| Template: bulk test with 251+ records that crosses the 200-record trigger batch boundary |
| Template: HTTP callout mock using |
| Template: reusable |
| Template: |
| Template: |
| Post-tool hook — parses |
| 文件 | 阅读场景 |
|---|---|
| 所有 |
| 测试类模板——基础、批量(251+条)、Mock调用及数据工厂模式 |
| 核心测试原则——AAA模式、命名规范、批量、异常及Mock策略 |
| 智能测试修复循环实现及失败分析决策树 |
| HttpCalloutMock、DML Mocking、StubProvider及选择器Mocking模式 |
| 减少测试执行时间的技巧——DML Mocking、SOQL Mocking、循环优化 |
| 模板:包含 |
| 模板:包含251+条记录的批量测试,跨越200条记录的触发器批量边界 |
| 模板:使用 |
| 模板:可复用的 |
| 模板: |
| 模板:基于 |
| 工具后处理钩子——解析 |
Score Guide
评分指南
| Score | Meaning |
|---|---|
| 108+ | strong production-grade test confidence |
| 96–107 | good test suite with minor gaps |
| 84–95 | acceptable but strengthen coverage / assertions |
| < 84 | below standard; revise before relying on it |
| 分数 | 含义 |
|---|---|
| 108+ | 强生产级测试可信度 |
| 96–107 | 良好测试套件,存在微小漏洞 |
| 84–95 | 可接受,但需强化覆盖率/断言 |
| < 84 | 低于标准,依赖前需修订 |