📌

Integration Testing là gì?

Định nghĩa

Integration Testing kiểm tra sự tương tác giữa các components: API endpoints, database queries, external services.

💡 Khác biệt với Unit Test:
• Unit Test: isolate, mock dependencies
• Integration Test: real dependencies (DB, API)
🐹

Go httptest

func TestGetUserHandler(t *testing.T) {
    req := httptest.NewRequest("GET", "/api/users/1", nil)
    rr := httptest.NewRecorder()
    
    handler := http.HandlerFunc(GetUserHandler)
    handler.ServeHTTP(rr, req)
    
    if rr.Code != http.StatusOK {
        t.Errorf("got %d, want %d", rr.Code, http.StatusOK)
    }
}
🐍

Python FastAPI TestClient

from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_create_user():
    response = client.post("/api/users", json={"name": "John"})
    assert response.status_code == 201
    assert response.json()["name"] == "John"

JavaScript Supertest

const request = require('supertest');
const app = require('./app');

describe('User API', () => {
    test('creates user', async () => {
        const res = await request(app)
            .post('/api/users')
            .send({ name: 'John' })
            .expect(201);
        expect(res.body.name).toBe('John');
    });
});