Skip to main content

Pytest testing utilities with docker containers.

Project description

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

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"
    build: .
      - "8080"
      - "crate"

    image: crate:latest
      - "4200"

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

import pytest

def docker_app(docker_services):
    public_port = docker_services.wait_for_service("app", 8080)
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

def docker_crate(docker_services):
    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 file:

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_execute(docker_services):
    # the first argument is the service name of the compose file,
    # the following arguments build the command to run
    res = docker_services.execute('crate', 'ls', '-a')

Stopping a Docker Container

It’s possible to stop single Docker containers. Use the stop method of the docker_services fixture:

def test_stop(docker_services):
    # the first argument is the service name of the compose file,
    # the following arguments build the command to run
    res = docker_services.stop('crate')

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:

def docker_custom_service(docker_services):
    public_port = docker_services.wait_for_service(
    url = "http://{docker_services.docker_ip}:{public_port}".format(**locals())
    return url

To use another request path with the default checker the url_checker method can be used to create a check_url method for another path:


Run Tests

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

./gradlew pytest

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

lovely-pytest-docker-0.3.0.tar.gz (8.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page