Apex テスト実行、カバレッジ分析(コードの検査範囲の測定)、およびテスト修正ループを 120 ポイントのスコアリングで実行します。 **次のような場合に使用:** ユーザーが Apex テストを実行する、コードカバレッジ(カバレッジを確認する、テスト失敗を修正する、または *Test.cls / *_Test.cls ファイルを操作する必要がある場合 **トリガー対象:** ユーザーが Apex テストを実行する、コードカバレッジを確認する、テスト失敗を修正する、または *Test.cls / *_Test.cls ファイルに触れた場合 **トリガー対象外:** Apex 本番用コード(platform-apex-generate を使用)、Agentforce(営業支援 AI)エージェント テスト(agentforce-test を使用)、Jest/LWC(軽量コンポーネント)テスト(experience-lwc-generate を使用)を作成する場合
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).
次のような場合に使用: ユーザーがApex テストの実行と失敗分析を必要とするとき — テストの実行、カバレッジの確認、失敗の解釈、カバレッジの改善、Salesforce コードの体系的なテスト修正ループの管理。
platform-apex-test-run を使用するのは、以下の作業が含まれる場合です:
sf apex run test ワークフロー以下の場合は別のスキルに委譲してください:
platform-apex-generate スキルagentforce-test スキル以下を確認または推定してください:
以下を特定します:
失敗のデバッグ時は範囲を狭く始め、修正が安定してから拡大します。
焦点を当てるポイント:
コードやテストの品質に問題がある場合:
platform-apex-generate スキルにコード修正を委譲以下をカバーしてください:
| ルール | 理由 |
|---|---|
デフォルトで SeeAllData=false を使用 |
テストの独立性を確保し、組織固有のデータへの依存を防止 |
| すべてのテストは意味のある検証を必須とする | 検証なしのテストは何も証明せず、偽りの信頼を生む |
| 251 件以上のレコードで一括動作をテスト | トリガーは 200 件単位でバッチ処理;251 件の場合は境界を越える |
明確性が向上する場合は、ファクトリ / @TestSetup を使用 |
一箇所でのデータ作成の一貫性;テストメソッド間でロールバック |
Test.startTest() と Test.stopTest() を非同期処理でペアで使用 |
非同期操作(キューバブル、フューチャー)が検証前に完了するのを保証 |
| テスト内に組織依存の不安定な要素を隠さない | 組織の状態に紐付いた断続的な失敗を防止 |
| 問題 | 解決方法 |
|---|---|
| ローカルではテストが成功するが、CI 組織で失敗 | SeeAllData=true または組織固有のレコードへの未宣言の依存をチェック |
| リファクタリング後、カバレッジが予期せず低下 | まずクラスレベルのテストに焦点を当て、次に RunLocalTests に拡大して確認 |
| コールアウトテストで「Uncommitted work pending」エラー | DML と HTTP コールアウトは Test.startTest() でラップせずに混在させることはできない |
| モックが機能しない | コールアウトを行うコードの前に Test.setMock() が呼び出されていることを確認 |
@TestSetup データがテストメソッド内に存在しない |
@TestSetup データはテストメソッドごとにコミットされるため、再度クエリを実行;静的変数には保存しない |
| API バージョン 67.0 以上でアクセスレベルチェックが不十分 | SOQL / DML スタックトレースで CRUD / FLS アクセスエラーをチェック;ユーザーモードの動作を意図する場合は割り当てられたパーミッションセットで System.runAs を使用、またはシステムアクセスが必要な場合は正当な理由を文書化した SYSTEM_MODE パスを指定 |
完了時は、以下の順序で報告してください:
推奨形式:
テスト実行: <範囲>
組織: <エイリアス>
結果: <成功 / 部分的 / 失敗>
カバレッジ: <パーセンテージ / 主要クラス>
問題: <最も重要な失敗>
次のステップ: <クラスを修正、テストを追加、範囲を再実行、または回帰テスト範囲を拡大>
| 必要な作業 | 委譲先 | 理由 |
|---|---|---|
| 本番コードの修正またはテストクラスの作成 | platform-apex-generate スキル |
コード生成と修復 |
| 一括 / エッジケースのテストデータ作成 | platform-data-manage | 現実的なテストデータセット |
| 更新されたテストを組織にデプロイ | platform-metadata-deploy | デプロイメントワークフロー |
| 詳細な実行時ログを検査 | platform-apex-logs-debug | より詳しい失敗分析 |
| ファイル | 参照するタイミング |
|---|---|
references/cli-commands.md |
sf apex run test コマンドのすべてのフラグ、出力形式、非同期実行、カバレッジコマンド |
references/test-patterns.md |
テストクラステンプレート — 基本、一括(251+)、モックコールアウト、データファクトリパターン |
references/testing-best-practices.md |
コアテスト原則 — AAA パターン、命名規則、一括、異常系、モック戦略 |
references/test-fix-loop.md |
エージェント型テスト修正ループの実装と失敗分析の判定フロー |
references/mocking-patterns.md |
HttpCalloutMock、DML モック、StubProvider、セレクタモックパターン |
references/performance-optimization.md |
テスト実行時間を短縮する技法 — DML モック、SOQL モック、ループ最適化 |
assets/basic-test.cls |
テンプレート:@TestSetup、正常系 / 異常系 / 一括 / エッジケースメソッド付きの標準テストクラス |
assets/bulk-test.cls |
テンプレート:200 件のトリガーバッチ境界を越える 251+ レコードの一括テスト |
assets/mock-callout-test.cls |
テンプレート:HttpCalloutMock を使用した HTTP コールアウトモック |
assets/test-data-factory.cls |
テンプレート:作成および挿入ヘルパー付きの再利用可能な TestDataFactory |
assets/dml-mock.cls |
テンプレート:IDML インターフェース + DMLMock 実装(データベース不要のユニットテスト) |
assets/stub-provider-example.cls |
テンプレート:StubProvider ベースの依存性注入スタブ |
scripts/parse-test-results.py |
ツール後フック — sf apex run test JSON 出力を解析して自動修正ループ向けに失敗をフォーマット |
| スコア | 意味 |
|---|---|
| 108+ | 本番環境レベルの強力なテスト信頼度 |
| 96–107 | 良好なテストスイート(軽微なギャップあり) |
| 84–95 | 許容範囲だが、カバレッジ / 検証を強化 |
| < 84 | 基準以下;信頼する前に改訂が必要 |
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.
Use platform-apex-test-run when the work involves:
sf apex run test workflowsDelegate elsewhere when the user is:
platform-apex-generate skillagentforce-test skillAsk for or infer:
Identify:
Start narrow when debugging a failure; widen only after the fix is stable.
Focus on:
When the issue is code or test quality:
platform-apex-generate skill when neededCover:
| 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 |
| 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 |
| API version 67.0 and higher without necessary access level checks | Check failing SOQL/DML stack traces for CRUD/FLS access errors, using System.runAs with an assigned permission set when user-mode behavior is intended, or documenting a justified SYSTEM_MODE path when system access is required |
When finishing, report in this order:
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>
| 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 |
| 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 | 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 |
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。