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 containers
  • 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.0.0.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

pydocks-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydocks-1.0.0.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pydocks-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c7a59bc196b193483d2e9cb954f729ad5d35d9a009eafde6af18176c5e5e12ed
MD5 58f16e75faec509f6ae67412eb8237f9
BLAKE2b-256 7707ff1222b4fa3d3c4eb15ea946694f6b00ae3d3679f0e71dc0104b7226e8b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydocks-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pydocks-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6275539fb8a54afae3cc56b2dbc3ca3ed510dcc1a6f533da4730e0a2eb3e227f
MD5 faa9b6e0812ca4eb12ca3cd32b576606
BLAKE2b-256 137c179a94b67ef7f593f1faadd22c8d3a452c5985aa36140f4a419a430f295c

See more details on using hashes here.

Supported by

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