designing-tests について
Designing Testsは、ソフトウェアテストの計画、作成、検証を支援する実践ガイドです。テストピラミッド、フレームワークの推奨、テンプレート、カバレッジ、テストデータ、モック、検証手順を扱います。
主な機能
- 01段階的なテストワークフロー
- 02テストピラミッドとテスト種別
- 03言語別フレームワークの推奨
- 04単体・統合・E2Eのテンプレート
- 05カバレッジとテストデータの指針
- 06モックと検証の戦略
活用シーン
- 01機能のテスト範囲を計画
- 02テスト種別とフレームワークを選定
- 03テスト結果とカバレッジを検証
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ cloudai-x/ claude-workflow-v2 --skill designing-testsインストール後、Claude Code で/designing-testsを入力して読み込まれたか確認します。
詳細
テストの設計
読み込むタイミング
- トリガー:テストの追加、テスト戦略の計画、カバレッジの改善、テストインフラストラクチャの構築
- スキップ:タスクにテストが含まれない、テスト以外のコード変更
テスト実装のワークフロー
このチェックリストをコピーして、進捗を記録します。
Test Implementation Progress:
- [ ] Step 1: Identify what to test
- [ ] Step 2: Select appropriate test type
- [ ] Step 3: Write tests following templates
- [ ] Step 4: Run tests and verify passing
- [ ] Step 5: Check coverage meets targets
- [ ] Step 6: Fix any failing tests
テストピラミッド
バランスの取れたカバレッジを実現するため、テストピラミッドを適用します。
/\
/ \ E2E Tests (10%)
/----\ - Critical user journeys
/ \ - Slow but comprehensive
/--------\ Integration Tests (20%)
/ \ - Component interactions
/------------\ - API contracts
/ \ Unit Tests (70%)
/________________\ - Fast, isolated
- Business logic focus
フレームワークの選択
JavaScript/TypeScript
| 種類 | 推奨 | 代替 |
|---|---|---|
| 単体 | Vitest | Jest |
| 統合 | Vitest + MSW | Jest + SuperTest |
| E2E | Playwright | Cypress |
| コンポーネント | Testing Library | Vitest Browser Mode |
Python
| 種類 | 推奨 | 代替 |
|---|---|---|
| 単体 | pytest | unittest |
| 統合 | pytest + httpx | pytest + requests |
| E2E | Playwright | Selenium |
| API | pytest + FastAPI TestClient | - |
Go
| 種類 | 推奨 |
|---|---|
| 単体 | testing + testify |
| 統合 | testing + httptest |
| E2E | testing + chromedp |
テスト構造のテンプレート
単体テスト
describe("[Unit] ComponentName", () => {
describe("methodName", () => {
it("should [expected behavior] when [condition]", () => {
// Arrange
const input = createTestInput();
// Act
const result = methodName(input);
// Assert
expect(result).toEqual(expectedOutput);
});
it("should throw error when [invalid condition]", () => {
expect(() => methodName(invalidInput)).toThrow(ExpectedError);
});
});
});
統合テスト
describe("[Integration] API /users", () => {
beforeAll(async () => {
await setupTestDatabase();
});
afterAll(async () => {
await teardownTestDatabase();
});
it("should create user and return 201", async () => {
const response = await request(app)
.post("/users")
.send({ name: "Test", email: "[email protected]" });
expect(response.status).toBe(201);
expect(response.body.id).toBeDefined();
});
});
E2E テスト
import { test, expect } from "@playwright/test";
test.describe("[E2E] User Registration Flow", () => {
test("should complete registration successfully", async ({ page }) => {
await page.goto("/register");
await page.getByTestId("email").fill("[email protected]");
await page.getByTestId("password").fill("SecurePass123!");
await page.getByTestId("submit").click();
await expect(page.locator(".welcome-message")).toBeVisible();
await expect(page).toHaveURL("/dashboard");
});
});
カバレッジ戦略
カバーする対象
- ✅ ビジネスロジック(100%)
- ✅ エッジケースとエラー処理(90%以上)
- ✅ API コントラクト(100%)
- ✅ 重要なユーザーパス(E2E)
- ⚠️ UI コンポーネント(スナップショット+インタラクション)
- ❌ サードパーティライブラリの内部実装
- ❌ 単純なゲッター/セッター
カバレッジのしきい値
Jest の設定を示しています。Vitest では、vitest.config の test.coverage.thresholds が相当します。
{
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"src/core/": {
"branches": 95,
"functions": 95
}
}
}
テストデータの管理
ファクトリー/ビルダー
// factories/user.js
export const userFactory = (overrides = {}) => ({
id: faker.string.uuid(),
name: faker.person.fullName(),
email: faker.internet.email(),
createdAt: new Date(),
...overrides,
});
// Usage
const admin = userFactory({ role: "admin" });
フィクスチャ
// fixtures/users.json
{
"validUser": { "name": "Test", "email": "[email protected]" },
"invalidUser": { "name": "", "email": "invalid" }
}
モック戦略
モックを使用する場面
- ✅ 外部 API とサービス
- ✅ 単体テストでのデータベース
- ✅ 決定性を確保するための時刻/日付
- ✅ ランダム値
- ❌ 内部モジュール(通常は使用しません)
- ❌ テスト対象のコード
モックの例
// API mocking with MSW
import { http, HttpResponse } from "msw";
export const handlers = [
http.get("/api/users", () => {
return HttpResponse.json([{ id: 1, name: "John" }]);
}),
];
// Time mocking
vi.useFakeTimers();
vi.setSystemTime(new Date("2024-01-01"));
テスト検証ループ
テストを記述したら、次の検証を実行します。
Test Validation:
- [ ] All tests pass: `npm test`
- [ ] Coverage meets thresholds: `npm test -- --coverage`
- [ ] No flaky tests (run multiple times)
- [ ] Tests are independent (order doesn't matter)
- [ ] Test names clearly describe behavior
テストが失敗した場合は、先に進む前に修正します。カバレッジが目標を下回る場合は、未カバーのコードパスに対するテストを追加します。
# Run tests
npm test
# Run with coverage
npm test -- --coverage
# Run specific test file
npm test -- path/to/test.spec.ts
# Run in watch mode during development
npm test -- --watch
コメント
Markdown 対応