jpskill.com
🛠️ 開発・MCP コミュニティ 🔴 エンジニア向け 👤 エンジニア・AI開発者

🛠️ テストBestPractices

testing-best-practices

モジュールに対するテスト設計、テストケース作成、テスト戦略立案時に、ユニットテストからE2Eテストまで階層を考慮した最適なテスト計画を支援するSkill。

⏱ ボイラープレート実装 半日 → 30分

📺 まず動画で見る(YouTube)

▶ 【衝撃】最強のAIエージェント「Claude Code」の最新機能・使い方・プログラミングをAIで効率化する超実践術を解説! ↗

※ jpskill.com 編集部が参考用に選んだ動画です。動画の内容と Skill の挙動は厳密には一致しないことがあります。

📜 元の英語説明(参考)

Use when designing tests, writing test cases, or planning test strategy for a module. Covers unit, integration, and e2e layering.

🇯🇵 日本人クリエイター向け解説

一言でいうと

モジュールに対するテスト設計、テストケース作成、テスト戦略立案時に、ユニットテストからE2Eテストまで階層を考慮した最適なテスト計画を支援するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o testing-best-practices.zip https://jpskill.com/download/4039.zip && unzip -o testing-best-practices.zip && rm testing-best-practices.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/4039.zip -OutFile "$d\testing-best-practices.zip"; Expand-Archive "$d\testing-best-practices.zip" -DestinationPath $d -Force; ri "$d\testing-best-practices.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して testing-best-practices.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → testing-best-practices フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-17
取得日時
2026-05-18
同梱ファイル
1

💬 こう話しかけるだけ — サンプルプロンプト

  • Testing Best Practices を使って、最小構成のサンプルコードを示して
  • Testing Best Practices の主な使い方と注意点を教えて
  • Testing Best Practices を既存プロジェクトに組み込む方法を教えて

これをClaude Code に貼るだけで、このSkillが自動発動します。

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

[Skill 名] testing-best-practices

テストの階層化ポリシー

単体テスト

目的: 個々の関数と不変条件を単独で検証することです。

  • データ駆動型: ハッピーパス、境界、エラー、エッジケースを網羅するパラメータ化されたテーブルです。
  • プロパティベース: すべての入力で保持されるべき不変条件 (例: 冪等性、ソートの安定性、ラウンドトリップのシリアライゼーション) をファズテストします。
  • モジュールの公開 API サーフェスからケースを導き出します。入力型/制約、出力形状、エラーモード、不変条件などです。

結合テスト / 契約テスト

目的: コンポーネントと外部サービス間の相互作用を検証することです。

  • API エンベロープ: リクエスト/レスポンスの形状、ステータスコード、コンテンツタイプ、ページネーションです。
  • エラー契約: エラーコード、エラー形状、レート制限、リトライです。
  • 認証とスコープ: トークン検証、ロールベースアクセス、テナント分離です。
  • 結果整合性: 制限時間内での収束を検証します。sleep ではなくポーリングを使用します。
  • 可能な場合はテスト間で認証状態を再利用し、冗長なログインフローを避けます。

E2E テスト

目的: フルスタックを通して実際のユーザーワークフローを検証することです。

  • モックは使用しません。実際のサービス、データベース、API を実行します。
  • ハッピーパスのワークフローのみを対象とし、エッジケースは下位レイヤーに任せます。
  • 状態許容型: クリーンな状態を前提とせず、以前の状態を許容し、それに対応して動作します。
  • 冪等性: 実行間のクリーンアップなしに繰り返し安全に実行できます。
  • フロー指向: 孤立したアサーションではなく、実際のデータパスをエンドツーエンドで検証します。

厳格なルール

  • シグネチャ、ソースの場所、行番号を捏造してはいけません。 コードベースから読み取ったもののみを参照してください。
  • 偽のフィクスチャを作成してはいけません。 テストデータは、リポジトリ内の実際のスキーマ、型、またはシードデータから導き出してください。
  • 製品コードにテスト専用のハックを加えてはいけません。 if (process.env.TEST) の分岐、テスト固有のエクスポート、テストのバックドアは禁止です。
  • E2E はクリーンな状態に依存してはいけません。 テストは、既存のデータ、以前のテスト実行、共有環境を許容する必要があります。

