How to Isolate a Database Per Parallel Test Worker
Parallel workers hitting one shared database race on the same rows, so give each worker its own database or schema.
Derive a unique database name from the worker id (for example PYTEST_XDIST_WORKER) so each worker migrates and mutates its own isolated store.
Steps
- Read the worker id from the runner (xdist exposes
PYTEST_XDIST_WORKER). - Create or select a database/schema named after that worker.
- Run migrations per worker before its tests start.
Terminal
Terminal
# example: derive a DB name per xdist worker
WORKER=${PYTEST_XDIST_WORKER:-gw0}
export DATABASE_URL="postgres://ci@localhost/app_${WORKER}"
createdb "app_${WORKER}" || true
pytest -n autoGotchas
- Creating a database per worker adds setup time; template databases speed this up.
- Tear down per-worker databases after the run so CI storage does not fill up.
Related guides
How to Run pytest in Parallel With pytest-xdistSpeed up a Python suite on a single CI runner with pytest-xdist, using -n auto to spread tests across every a…
How to Avoid Flaky Failures From Shared State Under ParallelismPrevent tests that pass serially but fail in parallel by removing shared state, fixed ports, temp files, and…