platform-apex-test-run: Salesforce Test Execution & Coverage Analysis
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.
When This Skill Owns the Task
Use platform-apex-test-run when the work involves:
sf apex run test workflows
- 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 →
platform-apex-generate skill
- testing Agentforce agents →
agentforce-test skill
- testing LWC with Jest → 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
Identify:
- existing test classes
- target production classes
- test data factories / setup helpers
2. Run the smallest useful test set first
Start narrow when debugging a failure; widen only after the fix is stable.
3. Analyze results
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
When the issue is code or test quality:
- delegate code fixes to
platform-apex-generate skill when needed
- add or improve tests
- rerun focused tests before broader regression
5. Improve coverage intentionally
Cover:
- positive path
- negative / exception path
- bulk path (251+ records where appropriate)
- callout or async path when relevant
High-Signal Rules
| Rule |
Rationale |
Default to SeeAllData=false |
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 / @TestSetup when they improve clarity |
Consistent data creation in one place; rolled back between test methods |
Pair Test.startTest() with Test.stopTest() for async |
Ensures async operations (queueable, future) complete before assertions |
| Do not hide flaky org dependencies inside tests |
Prevents intermittent failures tied to org state |
Gotchas
| Issue |
Resolution |
| Test passes locally but fails in CI org |
Check for SeeAllData=true or undeclared dependencies on org-specific records |
| Coverage drops unexpectedly after refactor |
Run focused class-level tests first, then widen to RunLocalTests to confirm |
| "Uncommitted work pending" error in callout test |
DML and HTTP callouts cannot be mixed in the same test context without Test.startTest() wrapping |
| Mock not taking effect in test |
Ensure Test.setMock() is called before the code that makes the callout |
@TestSetup data missing in test method |
@TestSetup data is committed per test method — re-query it; do not store in static variables |
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:
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 |
platform-apex-generate skill |
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 |
Reference File Index
| File |
When to read |
references/cli-commands.md |
All sf apex run test command flags, output formats, async execution, and coverage commands |
references/test-patterns.md |
Test class templates — basic, bulk (251+), mock callout, and data factory patterns |
references/testing-best-practices.md |
Core testing principles — AAA pattern, naming conventions, bulk, negative, and mock strategies |
references/test-fix-loop.md |
Agentic test-fix loop implementation and failure analysis decision tree |
references/mocking-patterns.md |
HttpCalloutMock, DML mocking, StubProvider, and selector mocking patterns |
references/performance-optimization.md |
Techniques to reduce test execution time — DML mocking, SOQL mocking, loop optimizations |
assets/basic-test.cls |
Template: standard test class with @TestSetup, positive / negative / bulk / edge-case methods |
assets/bulk-test.cls |
Template: bulk test with 251+ records that crosses the 200-record trigger batch boundary |
assets/mock-callout-test.cls |
Template: HTTP callout mock using HttpCalloutMock |
assets/test-data-factory.cls |
Template: reusable TestDataFactory with create and insert helpers |
assets/dml-mock.cls |
Template: IDML interface + DMLMock implementation for database-free unit tests |
assets/stub-provider-example.cls |
Template: StubProvider-based dependency injection stub |
scripts/parse-test-results.py |
Post-tool hook — parses sf apex run test JSON output and formats failures for the auto-fix loop |
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 |
1---2name: platform-apex-test-run3description: "Apex test execution, coverage analysis, and test-fix loops with 120-point scoring. Use when the user needs to run Apex tests, check code coverage, fix failing tests, or work with *Test.cls / *_Test.cls files. TRIGGER when: user runs Apex tests, checks code coverage, fixes failing tests, or touches *Test.cls / *_Test.cls files. DO NOT TRIGGER when: writing Apex production code (use platform-apex-generate), Agentforce agent testing (use agentforce-test), or Jest/LWC tests (use experience-lwc-generate)."4metadata:5 source: https://skillmd.com/skills/om-scogo/platform-apex-test-run6 version: "1.1"7---89# platform-apex-test-run: Salesforce Test Execution & Coverage Analysis1011Use 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.1213## When This Skill Owns the Task1415Use `platform-apex-test-run` when the work involves:16- `sf apex run test` workflows17- Apex unit-test failures18- code coverage analysis19- identifying uncovered lines and missing test scenarios20- structured test-fix loops for Apex code2122Delegate elsewhere when the user is:23- writing or refactoring production Apex → `platform-apex-generate` skill24- testing Agentforce agents → `agentforce-test` skill25- testing LWC with Jest → [experience-lwc-generate](../experience-lwc-generate/SKILL.md)2627---2829## Required Context to Gather First3031Ask for or infer:32- target org alias33- desired test scope: single class, specific methods, suite, or local tests34- coverage threshold expectation35- whether the user wants diagnosis only or a test-fix loop36- whether related test data factories already exist3738---3940## Recommended Workflow4142### 1. Discover test scope43Identify:44- existing test classes45- target production classes46- test data factories / setup helpers4748### 2. Run the smallest useful test set first49Start narrow when debugging a failure; widen only after the fix is stable.5051### 3. Analyze results52Focus on:53- failing methods54- exception types and stack traces55- uncovered lines / weak coverage areas56- whether failures indicate bad test data, brittle assertions, or broken production logic5758### 4. Run a disciplined fix loop59When the issue is code or test quality:60- delegate code fixes to `platform-apex-generate` skill when needed61- add or improve tests62- rerun focused tests before broader regression6364### 5. Improve coverage intentionally65Cover:66- positive path67- negative / exception path68- bulk path (251+ records where appropriate)69- callout or async path when relevant7071---7273## High-Signal Rules7475| Rule | Rationale |76|------|-----------|77| Default to `SeeAllData=false` | Ensures test isolation; prevents reliance on org-specific data |78| Every test must assert meaningful outcomes | Tests with no assertions prove nothing and give false confidence |79| Test bulk behavior with 251+ records | Triggers process in batches of 200; 251 records crosses the boundary |80| Use factories / `@TestSetup` when they improve clarity | Consistent data creation in one place; rolled back between test methods |81| Pair `Test.startTest()` with `Test.stopTest()` for async | Ensures async operations (queueable, future) complete before assertions |82| Do not hide flaky org dependencies inside tests | Prevents intermittent failures tied to org state |8384---8586## Gotchas8788| Issue | Resolution |89|-------|------------|90| Test passes locally but fails in CI org | Check for `SeeAllData=true` or undeclared dependencies on org-specific records |91| Coverage drops unexpectedly after refactor | Run focused class-level tests first, then widen to `RunLocalTests` to confirm |92| "Uncommitted work pending" error in callout test | DML and HTTP callouts cannot be mixed in the same test context without `Test.startTest()` wrapping |93| Mock not taking effect in test | Ensure `Test.setMock()` is called before the code that makes the callout |94| `@TestSetup` data missing in test method | `@TestSetup` data is committed per test method — re-query it; do not store in static variables |9596---9798## Output Format99100When finishing, report in this order:1011. **What tests were run**1022. **Pass/fail summary**1033. **Coverage result**1044. **Root-cause findings**1055. **Fix or next-run recommendation**106107Suggested shape:108109```text110Test run: <scope>111Org: <alias>112Result: <passed / partial / failed>113Coverage: <percent / key classes>114Issues: <highest-signal failures>115Next step: <fix class, add test, rerun scope, or widen regression>116```117118---119120## Cross-Skill Integration121122| Need | Delegate to | Reason |123|------|-------------|--------|124| Fix production code or author test classes | `platform-apex-generate` skill | Code generation and repair |125| Create bulk / edge-case test data | [platform-data-manage](../platform-data-manage/SKILL.md) | Realistic test datasets |126| Deploy updated tests to org | [platform-metadata-deploy](../platform-metadata-deploy/SKILL.md) | Deployment workflows |127| Inspect detailed runtime logs | [platform-apex-logs-debug](../platform-apex-logs-debug/SKILL.md) | Deeper failure analysis |128129---130131## Reference File Index132133| File | When to read |134|------|-------------|135| `references/cli-commands.md` | All `sf apex run test` command flags, output formats, async execution, and coverage commands |136| `references/test-patterns.md` | Test class templates — basic, bulk (251+), mock callout, and data factory patterns |137| `references/testing-best-practices.md` | Core testing principles — AAA pattern, naming conventions, bulk, negative, and mock strategies |138| `references/test-fix-loop.md` | Agentic test-fix loop implementation and failure analysis decision tree |139| `references/mocking-patterns.md` | HttpCalloutMock, DML mocking, StubProvider, and selector mocking patterns |140| `references/performance-optimization.md` | Techniques to reduce test execution time — DML mocking, SOQL mocking, loop optimizations |141| `assets/basic-test.cls` | Template: standard test class with `@TestSetup`, positive / negative / bulk / edge-case methods |142| `assets/bulk-test.cls` | Template: bulk test with 251+ records that crosses the 200-record trigger batch boundary |143| `assets/mock-callout-test.cls` | Template: HTTP callout mock using `HttpCalloutMock` |144| `assets/test-data-factory.cls` | Template: reusable `TestDataFactory` with create and insert helpers |145| `assets/dml-mock.cls` | Template: `IDML` interface + `DMLMock` implementation for database-free unit tests |146| `assets/stub-provider-example.cls` | Template: `StubProvider`-based dependency injection stub |147| `scripts/parse-test-results.py` | Post-tool hook — parses `sf apex run test` JSON output and formats failures for the auto-fix loop |148149---150151## Score Guide152153| Score | Meaning |154|---|---|155| 108+ | strong production-grade test confidence |156| 96–107 | good test suite with minor gaps |157| 84–95 | acceptable but strengthen coverage / assertions |158| < 84 | below standard; revise before relying on it |