実行ガイダンス

プリフライトチェック (E2E の前)

  1. ターゲット環境が到達可能であることを確認します (ヘルスエンドポイント、ping)。
  2. 必要なサービスが実行中であることを確認します (データベース、API、認証プロバイダー)。
  3. テストユーザー/資格情報が存在し、機能していることを検証します。
  4. 誤った失敗を引き起こす可能性のある残存状態がないか確認します。ログに記録し、それによって失敗させてはいけません。

決定論的フィクスチャ

  • 生成されたデータにはシード付きのランダム性を使用します (シード付き faker、決定論的 UUID)。
  • フィクスチャは自己完結型であるべきです。テスト間のフィクスチャ依存関係を避けます。
  • 共有可能な可変フィクスチャオブジェクトよりもファクトリ関数を優先します。

非同期処理

  • 制限付きタイムアウトとバックオフでポーリングします。固定の sleep/waitForTimeout は絶対に使用しません。
  • 操作ごとに明示的なタイムアウトを設定します。タイムアウト時には、説明的なメッセージとともに迅速に失敗させます。
  • リトライ試行回数を制限します (例: 指数バックオフで最大 3 回のリトライ)。
  • 手動ループではなく、フレームワークネイティブの待機 (Playwright expect、非同期アサーション) を使用します。

フレーキーテストの処理

  • テスト実行ごとに単一のインフラストラクチャリトライを行います。2 回失敗した場合は、フレーキーではありません。
  • リトライ失敗時には、診断情報を収集します。スクリーンショット、ネットワークログ、サービスヘルス、タイムスタンプなどです。
  • 修正を試みる前に、失敗を分類します (フレーキー / 古い / バグ)。
  • フレーキーテストの「修正」として、任意の遅延やリトライループを追加してはいけません。

API サーフェスの発見

テストケースを生成する前に:

  • モジュールのソースを読み、エクスポート/公開関数を列挙します。
  • ユーザーリクエストと検査されたコードコンテキストからスコープを確認します。曖昧な場合は、仮定を述べ、保守的に進めます。
  • 各関数について: 入力型/制約、出力形状、エラーモード、不変条件を特定します。
  • 関数間の状態依存性および順序制約を調査します。

出力形式

Markdown を使用します。3 つのセクションを作成します。

テスト戦略 -- 各レイヤー (単体/結合/E2E) ごとに、関数/フローとそのカバレッジタイプを記述した箇条書きを一つ作成します。

テストマトリックス -- 関数ごとのテーブル: 列は ID | Category | Name | Input | Expected です。ケース ID スキームは {CATEGORY}-{NN} (HP, BV, ERR, EDGE) です。追記のみで、番号を振り直してはいけません。

実装計画 -- 順序付けられたステップ: フィクスチャ、単体テスト、結合テスト、E2E フロー、実行コマンドです。

CI ガイダンス

高速 PR スモークレーン

  • すべての PR で単体テスト + リンティング + 型チェックを実行します。
  • 重要な契約をカバーする結合テストのサブセットを実行します。
  • 目標: 5 分以内です。

夜間フルレーン

より高いプロパティベースのイテレーションカウントを持つ、完全な単体 + 結合 + E2E スイートです。リトライで合格したが最初に失敗したテストにはフラグを付けます。

ワークフロー

  1. 仕様またはコードがモジュールの動作 (型、制約、API サーフェス) を定義します。
  2. エージェント (このスキルを使用) がテスト戦略、マトリックス、および実装計画を作成します。
  3. test-writer エージェントが計画をターゲット言語のイディオムで実行可能なコードに変換します。
  4. 開発者がテストに合格するように実装します。
  5. 実装で不足しているケースが明らかになった場合、まずそれらを提案します。明示的に要求された場合にのみ仕様に追加します。
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Test layering policy

Unit tests

