Skip to main content

Pytest fixures for running tests with Docker containers

Project description

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

Usage

Remove all old containers

import pytest
import pytest_asyncio
from loguru import logger

@pytest_asyncio.fixture(scope="session", loop_scope="session", autouse=True)
async def begin_clean_all_containers(postgresql_clean_all_containers):
    logger.info("Begin - clean all containers")

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydocks-1.10.0.tar.gz (79.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydocks-1.10.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file pydocks-1.10.0.tar.gz.

File metadata

  • Download URL: pydocks-1.10.0.tar.gz
  • Upload date:
  • Size: 79.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pydocks-1.10.0.tar.gz
Algorithm Hash digest
SHA256 2dc8f48b87a9b73e28a798e0e6b85ef3d0916c44104d43daef357d4320c7662b
MD5 ef0fcd9aead1b2059d9e3d4c4adb4fb1
BLAKE2b-256 c8629e8a4826a56924fbd728ff4d124f8da0f66d198caf026d284b42ecc42af6

See more details on using hashes here.

File details

Details for the file pydocks-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: pydocks-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pydocks-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 966678b27ae06f4f095657ddb943cef68794de2cae0fd8c0b3e855f7e91a2d56
MD5 832869de3d36d819d3f07616eef66d8b
BLAKE2b-256 d804eab42d9f9754727cdebd70ee5ce8c2f7481ec228f65233cd3abac6c0feb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page