API Test Automation with Claude Code: A QA Practical Guide
API Test Automation using Claude Code. A QA Practical Guide. Includes practical code examples.
APIテスト自動化をClaude Codeで実践する
APIの品質を保証するためには包括的なテストが不可欠です。Claude Codeを使えば、テストケースの設計からモック生成、CI統合まで一気に自動化できます。
テストケース設計
REST APIのCRUDテスト
> ユーザーAPIのCRUDテストをVitest + supertestで作成して。
> 正常系・異常系・境界値テストを網羅して。
// tests/api/users.test.ts
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import request from 'supertest';
import { app } from '../../src/app';
import { resetDatabase, seedUsers } from '../helpers/db';
describe('Users API', () => {
beforeEach(async () => {
await resetDatabase();
await seedUsers();
});
describe('GET /api/users', () => {
it('ユーザー一覧を取得できる', async () => {
const res = await request(app)
.get('/api/users')
.expect(200);
expect(res.body.data).toBeInstanceOf(Array);
expect(res.body.data.length).toBeGreaterThan(0);
expect(res.body.data[0]).toHaveProperty('id');
expect(res.body.data[0]).toHaveProperty('name');
expect(res.body.data[0]).not.toHaveProperty('password');
});
it('ページネーションが正しく動作する', async () => {
const res = await request(app)
.get('/api/users?page=1&limit=5')
.expect(200);
expect(res.body.data.length).toBeLessThanOrEqual(5);
expect(res.body.meta).toHaveProperty('totalPages');
expect(res.body.meta).toHaveProperty('currentPage', 1);
});
});
describe('POST /api/users', () => {
it('新規ユーザーを作成できる', async () => {
const newUser = {
name: 'テストユーザー',
email: '[email protected]',
password: 'SecurePass123!',
};
const res = await request(app)
.post('/api/users')
.send(newUser)
.expect(201);
expect(res.body.data.name).toBe(newUser.name);
expect(res.body.data.email).toBe(newUser.email);
});
it('不正なメールアドレスでは400を返す', async () => {
const res = await request(app)
.post('/api/users')
.send({ name: 'Test', email: 'invalid', password: 'Pass123!' })
.expect(400);
expect(res.body.errors).toBeDefined();
});
it('重複メールアドレスでは409を返す', async () => {
await request(app)
.post('/api/users')
.send({ name: 'User1', email: '[email protected]', password: 'Pass123!' });
await request(app)
.post('/api/users')
.send({ name: 'User2', email: '[email protected]', password: 'Pass456!' })
.expect(409);
});
});
});
モックとスタブの生成
外部API依存のテスト
> Stripe決済APIのモックを作成して。
> MSWを使って。
// tests/mocks/handlers.ts
import { http, HttpResponse } from 'msw';
export const handlers = [
http.post('https://api.stripe.com/v1/charges', () => {
return HttpResponse.json({
id: 'ch_test_123',
amount: 1000,
currency: 'jpy',
status: 'succeeded',
});
}),
http.post('https://api.stripe.com/v1/refunds', () => {
return HttpResponse.json({
id: 're_test_456',
amount: 1000,
status: 'succeeded',
});
}),
];
コントラクトテスト
API仕様と実装の整合性を検証するコントラクトテストもClaude Codeで生成できます。OpenAPIスキーマとレスポンスを自動比較するテストを構築できます。
パフォーマンステスト
> k6を使ったAPIの負荷テストスクリプトを作成して。
> 段階的な負荷増加シナリオで。
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 50 },
{ duration: '3m', target: 50 },
{ duration: '1m', target: 100 },
{ duration: '3m', target: 100 },
{ duration: '1m', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
Summary
Claude Codeを使えば、APIテストの設計から実装、CI統合まで一気に自動化できます。テスト戦略ガイドやCI/CDパイプライン構築も合わせて参考にしてください。
APIテストの詳細はVitest公式ドキュメントを参照してください。
#Claude Code
#API testing
#automation
#testing
#quality assurance
Related Posts
Use Cases
Use Cases
How to Supercharge Your Side Projects with Claude Code [With Examples]
How to Supercharge Your Side Projects with Claude Code [With Examples]. A practical guide with code examples.
Use Cases
Use Cases
How to Automate Refactoring with Claude Code
Learn how to automate refactoring using Claude Code. Includes practical code examples and step-by-step guidance.
Use Cases
Use Cases
Complete CORS Configuration Guide with Claude Code
Learn about complete cors configuration guide using Claude Code. Practical tips and code examples included.