Skip to main content

Lovely Pytest Docker

https://img.shields.io/pypi/v/lovely-pytest-docker.svg https://img.shields.io/pypi/pyversions/lovely-pytest-docker.svg https://travis-ci.com/lovelysystems/lovely-pytest-docker.svg?branch=master

Create simple Pytest fixtures for writing integration tests based on Docker containers. The framework provides a service class to start and stop containers based Docker Compose. Each single container can be started individually.

Some parts of this package are taken from https://github.com/AndreLouisCaron/pytest-docker

Usage with Pytest

The docker compose file should contain all desired containers and the ports should be exposed. In the following example we want to start the app to test and a SQL database (Crate). Let’s assume there is a Dockerfile for the app in the same folder as the docker compose file:

version: "3"
services:
  app:
    build: .
    ports:
      - "8080"
    depends_on:
      - "crate"

  crate:
    image: crate:latest
    ports:
      - "4200"

In the conftest.py file we can declare the docker fixtures for each service we want to be able to start in the tests:

import pytest

@pytest.fixture(scope='session')
def docker_app(docker_services):
    docker_services.start('app')
    public_port = docker_services.wait_for_service("app", 8080)
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

@pytest.fixture(scope='session')
def docker_crate(docker_services):
    docker_services.start('crate')
    public_port = docker_services.wait_for_service("crate", 4200)
    dsn = "{docker_services.docker_ip}:{public_port}".format(**locals())
    return dsn

By default the fixture will look for the docker-compose.yml file in the tests subfolder of the path where pytest.ini resides (or the project’s root directory if no ini file is given - as in the tests example). In many cases you will want to override the location for the docker compose files. Just overwrite the docker_compose_files fixture in your conftest.py file:

@pytest.fixture(scope='session')
def docker_compose_files(pytestconfig):
    """Get the docker-compose.yml absolute path.
    Override this fixture in your tests if you need a custom location.
    """
    return [
        project_path('docker', 'docker-compose.yml'),
    ]

In your test file declare the fixtures you want to use:

def test_something(docker_app, docker_crate):
    # e.g. initialize database
    ...
    # test something (e.g. request to docker_app)
    ...

A working configuration and test example can be found in the tests folder of this package.

Execution in Docker Container

It’s possible to execute a command inside one of the Docker containers. Use the exec method of the docker_services fixture:

def test_exec(docker_services):
    # the first argument is the service name of the compose file,
    # the following arguments build the command to run
    res = docker_services.exec('crate', 'ls', '-a')

Wait for Service

The wait_for_service method of the service module checks whether the docker service is really started. By default it makes a HTTP GET request to the server’s / endpoint. The service will retry to check until a timeout of 30 seconds has passed.

Custom Service Checker

Some services may work differently and require a custom checker.

Create a custom service checker function which receives the IP address and the port as parameters:

def custom_service_checker(ip_address, port):
    # if service is ready
    return True
    # otherwise return False

In the fixture provide the custom service checker function as check_service parameter to the wait_for_service method:

@pytest.fixture(scope='session')
def docker_custom_service(docker_services):
    docker_services.start('custom_service')
    public_port = docker_services.wait_for_service(
        "app",
        8080,
        check_service=custom_service_checker
    )
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

Run Tests

Tests are held in the tests directory. Running tests is done via the pytest package with:

./gradlew pytest

Metadata

Release files for penguins-pytest-docker 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for penguins-pytest-docker 0.0.7
File Size Uploaded Provenance Actions
penguins-pytest-docker-0.0.7.tar.gz 6.1 kB Without provenance Details for penguins-pytest-docker-0.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for penguins-pytest-docker 0.0.7
File Interpreter ABI Platform Actions
penguins_pytest_docker-0.0.7-py3-none-any.whl Python 3 none any Details for penguins_pytest_docker-0.0.7-py3-none-any.whl

Total release size: 17.3 kB

Release files / penguins-pytest-docker-0.0.7.tar.gz

Download URL penguins-pytest-docker-0.0.7.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
247baf72e07e76429b8ddacb0285eadf273bef181275f3972d431bfee525d0c3
BLAKE2b-256 checksum
How to use checksums
4132355c7a590f2be38d2873c1445882d9143a65fd353dc97479cbf3ab7e2682
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

Release files / penguins_pytest_docker-0.0.7-py3-none-any.whl

Download URL penguins_pytest_docker-0.0.7-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
205982032b1d8b7b954552f5cddb25bb3dd0e0f88f79ff1439f1c484efb56180
BLAKE2b-256 checksum
How to use checksums
8629405811544f2b8239c822f8018357ca3a11923f7aa63cb640631e52d08b66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

Security for penguins-pytest-docker 0.0.7

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 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