Purpose: verify individual functions and invariants in isolation.

  • Data-driven: parameterized tables covering happy path, boundary, error, and edge cases.
  • Property-based: fuzz invariants that must hold across all inputs (e.g., idempotency, sort stability, roundtrip serialization).
  • Derive cases from the module's public API surface: input types/constraints, output shape, error modes, invariants.

Integration / contract tests

Purpose: verify interactions between components and external services.

  • API envelope: request/response shape, status codes, content types, pagination.
  • Error contract: error codes, error shapes, rate limiting, retries.
  • Auth and scoping: token validation, role-based access, tenant isolation.
  • Eventual consistency: verify convergence within bounded time; poll rather than sleep.
  • Reuse auth state across tests where possible; avoid redundant login flows.

E2E tests

Purpose: verify real user workflows through the full stack.

  • No mocks; exercise real services, databases, and APIs.
  • Happy-path workflows only; save edge cases for lower layers.
  • State-tolerant: never assume a clean slate; tolerate and work with prior state.
  • Idempotent: safe to run repeatedly without cleanup between runs.
  • Flow-oriented: validate real data paths end-to-end rather than isolated assertions.

Hard rules

  • Never invent signatures, source locations, or line numbers. Only reference what you have read from the codebase.
  • No fabricated fixtures. Derive test data from actual schemas, types, or seed data in the repo.
  • No test-only hacks in product code. No if (process.env.TEST) branches, no test-specific exports, no test backdoors.
  • E2E must not rely on clean slate. Tests must tolerate pre-existing data, prior test runs, and shared environments.

Execution guidance

Preflight checks (before e2e)

  1. Verify the target environment is reachable (health endpoint, ping).
  2. Confirm required services are running (database, API, auth provider).
  3. Validate test user / credentials exist and are functional.
  4. Check for leftover state that could cause false failures; log it, do not fail on it.

Deterministic fixtures

  • Use seeded randomness for generated data (seeded faker, deterministic UUIDs).
  • Fixtures should be self-contained; avoid cross-test fixture dependencies.
  • Prefer factory functions over shared mutable fixture objects.

Async handling

  • Poll with bounded timeout and backoff; never use fixed sleep/waitForTimeout.
  • Set explicit timeout per operation; fail fast with a descriptive message on timeout.
  • Bound retry attempts (e.g., max 3 retries with exponential backoff).
  • Use framework-native waiting (Playwright expect, async assertions) over manual loops.

Flake handling

  • Single infrastructure retry per test run; if it fails twice, it is not flake.
  • On retry failure, collect diagnostics: screenshots, network logs, service health, timestamps.
  • Classify the failure (flaky / outdated / bug) before attempting a fix.
  • Never add arbitrary delays or retry loops as a flake "fix."

API surface discovery

Before generating test cases:

  • Read the module source to enumerate exports/public functions.
  • Confirm scope from the user request and inspected code context; if ambiguous, state assumptions and proceed conservatively.
  • For each function: input types/constraints, output shape, error modes, invariants.
  • Probe for state dependencies and ordering constraints between functions.

Output format

Use markdown. Produce three sections:

Test Strategy -- one bullet per layer (unit/integration/e2e) naming the functions/flows and their coverage type.

Test Matrix -- table per function: columns ID | Category | Name | Input | Expected. Case ID scheme: {CATEGORY}-{NN} (HP, BV, ERR, EDGE). Append-only; never renumber.

Implementation Plan -- ordered steps: fixtures, unit tests, integration tests, e2e flows, run command.

CI guidance

Fast PR smoke lane

  • Unit tests + linting + type-check on every PR.
  • Subset of integration tests covering critical contracts.
  • Target: under 5 minutes.

Nightly full lane

Full unit + integration + e2e suite with higher property-based iteration counts. Flag tests that pass on retry but failed initially.

Workflow

  1. Spec or code defines the module behavior (types, constraints, API surface).
  2. Agent (with this skill) produces test strategy, matrix, and implementation plan.
  3. test-writer agent translates the plan to runnable code in the target language's idiom.
  4. Developer implements to pass the tests.
  5. If implementation reveals missing cases, propose them first; append to spec only when explicitly requested.