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:

@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 containers, Redis
  • 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_asyncio

@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

@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

@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

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.6.0.tar.gz (73.2 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.6.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydocks-1.6.0.tar.gz
  • Upload date:
  • Size: 73.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pydocks-1.6.0.tar.gz
Algorithm Hash digest
SHA256 817960ac4a49014019450f2641bcdef55b958163b828dfbc565079e312e2b9cd
MD5 5c1bef0cda35f06a287b7041af45b394
BLAKE2b-256 9ad60c6e5c1c46b87873e05a8a3b93face2aef0e1c29fc4dd6119a9610de5afb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydocks-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pydocks-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 465b1ecad0024d06ade4a8056b11bf17878a0db9425c5d4c733115cf7ad0822e
MD5 c703bc990825374514dc2efa64e3cffc
BLAKE2b-256 d289c7da2fb91b5dc22b2cca576b05e397a02bca7777249ef95a6def9264788a

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