test-writer について
ユニット、統合、エンドツーエンドテストを書く開発者向けのスキルです。テスト種別の選択、AAA に沿ったケース作成、境界条件の網羅、フィクスチャやモックの作成を案内します。
主な機能
- 01ユニット・統合・E2E テストの作成
- 02対象コードに応じたテスト種別の選択
- 03準備・実行・検証によるケース構成
- 04境界条件とエラー経路の網羅
- 05フィクスチャとモックデータの作成
活用シーン
- 01関数やクラスの単体テスト
- 02API とデータベースの連携確認
- 03エンドツーエンドのユーザーワークフロー検証
- 04境界条件や失敗ケースのテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ partme-ai/ full-stack-skills --skill test-writerインストール後、Claude Code で/test-writerを入力して読み込まれたか確認します。
詳細
このスキルを使用するタイミング
ユーザーが次のことを望んでいる場合は、いつでもこのスキルを使用してください。
- 関数、クラス、モジュールのユニットテストを作成する
- API エンドポイントやデータベースとのやり取りの統合テストを作成する
- ユーザーワークフローのエンドツーエンドテストを作成する
- エッジケースやエラー処理のテストカバレッジを追加する
- テストフレームワーク(pytest、Jest、JUnit、Vitest、Playwright)をセットアップする
- テストフィクスチャやモックデータを生成する
このスキルの使い方
ワークフロー
- テスト対象のコードを特定する - 関数、クラス、モジュールを読み、その動作を把握します
- テストの種類を選択する - ユニット(独立)、統合(複数コンポーネント)、または E2E(全体のフロー)
- AAA パターンを使ってテストを作成する - 入力を準備し、関数を実行し、期待される結果を検証します
- エッジケースをカバーする - 空の入力、境界値、エラー条件、同時アクセス
ユニットテストの例(pytest)
import pytest
from decimal import Decimal
from orders import calculate_order_total, OrderItem
class TestCalculateOrderTotal:
def test_single_item_no_discount(self):
items = [OrderItem(price=Decimal("10.00"), quantity=2)]
result = calculate_order_total(items, discount_pct=0.0, tax_rate=0.08)
assert result == Decimal("21.60")
def test_applies_discount_before_tax(self):
items = [OrderItem(price=Decimal("100.00"), quantity=1)]
result = calculate_order_total(items, discount_pct=0.1, tax_rate=0.10)
assert result == Decimal("99.00")
def test_empty_items_returns_zero(self):
result = calculate_order_total([], discount_pct=0.0, tax_rate=0.08)
assert result == Decimal("0.00")
def test_invalid_discount_raises_error(self):
items = [OrderItem(price=Decimal("10.00"), quantity=1)]
with pytest.raises(ValueError, match="discount_pct must be 0-1"):
calculate_order_total(items, discount_pct=1.5)
統合テストの例(Jest + Supertest)
import request from 'supertest';
import { app } from '../src/app';
import { db } from '../src/database';
describe('POST /api/users', () => {
afterEach(async () => { await db.query('DELETE FROM users WHERE email LIKE $1', ['%@test.com']); });
it('creates a user and returns 201', async () => {
const res = await request(app)
.post('/api/users')
.send({ name: 'Alice', email: '[email protected]' })
.expect(201);
expect(res.body).toMatchObject({ name: 'Alice', email: '[email protected]' });
});
it('returns 400 for missing email', async () => {
await request(app).post('/api/users').send({ name: 'Bob' }).expect(400);
});
});
ベストプラクティス
- 動作ごとにアサーションを1つにする - 各テストでは、特定の動作を1つ検証します
- 分かりやすいテスト名を付ける -
test_<behavior>_when_<condition>またはit('should <outcome> when <input>')の形式でテストに名前を付けます - 依存関係を分離する - 外部サービス、データベース、API にはモックやスタブを使用します
- 実装ではなく契約をテストする - 内部状態ではなく、出力と副作用を検証します
- CI でテストを実行する - すべてのコミットでテストに合格するようにし、重要な処理では行カバレッジ80%以上を目指します
キーワード
测试编写, test writing, unit test, integration test, e2e test, pytest, Jest, JUnit, Vitest, Playwright, mock, fixture, 单元测试, 集成测试, 端到端测试, test coverage
コメント
Markdown 対応