✦ For everyone, free.

Practical knowledge for real and everyday life

Home

1.1.3.5 Repeatable Test Setup

A focused guide to Repeatable Test Setup, connecting core concepts with practical Docker and container operations.

Repeatable test setup refers to the ability to run a test suite against an identical, freshly created environment every time, which Docker enables by instantiating containers from a fixed image rather than running tests against a long-lived, possibly drifted, environment.

Why Repeatability Matters for Tests

A test suite is only trustworthy if a failure means the code is wrong, not that the environment happened to be in an unusual state. Without containers, a test database might carry over leftover rows from a previous run, or a system library might have been upgraded since the suite last passed, introducing failures unrelated to the code being tested.

Starting From a Known State Every Time

A container started from an image always begins in the same state, so a test run that depends on a particular starting condition — an empty database, a clean filesystem — can guarantee that condition by simply starting a new container rather than resetting an existing one.

docker run --rm -e POSTGRES_PASSWORD=test --name test-db -d postgres:16
docker run --rm --network container:test-db myapp:test pytest
docker rm -f test-db
Repeatable Setup in CI

Continuous integration systems rely heavily on this property: each pipeline run typically starts fresh containers for the application and any service it depends on, runs the test suite, and discards everything afterward, so no state from one CI run can leak into the next.

services:
  test:
    image: myapp:test
    depends_on: [db]
  db:
    image: postgres:16
docker compose run test pytest
docker compose down -v

The -v flag also removes named volumes, ensuring that database state does not persist between separate test runs.

Parameterizing Test Environments

Because containers are cheap to create, the same test suite can be run against multiple versions of a dependency by simply swapping the image tag, which makes compatibility testing across versions practical without maintaining multiple physical machines.

docker compose run -e DB_IMAGE=postgres:14 test pytest
docker compose run -e DB_IMAGE=postgres:16 test pytest
Repeatability and Confidence in Results

When the test environment is guaranteed to be identical on every run, a passing test result carries more weight, and a failing test result can be trusted to point at an actual regression rather than an artifact of environmental drift accumulated since the last run.