Skip to main content

PyDocks

PyDocks is a group of pytest fixures for running tests with Docker containers

Demonstration:

import pytest
import asyncpg

@pytest.mark.asyncio
async def test_postgresql_execute_command(postgresql_container):
    # Connect to the PostgreSQL database
    conn = await asyncpg.connect(
        host="127.0.0.1",
        port=5433,
        user="postgres",
        password="postgres",
        database="postgres",
    )

    try:
        # Execute a simple command
        result = await conn.fetchval("SELECT 1")
        assert result == 1, "Failed to execute command on PostgreSQL"
    finally:
        # Close the connection
        await conn.close()

Table of Contents

Description

PyDocks is a Python library that provides a set of pytest fixtures for running tests with Docker containers. It simplifies the process of setting up, managing, and tearing down Docker containers during test execution.

Key features include:

  • Easy integration with pytest
  • Support for PostgreSQL, Hashicorp Vault, Redis, Valkey, and more
  • Automatic container cleanup
  • Configurable container settings
  • Reusable session-scoped containers for improved test performance

PyDocks is designed to make testing with Docker containers more efficient and less error-prone, allowing developers to focus on writing tests rather than managing infrastructure.

Installation

# Install the dependency
pip install pydocks
uv add pydocks
poetry add pydocks

Documentation

Preview the site locally with just docs-serve or build it with just docs-build.

Development

Install just and uv, then:

just install    # install dependencies
just test       # run tests (no coverage gate)
just test-cov   # run tests with 100% coverage enforcement
just coverage   # show coverage report from the last test-cov run
just lint       # run linter and formatter
just check      # run pyright
just docs-build # build MkDocs site
just docs-serve # preview docs locally
just --list     # list all recipes

Tests collect coverage via just test-cov, which fails if line coverage drops below 100%. CI runs the same recipe, publishes a coverage summary in the job output, posts a comment on pull requests, and uploads coverage.xml / HTML reports as workflow artifacts.

Usage

Remove all old containers

import logging
import pytest
import pytest_asyncio

logger = logging.getLogger(__name__)

@pytest_asyncio.fixture(scope="session", loop_scope="session", autouse=True)
async def begin_clean_all_containers(postgresql_clean_all_containers):
    logger.info(
        "Beginning container cleanup session",
        extra={"feature": "postgresql"},
    )

Use a function container

import pytest

@pytest.mark.asyncio
async def test_postgresql_execute_command(postgresql_container):
  ...

Use a session container, to keep the container to use it in multiple tests

import pytest

@pytest.mark.asyncio(loop_scope="session")
async def test_reuse_postgresql_container_1_2(postgresql_container_session):
  ...
  # postgresql_container_session creates a new container

@pytest.mark.asyncio(loop_scope="session")
async def test_reuse_postgresql_container_2_2(postgresql_container_session):
  ...
  # postgresql_container_session uses the same instance of container created in test_reuse_postgresql_container_1_2

Available Containers

PyDocks provides fixtures for the following Docker containers:

  • PostgreSQL: postgresql_container, postgresql_container_session, postgresql_clean_all_containers
  • Redis: redis_container, redis_container_session, redis_clean_all_containers
  • Valkey: valkey_container, valkey_container_session, valkey_clean_all_containers
  • Hashicorp Vault: vault_container, vault_container_session, vault_clean_all_containers
  • Ubuntu: ubuntu_container, ubuntu_container_session, ubuntu_clean_all_containers
  • Alpine: alpine_container, alpine_container_session, alpine_clean_all_containers
  • OpenTofu: opentofu_container, opentofu_container_session, opentofu_clean_all_containers

License

PyDocks is released under the MIT License. See the LICENSE file for more details.

Contact

For questions, suggestions, or issues related to PyDocks, please open an issue on the GitHub repository.

Metadata

Release files for pydocks 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydocks 2.0.0
File Size Uploaded
pydocks-2.0.0.tar.gz 199.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydocks 2.0.0
File Interpreter ABI Platform
pydocks-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 239.4 kB

Release files / pydocks-2.0.0.tar.gz

Download URL pydocks-2.0.0.tar.gz
Size 199.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d6d9488f069e79be68f990cba2f580823c6bbb1015e669e86ca60ec3c46ce3cc
BLAKE2b-256 checksum
How to use checksums
da6b2fe2a611fabf45115e82f8b48a176b725e7b236bc6d3a2550eede993e3c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / pydocks-2.0.0-py3-none-any.whl

Download URL pydocks-2.0.0-py3-none-any.whl
Size 39.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b60dd5c630e433d21185d9f3e7ebee3515cc7da6e88c6c7ddbcef1c6ab1c49f4
BLAKE2b-256 checksum
How to use checksums
614314c3d967b33af0dd9106907497a4f0ae5ac50954e265457c67af2ff4cbee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page