Production Testing Strategy: Unit, Integration with Real DBs, and E2E Confidence
Building an effective testing pyramid: test doubles boundaries, running real ephemeral databases via Testcontainers, and preventing flaky CI/CD runs.
By Amr Samir• August 19, 2026• 1 min
Production Testing Strategy: Unit, Integration with Real DBs, and E2E Confidence
1. Rethinking the Testing Pyramid
Many engineering teams struggle with testing: either they maintain thousands of fragile unit tests with excessive mocks that test implementation details rather than behavior, or they rely exclusively on slow, flaky end-to-end tests.
code
+-----------------------------------------------------------------------+
| The Practical Testing Pyramid |
| |
| /\ [ E2E Tests ] -> Critical User Journeys (5-10%) |
| / \ [ Integration Tests] -> Real DB & Service Boundaries (40%) |
| /____\ [ Unit Tests ] -> Domain Logic & Pure Invariants (50%)|
+-----------------------------------------------------------------------+
2. The Danger of Over-Mocking
When you mock every database call, ORM query, and external library:
- Tests pass with 100% code coverage.
- In production, real SQL queries fail due to syntax errors, constraint violations, or type mismatches!
3. Integration Testing Against Real Ephemeral Databases (Testcontainers)
typescript
import { PostgreSqlContainer } from '@testcontainers/postgresql';
import { PrismaClient } from '@prisma/client';
describe('OrderService Integration Tests', () => {
let container: any;
let prisma: PrismaClient;
beforeAll(async () => {
container = await new PostgreSqlContainer().start();
prisma = new PrismaClient({ datasources: { db: { url: container.getConnectionUri() } } });
await runMigrations(prisma);
});
afterAll(async () => {
await prisma.$disconnect();
await container.stop();
});
it('should atomically deduct inventory on checkout', async () => {
const service = new OrderService(prisma);
const order = await service.createOrder({ productId: 'p1', qty: 2 });
expect(order.status).toBe('CONFIRMED');
const updatedStock = await prisma.product.findUnique({ where: { id: 'p1' } });
expect(updatedStock.stock).toBe(8); // Real DB update verified!
});
});
4. Summary Checklist
- Unit test complex algorithmic logic and pure business domain rules.
- Integration test database queries and repository layers against real ephemeral PostgreSQL/Redis containers.
- Limit E2E tests strictly to top conversion flows (Sign Up, Checkout, Inquiries).
Recommended Posts
Related Projects
Qader - Accessible Examination & Learning Diagnostic System
An educational examination and assessment web application engineered with React and TypeScript for simulated timed tests, resilient local storage session recovery, and academic skill diagnostic reporting.