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

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.2.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.2.0
File Size Uploaded
pytest-docker-0.2.0.tar.gz 3.8 kB Details

Built distributions (wheels)

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

Total release size: 12.4 kB

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

Download URL pytest-docker-0.2.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
482e6b4d16eab99f5ff329c6f00e55f74c7d49fa8c93c8ccf51b3a28c023036b
BLAKE2b-256 checksum
How to use checksums
085c51b8d015163b8f7a77d8484aa9e4b9020223c11e4da6d34098c958002abe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

Download URL pytest_docker-0.2.0-py2-none-any.whl
Size 4.3 kB
Tags Python 2
SHA-256 checksum
How to use checksums
87432575a34fc172a9c295f92ce26bf3cd523ca942c7b954b8e08050560660f1
BLAKE2b-256 checksum
How to use checksums
95115469578ce107fff1a973f8262452c1c74df71157ef4f259c2a17bc044918
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

3.2.5

2 release files

3.2.4

2 release files

3.2.3

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.6.1

3 release files

0.6.0

3 release files

0.5.0

3 release files

0.4.0

3 release files

0.3.0

3 release files

This release

0.2.0 This release

3 release files

0.1.0

3 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