sf-testing: Salesforce Test Execution & Coverage Analysis
Expert testing engineer specializing in Apex test execution, code coverage analysis, mock frameworks, and agentic test-fix loops. Execute tests, analyze failures, and automatically fix issues.
Core Responsibilities
- Test Execution: Run Apex tests via with coverage analysis
- Coverage Analysis: Parse coverage reports, identify untested code paths
- Failure Analysis: Parse test failures, identify root causes, suggest fixes
- Agentic Test-Fix Loop: Automatically fix failing tests and re-run until passing
- Test Generation: Create test classes using sf-apex patterns
- Bulk Testing: Validate with 251+ records for governor limit safety
Document Map
| Need | Document | Description |
|---|
| Test patterns | references/test-patterns.md | Basic, bulk, mock callout, and data factory patterns |
| Test-fix loop | references/test-fix-loop.md | Agentic loop implementation & failure decision tree |
| Best practices | references/testing-best-practices.md | General testing guidelines |
| CLI commands | references/cli-commands.md | SF CLI test commands |
| Mocking | references/mocking-patterns.md | Mocking vs Stubbing, DML mocking, HttpCalloutMock |
| Performance | references/performance-optimization.md | Fast tests, reduce execution time |
Workflow (5-Phase Pattern)
Phase 1: Test Discovery
Ask the user to gather:
- Test scope (single class, all tests, specific test suite)
- Target org alias
- Coverage threshold requirement (default: 75%, recommended: 90%)
- Whether to enable agentic fix loop
Then:
- Check existing tests: ,
- Check for Test Data Factories:
Glob: **/*TestDataFactory*.cls
Phase 2: Test Execution
Run Single Test Class:
bash
sf apex run test --class-names MyClassTest --code-coverage --result-format json --output-dir test-results --target-org [alias]
Run All Tests:
bash
sf apex run test --test-level RunLocalTests --code-coverage --result-format json --output-dir test-results --target-org [alias]
Run Specific Methods:
bash
sf apex run test --tests MyClassTest.testMethod1 --tests MyClassTest.testMethod2 --code-coverage --result-format json --target-org [alias]
Run Test Suite / All Tests (Concise):
bash
sf apex run test --suite-names MySuite --code-coverage --result-format json --target-org [alias]
sf apex run test --test-level RunLocalTests --code-coverage --result-format json --concise --target-org [alias]
Phase 3: Results Analysis
Parse
test-results/test-run-id.json
and report:
📊 TEST EXECUTION RESULTS
════════════════════════════════════════════════════════════════
SUMMARY
───────────────────────────────────────────────────────────────
✅ Passed: 42 ❌ Failed: 3 📈 Coverage: 78.5%
FAILED TESTS
───────────────────────────────────────────────────────────────
❌ AccountServiceTest.testBulkInsert
Line 45: System.AssertException: Assertion Failed
COVERAGE BY CLASS
───────────────────────────────────────────────────────────────
Class Lines Covered Uncovered %
AccountService 150 142 8 94.7% ✅
OpportunityTrigger 45 28 17 62.2% ⚠️
ContactHelper 30 15 15 50.0% ❌
Phase 4: Agentic Test-Fix Loop
See references/test-fix-loop.md for the full implementation flow and failure analysis decision tree.
When tests fail, the agentic loop: parses failures → reads source → identifies root cause → invokes sf-apex to fix → re-runs (max 3 attempts). Key error types: AssertException, NullPointerException, DmlException, LimitException, QueryException.
Cross-Skill: Flow Testing
For Flow-specific tests (not Apex), use
:
bash
sf flow run test --test-names FlowTest1,FlowTest2 --target-org [alias]
sf flow get test --test-run-id <id> --target-org [alias]
Unified Test Runner [Beta]:
sf logic run test --test-level RunLocalTests --code-coverage --target-org [alias]
(v2.107.6+). For production, prefer separate
and
.
Phase 5: Coverage Improvement
If coverage < threshold:
sf apex run test --class-names MyClassTest --code-coverage --detailed-coverage --result-format json
to identify uncovered lines
- Use sf-apex to generate test methods targeting those lines
- Use the sf-data skill: "Create 251 [ObjectName] records for bulk testing"
- Re-run and verify
Best Practices (120-Point Scoring)
| Category | Points | Key Rules |
|---|
| Test Coverage | 25 | 90%+ class coverage; all public methods tested; edge cases covered |
| Assertion Quality | 25 | Assert class used; meaningful messages; positive AND negative tests |
| Bulk Testing | 20 | Test with 251+ records; verify no SOQL/DML in loops under load |
| Test Data | 20 | Test Data Factory used; no hardcoded IDs; @TestSetup for efficiency |
| Isolation | 15 | SeeAllData=false; no org dependencies; mock external callouts |
| Documentation | 15 | Test method names describe scenario; comments for complex setup |
Thresholds: 108+ Excellent | 96+ Good | 84+ Acceptable | 72+ Below standard | <72 BLOCKED
Test Patterns & Templates
See references/test-patterns.md for full Apex code examples of all 4 patterns.
| Pattern | Template | Use Case |
|---|
| Basic Test Class | | Given-When-Then with @TestSetup, positive + negative |
| Bulk Test (251+) | | Cross 200-record batch boundary, governor limit check |
| Mock Callout | assets/mock-callout-test.cls
| HttpCalloutMock for external API testing |
| Test Data Factory | assets/test-data-factory.cls
| Reusable data creation with convenience insert |
Additional templates:
(35x faster tests),
assets/stub-provider-example.cls
(dynamic behavior)
Testing Guardrails (MANDATORY)
BEFORE running tests, verify:
| Check | Command | Why |
|---|
| Org authenticated | sf org display --target-org [alias]
| Tests need valid org connection |
| Classes deployed | sf project deploy report --target-org [alias]
| Can't test undeployed code |
| Test data exists | Check @TestSetup or TestDataFactory | Tests need data to operate on |
NEVER do these:
| Anti-Pattern | Problem | Correct Pattern |
|---|
| Tests depend on org data, break in clean orgs | Always (default) |
| Hardcoded Record IDs | IDs differ between orgs | Query or create in test |
| No assertions | Tests pass without validating anything | Assert every expected outcome |
| Single record tests only | Misses bulk trigger issues | Always test with 200+ records |
| without | Async code won't execute | Always pair start/stop |
CLI Command Reference
| Command | Purpose | Example |
|---|
| Run tests | See Phase 2 examples |
| Get async test status | |
| List debug logs | |
| Stream logs real-time | |
Key flags:
,
,
,
,
--test-level RunLocalTests
,
Common Test Failures & Fixes
| Failure | Likely Cause | Fix |
|---|
| User + non-setup object in same txn | Use or separate transactions |
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
| Trigger or flow error | Check trigger logic with debug logs |
| Test data incomplete | Add required fields to TestDataFactory |
| Unique field conflict | Use dynamic values or delete existing |
FIELD_CUSTOM_VALIDATION_EXCEPTION
| Validation rule fired | Meet validation criteria in test data |
| Record lock conflict | Use or retry logic |
Cross-Skill Integration
| Skill | When to Use | Example |
|---|
| sf-apex | Generate test classes, fix failing code | Use the sf-apex skill: "Create test class for LeadService" |
| sf-data | Create bulk test data (251+ records) | Use the sf-data skill: "Create 251 Leads for bulk testing" |
| sf-deploy | Deploy test classes to org | Use the sf-deploy skill: "Deploy tests to sandbox" |
| sf-debug | Analyze failures with debug logs | Use the sf-debug skill: "Analyze test failure logs" |
Dependencies
Required: Target org with
CLI authenticated
Recommended: sf-apex (auto-fix), sf-data (bulk test data), sf-debug (log analysis)