Không nên để test dùng chung production database. Dùng test database riêng, transaction rollback, fixture tạo schema, hoặc container database tùy mức integration.
Với dependency DB session, test có thể override dependency để inject session test:
async def override_db():
async with TestSessionLocal() as session:
yield session
app.dependency_overrides[get_db] = override_dbCần đảm bảo isolation giữa tests: reset data, rollback transaction hoặc tạo database/schema riêng cho từng test worker.
Tests should not share a production database. Use a separate test database, transaction rollback, schema fixtures or containerized databases depending on integration depth.
With a DB session dependency, tests can override it to inject a test session:
async def override_db():
async with TestSessionLocal() as session:
yield session
app.dependency_overrides[get_db] = override_dbEnsure isolation between tests: reset data, roll back transactions or create separate databases/schemas for each test worker.