Test Environment Management: Stop Sharing Environments, Start Using Docker and Terraform
Shared test environments cause 40% of CI failures. Different data, different configs, different service versions. Fix: disposable, consistent, isolated environments per test run.
🎠Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.
Contents
- Test A modifies data Test B depends on
- Someone deployed a broken service to staging
- Environment config drifts from production
- “Works on my machine” because local != CI != staging
Docker Compose for Local + CI Parity
version: '3.8'
services:
app:
build: .
environment:
DATABASE_URL: postgres://test:test@db:5432/testdb
REDIS_URL: redis://cache:6379
depends_on:
db: { condition: service_healthy }
cache: { condition: service_started }
ports: ['3000:3000']
db:
image: postgres:16
environment:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
healthcheck:
test: ["CMD-SHELL", "pg_isready -U test"]
interval: 5s
retries: 5
cache:
image: redis:7-alpine
playwright:
image: mcr.microsoft.com/playwright:v1.59.0
command: npx playwright test
depends_on: [app]
environment:
BASE_URL: http://app:3000
volumes:
- ./test-results:/app/test-results
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Testcontainers for Per-Test Isolation
import { PostgreSqlContainer } from '@testcontainers/postgresql';
let container;
test.beforeAll(async () => {
container = await new PostgreSqlContainer().withDatabase('testdb').start();
process.env.DATABASE_URL = container.getConnectionUri();
await runMigrations();
});
test.afterAll(async () => {
await container.stop(); // Clean slate
});
Environment Maturity Model
| Level | Setup | Isolation |
|---|---|---|
| 1 | Shared staging | None (tests interfere) |
| 2 | Docker Compose locally | Per developer |
| 3 | Docker Compose in CI | Per pipeline run |
| 4 | Testcontainers per test | Per test (maximum) |
| 5 | Ephemeral environments (Terraform) | Per PR branch |
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
