Skip to main content

pytest-docker: Docker-based integration tests

Description

Simple py.test fixtures for writing integration tests based on Docker containers. Specify all containers you need and pytest-docker will use Docker Compose to spin them up for the duration of your test suite.

Usage

Here is the basic recipe for writing a test that depends on a service that responds over HTTP:

import pytest
import requests

from requests.exceptions import (
    ConnectionError,
)

def is_responsive(url):
    """Check if something responds to ``url``."""
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return True
    except ConnectionError:
        return False

@pytest.fixture(scope='session')
def some_http_service(docker_ip, docker_services):
    """Ensure that "some service" is up and responsive."""
    url = 'http://' % (
        docker_ip,
        docker_services.port_for('abc', 123),
    )
    docker_services.wait_until_responsive(
       timeout=30.0, pause=0.1,
       check=lambda: is_responsive(url)
    )
    return url

def test_something(some_service):
    """Sample test."""
    response = requests.get(some_service)
    response.raise_for_status()

By default this plugin will try to open docker-compose.yml in your tests directory. If you need to use a custom location, override the docker_compose_file fixture inside your conftest.py file:

import pytest

@pytest.fixture(scope='session')
def docker_compose_file(pytestconfig):
    return os.path.join(
        str(pytestconfig.rootdir),
        'mycustomdir'
        'docker-compose.yml'
    )

You can allow this plugin to run your tests when Docker is not available. It will use the container port and localhost instead:

@pytest.fixture(scope='session')
def docker_allow_fallback():
    return True

Changelog

Version 0.3.0

  • Added --build option to docker-compose up command to automatically rebuild local containers.

Contributing

This py.test plug-in and its source code are made available to your under and MIT license. It is safe to use in commercial and closed-source applications. Read the license for details!

Found a bug? Think a new feature would make this plug-in more practical? No one is paid to support this software, but we welcome pull requests!

Metadata

Release files for pytest-docker 0.5.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 pytest-docker 0.5.0
File Size Uploaded Provenance
pytest-docker-0.5.0.tar.gz 4.0 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for pytest-docker 0.5.0
File Interpreter ABI Platform
pytest_docker-0.5.0-py3-none-any.whl Python 3 none any Details
pytest_docker-0.5.0-py2-none-any.whl Python 2 none any Details

Total release size: 13.0 kB

Release files / pytest-docker-0.5.0.tar.gz

Download URL pytest-docker-0.5.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
190e11a8eb679fae3678fb9128e7340c4193711f4fd01fae6689bc173464680a
BLAKE2b-256 checksum
How to use checksums
3d95e85696a18cb58100d8e415ba24311f4e43d7effe90ca573d57b699d4427d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pytest_docker-0.5.0-py3-none-any.whl

Download URL pytest_docker-0.5.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de15d8628b0375d0bb5a40894a83730c7d8590fb367a49f90732cc53f961f8f8
BLAKE2b-256 checksum
How to use checksums
d286a48f442acc8c5e6435417f00d2191c7efab1e99cb7fcd7b8f04675ac204e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pytest_docker-0.5.0-py2-none-any.whl

Download URL pytest_docker-0.5.0-py2-none-any.whl
Size 4.5 kB
Tags Python 2
SHA-256 checksum
How to use checksums
66d0d98a3229d95466453e2db730de2f159f3efeba1fd012f8d58707e69ffc49
BLAKE2b-256 checksum
How to use checksums
8fe0057d0f0f59d768fd086c5ae1290c05f5dda41579d640663e6bc833bcc25b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pytest-docker 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.5.0 This release

3 release files No provenance

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