skillfed

pytest-docker

Simple pytest fixtures for Docker and Docker Compose based tests

pytest-docker v3.2.5 2.4M downloads/30d#3,092 on PyPI488
Permissive license MIT AGING released

What it is and what it does

pytest-docker is a pytest plugin that manages Docker containers as test fixtures. It reads a docker-compose.yml file, starts all defined services before tests run, and tears them down afterward—all handled transparently by pytest's fixture system. You define your services once in compose YAML, then inject a docker_services fixture into your tests to query container ports, wait for readiness, and interact with running services.

The plugin handles the boilerplate of container lifecycle management, port mapping discovery, and health checks. It supports both Docker Compose V2 (default, via the docker compose command) and V1 (via optional extra or custom fixture override). Tests run against real containerized services rather than mocks, making integration tests more realistic and catching issues that unit tests miss.

Use it for:

  • Write integration tests against a real database running in a container without manual docker-compose commands.
  • Test HTTP services by spinning up a containerized service and making requests to it from pytest.
  • Validate multi-service interactions in a single test run using docker-compose to define the stack.
  • Debug test failures in an IDE by pinning the compose project name and reusing containers across debug sessions.
  • Use multiple docker-compose files to compose different service stacks for different test scenarios.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

pytest-docker provides pytest fixtures that automatically spin up and tear down Docker containers from a docker-compose.yml file for the duration of your test session, eliminating manual container lifecycle management.

Yes. pytest-docker is a well-established plugin (in top 5000 PyPI packages by downloads) with low install friction, permissive licensing, no known vulnerabilities, and active maintenance. It solves a real problem—automating container lifecycle for integration tests—cleanly. Install it if your test suite needs real containerized services rather than mocks or if you're already using docker-compose for local development.

Install

pytest-docker on PyPI

pip

pip install pytest-docker

uv

uv add pytest-docker

poetry

poetry add pytest-docker

Installing pytest-docker

Before you install

Low friction: pure Python wheel with only pytest and attrs as runtime dependencies. Maintenance status is aging—last release 275 days ago—but the repository remains active with recent commits and no archived status.

License in practice

MIT license is permissive; safe for commercial and closed-source use without reciprocal obligations.

Quickstart

# Install
pip install pytest-docker

# In conftest.py or test file:
import pytest

@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
    return "docker-compose.yml"

# In test:
def test_service(docker_services):
    port = docker_services.port_for("myservice", 80)
    # Use port to connect to containerized service

Docker and Docker Compose V2 (or V1 via optional extra) must be installed and available in PATH on the test environment.

Verify before relying

  • Whether Docker and Docker Compose V2 (or V1 via optional extra) are assumed to be pre-installed on the test environment.
  • Performance characteristics when managing many containers or long-running test suites.
  • Compatibility with Docker Desktop on Windows and macOS beyond the stated OS classifiers.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 2 — pytest, attrs
Maintenance aging — 275 days since the last release
Last repo commit
First released
Downloads 2,387,633/month — #3,092 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytest_docker-3.2.5-py3-none-any.whl

Keywords: docker, docker-compose, pytest

Intended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: Microsoft :: WindowsOperating System :: POSIXOperating System :: UnixProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Utilities

Tags

pytest docker fixturesdocker compose integration testscontainerized pytest testsdocker test automationpytest docker compose plugincontainer-based testingdocker fixtures pytest
dockerintegration-testingfixtures

More Utilities packages