Claude Code के साथ Complete Guide
Claude Code का उपयोग करके complete guide सीखें। Practical tips और code examples शामिल हैं।
test戦略को Claude Code से設計・implement करना
testは書いたほうがいいとわかっていても、どこ तकलिखनाべきか、何 testすべきかの判断は難しい चीज़ है।Claude Code का उपयोग करके、testピラミッドに基づいた戦略的なtest設計とimplementationをefficiently進められ है।
testピラミッドの実践
> Projectのtest戦略を提案して。
> testピラミッドに基づいて、
> 単体test、integrationtest、E2Etestの
> 比率と対象を整理して。
| test種別 | 比率 | 対象 | ツール |
|---|---|---|---|
| 単体test | 70% | ビジネスロジック、utility | Vitest |
| integrationtest | 20% | API、DBintegration、component | Vitest + Testing Library |
| E2Etest | 10% | 主要userフロー | Playwright |
単体testの自動generate
> src/services/ निम्नलिखितの全functionの単体testをबनाओ。
> 正常系、異常系、境界値をカバーして。
> カバレッジ80%以ऊपरを目標に。
// src/services/pricing.ts
export function calculatePrice(
basePrice: number,
quantity: number,
discountRate: number
): number {
if (quantity < 0) throw new Error("数量は0以ऊपरज़रूरीです");
if (discountRate < 0 || discountRate > 1) {
throw new Error("割引率は0~1の範囲で指定してください");
}
const subtotal = basePrice * quantity;
return Math.round(subtotal * (1 - discountRate));
}
// __tests__/services/pricing.test.ts
import { describe, it, expect } from "vitest";
import { calculatePrice } from "../../services/pricing";
describe("calculatePrice", () => {
// 正常系
it("基本価格 × 数量を返す", () => {
expect(calculatePrice(100, 5, 0)).toBe(500);
});
it("割引を適用する", () => {
expect(calculatePrice(1000, 1, 0.1)).toBe(900);
});
it("小数点निम्नलिखितを四捨五入する", () => {
expect(calculatePrice(333, 3, 0.1)).toBe(899);
});
// 境界値
it("数量0で0を返す", () => {
expect(calculatePrice(100, 0, 0)).toBe(0);
});
it("割引率100%で0を返す", () => {
expect(calculatePrice(100, 5, 1)).toBe(0);
});
// 異常系
it("負の数量でerrorを投げる", () => {
expect(() => calculatePrice(100, -1, 0)).toThrow("数量は0以ऊपर");
});
it("範囲बाहरの割引率でerrorを投げる", () => {
expect(() => calculatePrice(100, 1, 1.5)).toThrow("割引率は0~1");
expect(() => calculatePrice(100, 1, -0.1)).toThrow("割引率は0~1");
});
});
integrationtestのgenerate
APIendpointのintegrationtest generateし है।
> /api/orders endpointのintegrationtestをबनाओ。
> test用DBका उपयोग करके実際のCRUD操作 test。
> testबीचでdataが干渉しない तरह。
import { describe, it, expect, beforeEach, afterAll } from "vitest";
import request from "supertest";
import { app } from "../../app";
import { prisma } from "../../lib/prisma";
describe("Orders API Integration", () => {
beforeEach(async () => {
await prisma.order.deleteMany();
await prisma.user.upsert({
where: { id: "test-user" },
update: {},
create: { id: "test-user", email: "[email protected]", name: "Test" },
});
});
afterAll(async () => {
await prisma.$disconnect();
});
it("POST /api/orders で注文 createできる", async () => {
const res = await request(app)
.post("/api/orders")
.set("Authorization", "Bearer test-token")
.send({
items: [{ productId: "P1", quantity: 2, price: 1000 }],
});
expect(res.status).toBe(201);
expect(res.body.data.id).toBeDefined();
expect(res.body.data.totalAmount).toBe("2000");
});
it("GET /api/orders で注文list fetchできる", async () => {
// Testsdatacreate
await prisma.order.create({
data: {
userId: "test-user",
totalAmount: 5000,
status: "confirmed",
},
});
const res = await request(app)
.get("/api/orders")
.set("Authorization", "Bearer test-token");
expect(res.status).toBe(200);
expect(res.body.data).toHaveLength(1);
});
});
E2Etestのgenerate
主要なuserフローをPlaywrightでtestし है।
> user登録 सेlogイン तकのE2Etestを
> Playwrightでबनाओ。
import { test, expect } from "@playwright/test";
test.describe("user登録・logインフロー", () => {
test("新規user登録 सेdashboarddisplay तक", async ({ page }) => {
// 登録pageにアクセス
await page.goto("/register");
await expect(page.getByRole("heading", { name: "新規登録" })).toBeVisible();
// form入力
await page.getByLabel("メールアドレス").fill("[email protected]");
await page.getByLabel("pathワード").fill("SecurePass123");
await page.getByLabel("pathワードconfirm").fill("SecurePass123");
// 登録実行
await page.getByRole("button", { name: "登録" }).click();
// dashboardにredirectされる
await expect(page).toHaveURL("/dashboard");
await expect(page.getByText(" तरहこそ")).toBeVisible();
});
test("無効なメールアドレスでerrordisplay", async ({ page }) => {
await page.goto("/register");
await page.getByLabel("メールアドレス").fill("invalid-email");
await page.getByRole("button", { name: "登録" }).click();
await expect(page.getByText("有効なメールアドレスを入力")).toBeVisible();
});
});
testカバレッジの改善
> testカバレッジレポート generateして、
> カバレッジが低いfileにtestをadd करो。
> 80%以ऊपरを目標に。
# Claude Codeが実行するcommand
npx vitest --coverage --run
TDDでのtest先行developmentはTDDとClaude Codeの相性を、CI/CDでのtest自動実行はCI/CDpipelineconstruction guideをदेखें。debugでのtestutilizationはdebugテクニックcomplete guideもあわせてदेखें。
Summary
test戦略のbuildは「何をकौन साレベルでtestするか」の判断がimportant है।Claude Codeにtestピラミッドを意識させて指示すれば、効率的で効果的なtest体系がbuild किया जा सकता है。まずは単体test से始めて、段階的にカバレッジを広げましょう。
testframeworkके details के लिएVitestofficial documentation、Claude Codeके बारे मेंはAnthropicofficial documentationをदेखें。
Related Posts
Claude Code Hooks में Mastery: Auto-Format, Auto-Test, और बहुत कुछ
Claude Code hooks से auto-formatting और auto-testing setup करना सीखें। Practical configuration examples और real-world use cases शामिल हैं।
Claude Code MCP Server Setup और Practical Use Cases
Claude Code की MCP server capabilities की comprehensive guide। External tools connect करना, servers configure करना, और real-world integration examples सीखें।
CLAUDE.md लिखने की Complete Guide: Project Configuration की Best Practices
Effective CLAUDE.md files लिखने की thorough guide। अपना tech stack, conventions, और project structure communicate करना सीखें और Claude Code की output quality maximize करें।