Kiểm thử tích hợp giữa các module, API và database
Integration Testing kiểm tra sự tương tác giữa các components: API endpoints, database queries, external services.
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)
}
}
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"
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');
});
});