Use Cases

Claude Code के साथ API Test Automation: QA Practical Guide

Claude Code का उपयोग करके API Test Automation। QA Practical Guide। Practical code examples शामिल हैं।

Claude Code से API Test Automation को Practice करें

API की quality guarantee करने के लिए comprehensive testing अनिवार्य है। Claude Code का उपयोग करके, test case design से mock generation, CI integration तक एक बार में automate किया जा सकता है।

Test Case Design

REST API CRUD Tests

> User API के CRUD tests Vitest + supertest से बनाओ।
> Normal cases, error cases, और boundary value tests cover करो।
// 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('User list fetch कर सकता है', 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('Pagination correctly काम करता है', 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('New user create कर सकता है', async () => {
      const newUser = {
        name: 'Test User',
        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('Invalid email address पर 400 return करता है', async () => {
      const res = await request(app)
        .post('/api/users')
        .send({ name: 'Test', email: 'invalid', password: 'Pass123!' })
        .expect(400);

      expect(res.body.errors).toBeDefined();
    });

    it('Duplicate email address पर 409 return करता है', 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);
    });
  });
});

Mocks और Stubs Generate करना

External API Dependency Tests

> Stripe payment API का mock बनाओ।
> MSW use करो।
// 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',
    });
  }),
];

Contract Tests

API specification और implementation की consistency verify करने वाले contract tests भी Claude Code से generate किए जा सकते हैं। OpenAPI schema और response को automatically compare करने वाले tests बनाए जा सकते हैं।

Performance Tests

> k6 का उपयोग करके API load test script बनाओ।
> Step-wise load increase scenario के साथ।
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 test design से implementation, CI integration तक एक बार में automate किया जा सकता है। Testing Strategy Guide और CI/CD Pipeline Construction भी reference करें।

API testing की details के लिए Vitest Official Documentation देखें।

#Claude Code #API testing #automation #testing #quality assurance