|

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

The Problem with Shared Environments

  • 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

LevelSetupIsolation
1Shared stagingNone (tests interfere)
2Docker Compose locallyPer developer
3Docker Compose in CIPer pipeline run
4Testcontainers per testPer test (maximum)
5Ephemeral 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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.